5

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

Ivan
  • 1,477
  • 3
  • 18
  • 36
  • Out of interest, which versions of Ubuntu/Java are you running? I've just run this code my Ubuntu 12.04 machine with OpenJDK 1.6.0_24, and it works as expected; it looks just like a label. – Jamie May 17 '12 at 12:26
  • @Jamie Ubuntu 10.04 and java 1.6.0.22 . Have you set UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) ? – Ivan May 17 '12 at 12:35
  • Ah, that does break it... Excuse me, I thought it would use the GTK LookAndFeel by default, but apparently not! – Jamie May 17 '12 at 12:40

2 Answers2

2

I think that setBorder(null); to reset default Borders in/came from UIMananager for GTK L&F, or to set this value to the UIMananager directly for JTextField

EDIT:

you have to change BorderUIResource, the same way as demonstrated here for FontUIResource

EDIT2:

enter image description here and at 1,5 sec enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.plaf.BorderUIResource;

public class SystemFontDisplayer extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
    private javax.swing.Timer timer = null;
    private JTextField testTextField = new JTextField("testTextField");
    private JButton testButton = new JButton("testButton");

    public SystemFontDisplayer() {
        frame.setLayout(new GridLayout(4, 0, 20, 20));
        testTextField.setBorder(null);
        frame.add(testTextField);
        frame.add(testButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(200, 105);
        frame.pack();
        frame.setVisible(true);
        start();
    }

    private void start() {
        timer = new javax.swing.Timer(1500, updateCol());
        timer.setRepeats(false);
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                Border border = new LineBorder(Color.red, 2);
                final BorderUIResource res = new BorderUIResource(border);
                UIManager.getLookAndFeelDefaults().put("TextField.border", new BorderUIResource(border));
                UIManager.getLookAndFeelDefaults().put("Button.border", new BorderUIResource(border));
                SwingUtilities.updateComponentTreeUI(frame);

                /*Border border = new LineBorder(Color.red, 2);
                try {
                LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
                final BorderUIResource res = new BorderUIResource(border);
                UIDefaults uiDefaults = lnf.getDefaults();
                uiDefaults.put("TextField.border", res);
                uiDefaults.put("Label.border", res);
                uiDefaults.put("Button.border", res);
                UIManager.getLookAndFeel().uninitialize();
                UIManager.setLookAndFeel(lnf);
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }
                UIDefaults defaults = UIManager.getDefaults();
                BorderUIResource res = new BorderUIResource(border);
                defaults.put("TextField.border", res);
                defaults.put("Label.border", res);
                defaults.put("Button.border", res);
                SwingUtilities.updateComponentTreeUI(frame);*/
            }
        };
    }

    public static void main(String arg[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I already tried setBorder(null) with the same results. I don't understand the second solution. – Ivan May 17 '12 at 11:59
  • please to run [UIManager Defaults](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/), select GTK and search rellated values for JTextField – mKorbel May 17 '12 at 12:14
  • I tried it using UIManager.getLookAndFeelDefaults().put("TextField.border", new BorderUIResource(new myBorder()); But it doesn't work. – Ivan May 17 '12 at 12:56
  • Yes I know, but if you add UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel") in your code it doesn't work on the same way. – Ivan May 17 '12 at 13:49
  • @ Ivan I still assumed that UIManager and BasicXxxUI work for all Native OS the same things with the same way, I don't believe that setBorder doesn't works, nor with put the value to UIManager – mKorbel May 17 '12 at 17:00
  • You run your code with GTKLookAndFeel and it doesn't work as expected (the new border is not red). Anyway I think that the main problem is the background. – Ivan May 18 '12 at 07:58
  • I can't to run GTK nor virtual session with Ubuntu 10.04, I still assumed that setBorders setBackground or rellated settings in the UIManager works, otherwise you have to report this issue to the BugParade – mKorbel May 18 '12 at 08:22
2

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: http://bugs.sun.com/view_bug.do?bug_id=6531760

Ivan
  • 1,477
  • 3
  • 18
  • 36