Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change JTextField enabled background color

i've got a question about JTextField background color. How can I change it in enabled text field (while editing) ? setBackground works only for disabled text field. UIManager.put can change this background for all of my text field in window but i want to do that only for one of them.

like image 749
clsbartek Avatar asked Nov 04 '25 19:11

clsbartek


1 Answers

There's a number of ways you might achieve this, but the basic idea is, when the field gets focus, you want to set the fields background color to something else and when it loses focus, you want to reset it...

FocusChange

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FocusedField {

    public static void main(String[] args) {
        new FocusedField();
    }

    public FocusedField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextField field1 = new JTextField(20);
                JTextField field2 = new JTextField(20);

                FocusListener highlighter = new FocusListener() {

                    @Override
                    public void focusGained(FocusEvent e) {
                        e.getComponent().setBackground(Color.GREEN);
                    }

                    @Override
                    public void focusLost(FocusEvent e) {
                        e.getComponent().setBackground(UIManager.getColor("TextField.background"));
                    }
                };

                field1.addFocusListener(highlighter);
                field2.addFocusListener(highlighter);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(4, 4, 4, 4);
                gbc.gridwidth = gbc.REMAINDER;
                frame.add(field1, gbc);
                frame.add(field2, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

I would be tempted to write a simple singleton "manager" which allowed you to register and unregister fields as you needed.

You might also be able to achieve something similar by attaching a PropertyChangeListener to the KeyboardFocusManager, this would allow you to basic apply this highlighting concept to all fields within in any program without the need to change any of the code, but that would depend on your requirements

like image 191
MadProgrammer Avatar answered Nov 06 '25 11:11

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!