Reputation: 1990
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?
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);
});
}
}
Upvotes: 3
Views: 375
Reputation: 1990
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);
Upvotes: 1