Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 298
Ivan Avatar asked Oct 22 '22 22:10

Ivan


1 Answers

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();
            }
        });
    }
}
like image 138
mKorbel Avatar answered Oct 25 '22 12:10

mKorbel