3

I noticed a difference in the appearance of a TitledBorder between Java 8 (and earlier) and Java 9 (and later) on Windows with native look-and-feel. Starting with Java 9, the border is darker and doesn't have round corners. Especially with nested TitledBorder, this looks disturbing. Is there a way to use Java 9 and have the border painted like in Java 8?

Java 8 vs Java 9

MWE:

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

public class TitledBorderWithJava9 {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                //...
            }
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(150, 100);
            JPanel panel = new JPanel();
            frame.add(panel);
            
            TitledBorder border = BorderFactory.createTitledBorder("Title");
            panel.setBorder(border);
            
            frame.setVisible(true); 
        });
    }
}
user7291698
  • 1,972
  • 2
  • 15
  • 30
  • 3
    Since the look and feel is a class, you could get the Java 8 one and copy it. This might work and if that doesn't, you could try to change the UI properties via `UIManager.pu("property", "value")` is the java version is greater than 8. – Frederic Perron Jul 26 '21 at 16:02
  • 1
    You can specify whatever base border you want to `createTitledBorder​` to get your desired look (I won’t discuss taste). The actual question is, why does the system look and feel use a (soft) bevel border when the actual Window’s look uses a single color line border. – Holger Jul 27 '21 at 18:59
  • Thank you both for your helpful comments! @ Frédéric Perron: This is unfortunately not as straightforward as it seems, because the class refers to many relevant classes that are not public API, which leads to further issues. @Holger: I tried that approach and came close to the desired result, see my answer below. – user7291698 Jul 28 '21 at 15:33

1 Answers1

0

Based on the comments to the question, the following approach comes close to the desired result (colors and dimensions are correct, but I'didn't manage to have round corners the same way as in Java 8).

Border baseBorderOuter = BorderFactory.createLineBorder(new Color(213, 223, 229), 1, true);
Border baseBorderInner = BorderFactory.createLineBorder(Color.WHITE, 1, true);
Border baseBorder = BorderFactory.createCompoundBorder(baseBorderOuter, baseBorderInner);

TitledBorder border = BorderFactory.createTitledBorder(baseBorder, "Title");
panel.setBorder(border);
user7291698
  • 1,972
  • 2
  • 15
  • 30
  • 1
    Did you try [`BorderFactory.createLoweredSoftBevelBorder()`](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/javax/swing/BorderFactory.html#createLoweredSoftBevelBorder())? There’s also a version allowing to set colors explicitly… – Holger Jul 28 '21 at 18:09
  • Yes I tried that, also the method with 4 color parameters and also the etched border. But if you look at the exact pixels, it is still not equal. One problem is the radius of the round corners, the other is that the bevel borders have an inside and an outside color, but in `WindowsLookAndFeel`, on the bottom border, the inside and outside colors are reversed (i.e. the outside color is inside at the bottom, while it is outside at left/up/right). – user7291698 Jul 28 '21 at 19:33