3

after upgrading from Java 10 to Java 11 the text in a JTextField is not vertically centered anymore. Is there something I can do to get back the old behaviour? Following code run with JDK 10 (upper window in image) and JDK 11.0.8 for Windows (lower window in image) reproduces the problem.

import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class JTextFieldTest{
public static void main(String[] args) {
    JTextField textInputField = new JTextField(20);
    textInputField.setBackground(Color.ORANGE);
    textInputField.setFont(new Font("Calibri", Font.BOLD, 30));
    textInputField.setSize(textInputField.getPreferredSize());
    textInputField.setVisible(true);
    JFrame frame = new JFrame();
    frame.add(textInputField);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

Output of code with JDK 10 and 11:

https://i.stack.imgur.com/WiNqQ.png

Perminides
  • 41
  • 3
  • I can also verify the vertical alignment is different than JDK8 on Windows 10. However, I don't know how to fix the difference. – camickr Aug 22 '20 at 17:29
  • I fixed it for the moment with textInputField.setBorder(new EmptyBorder(8, 0, 0, 0)); But that is quite a dirty hack of course... – Perminides Aug 22 '20 at 17:33
  • 1
    I was going to suggest that, but agree is is a dirty hack. Is this a problem with all fonts? Maybe you can also check the FontMetrics on both versions to see if any properties might have changed from version to version. – camickr Aug 22 '20 at 17:41
  • 1
    I think the issue is with the font and not with the `JTextField`. Do you get the same behavior when you use the default font? – Abra Aug 22 '20 at 17:42
  • Take a look at [Calibri Font when in text moves to the bottom part of the component](https://stackoverflow.com/questions/61565649/calibri-font-when-in-html-text-moves-to-the-bottom-part-of-the-component). – George Z. Aug 22 '20 at 18:39
  • 1
    You are right, the font metrics changed for some fonts and especially for Calibry they changed a lot. There are already some bug reports about this, see e.g. here: https://bugs.openjdk.java.net/browse/JDK-8215290 but it does not look like this is going to be fixed. So either I go with the dirty hack described earlier or I just switch to a font where the metrics have not changed so badly. Thanks all. – Perminides Aug 22 '20 at 19:48

0 Answers0