Search code examples
javaswingubuntulook-and-feeluimanager

JTextField with JLabel style using GTKLookAndFeel


I need a JTextField with a JLabel style (in other words, I need a JLabel but I want to be able to select the text).

JTextField value = new JTextField(valueText);
value.setEditable(false);
value.setBorder(BorderFactory.createEmptyBorder);
value.setForeground(UIManager.getColor("Label.foreground"));               
value.setFont(UIManager.getFont("Label.font"));
value.setBackground(UIManager.getBackground("Label.background"));
...

It works fine on Windows and Mac:

JTextField like a JLabel (Mac)

but on Ubuntu (GTKLookAndFeel) I get the next result:

JTextField like a JLabel (Ubuntu)

Where "By" is the JTextField and "Size" is a JLabel


Solution

  • Finally I got the expected result using JEditorPane instead of JTextField:

    JEditorPane value = new JEditorPane();
    value.setText(valueText);
    ...  
    

    It seems that GTK Look and Feel does not allow a custom background in the JTextField: https://bugs.java.com/bugdatabase/view_bug?bug_id=6531760