JDK-8226215 : JToggleButton text truncated with Windows L&F
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 10.0.2,13
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2019-06-12
  • Updated: 2019-08-05
  • Resolved: 2019-08-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 14
14Resolved
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
The text of a JToggleButton using certain characters such as lowercase "x" will be truncated when displayed using the Windows look and feel. This worked in JDK 8, 9, 10.0.1 and stopped working in JDK 10.0.2. It remains broken in JDK 11 and later (including JDK 13 EA).

REGRESSION : Last worked in version 10.0.1

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the provide `WindowsToggleButtonTextTruncation` class.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A toggle button and a regular button both displaying the label "x"
ACTUAL -
A toggle button displaying an elipsis ("...") and a regular button displaying "x"

---------- BEGIN SOURCE ----------
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

public class WindowsToggleButtonTextTruncation {
    private static final String LABEL = "x";

    public static void main(String... args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(WindowsToggleButtonTextTruncation.class.getName()).log(Level.SEVERE, null, ex);
        }

        JFrame frame = new JFrame("Toggle Button Text Truncation");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));

        frame.add(new JToggleButton(LABEL)); // Toggle button label will be truncated
        frame.add(new JButton(LABEL));

        frame.pack();
        frame.setVisible(true);
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Manually adjust the preferred size

FREQUENCY : always



Comments
I tried with -Dawt.useSystemAAFontSettings=lcd WindowsToggleButtonTextTruncation but I can still see the text without truncation as shown in "myscreenshot.png"
05-08-2019

Submitter has shared additional step to verify this issue successfully, therefore reopening the original report for rechecking. "This issue is reproducible if you have font smoothing enabled (e.g. via Windows control panel > System > Adjust the appearance and performance of Windows > Smooth edges of screen fonts). You can also explicitly specify antialiasing when running by specify the system property "awt.useSystemAAFontSettings": java -Dawt.useSystemAAFontSettings=lcd WindowsToggleButtonTextTruncation"
10-07-2019

I tried on my windows 10 Pro 1809 OSBuild:17763.316 with latest jdk14 binary and I am not able to reproduce. Attached is the screenshot.
26-06-2019

Reported as a regression with JDK 10.0.2, the text of a JToggleButton using certain characters such as lowercase "x" appears truncated when displayed using the Windows look and feel. Checked this as reported with provided test case, however couldn't confrm the issue. Submitter has shared additional information conveying the issue continue to reproduce at his end when checked inside a VM and another physical machine. ====================== I've attached two screenshots, one compiled with and run against JDK 8u202 and the other against Oracle's JDK 11.0.3. Working (when using JDK 8u202) Truncated (when using JDK 11.0.3) This is running on Windows 10. I've tried on two separate machines with the same result. The Windows specifications for these two machines: Machine 1 (Virtual machine) Edition: Windows 10 Pro N Version: 1803 OS build: 17134.191 Machine 2 (Physical machine, Dell laptop) Edition: Windows 10 Enterprise Version: 1709 OS build: 16299.904 Both are running with LCD screens and I notice that the antialiasing hint is set to 0 (Default antialiasing text mode) on the graphics instance provided to `BasicToggleButtonUI.paint`. The `aaHint` value on the component is 4 (LCD HRGB antialiasing text mode) As a result, the font metrics retrieved on line 85 does not use the same antialiasing to calculate the text width in paint as it does when calculating the preferred size. The physical font in use on my systems is Tahoma 11 Plain and the character "x" is 1 pixel wider using the default antialiasing than when using LCD antialiasing. It's possible your system isn't using antialiasing or is using a different mode (possibly 6 (LCD VRGB antialiasing text mode) which may not affect the width). I note that `BasicButtonUI.paint` uses `SwingUtilities2.getFontMetrics(b, g)` to get the font metrics which does copy the antialiasing from the component. The fix I made that worked was to use `SwingUtilities2.getFontMetrics` in `BasicToggleButtonUI.paint` as well (see patch below). Looking at the code for both UI implementations, it seems that simply removing the `BasicToggleButtonUI.paint` override entirely would also solve the problem. The only difference I see is the call to `clearTextShiftOffset` in BasicButtonUI which could be done in the override if needed. I was able to resolve the issue by changing `BasicToggleButtonUI` to use `SwingUtilities2.getFontMetrics(c, g)` instead of `g.getFontMetrics()`. See source code for the patch. ---------- BEGIN SOURCE ---------- diff -r 175eb80c253a src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java --- a/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java Wed Apr 03 02:25:37 2019 +0100 +++ b/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java Wed Jun 12 14:12:22 2019 -0400 @@ -25,6 +25,7 @@ package javax.swing.plaf.basic; +import sun.swing.SwingUtilities2; import sun.awt.AppContext; import java.awt.*; @@ -82,7 +83,7 @@ ButtonModel model = b.getModel(); Dimension size = b.getSize(); - FontMetrics fm = g.getFontMetrics(); + FontMetrics fm = SwingUtilities2.getFontMetrics(c, g); Insets i = c.getInsets(); ---------- END SOURCE ----------
17-06-2019