RFR: 6734168: BasicButtonUI uses wrong FontMetrics to Layout JButtons text

Prasanta Sadhukhan psadhukhan at openjdk.org
Fri Mar 21 11:37:58 UTC 2025


javax.swing.plaf.basic.BasicButtonUI uses wrong FontMetrics object to layout the text on a JButton. 
The paint(Graphics, JComponent) method of BasicButtonUI calculates the [FontMetrics](https://github.com/openjdk/jdk/blob/6656254c346ef505a48652fdf4dedd6edc020e33/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicButtonUI.java#L331) using the passed Graphics object without setting the buttons font before. If the buttons font varies from the currently set font of the passed graphics object, the font metrics do not fit the metrics of the expected font leading to truncated text on the button.
In WindowsLookAndFeel, the font in passed graphics object differs from currently set font as in 

button font javax.swing.plaf.FontUIResource[family=Tahoma,name=Tahoma,style=plain,size=10] 
graphics font java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12]

whereas in Metal and other L&F it is 
`font javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=bold,size=12]`

so the fix is made in Windows L&F to ensure to set the font of the button to the passed graphics object font before calculating the current FontMetrics.

-------------

Commit messages:
 - whitespace
 - 6734168: BasicButtonUI uses wrong FontMetrics to Layout JButtons text
 - 6734168: BasicButtonUI uses wrong FontMetrics to Layout JButtons text

Changes: https://git.openjdk.org/jdk/pull/24152/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=24152&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-6734168
  Stats: 117 lines in 2 files changed: 116 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/24152.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/24152/head:pull/24152

PR: https://git.openjdk.org/jdk/pull/24152


More information about the client-libs-dev mailing list