Change JTextField enabled background color

后端 未结 4 1327
执笔经年
执笔经年 2021-01-03 10:37

i\'ve got a question about JTextField background color. How can I change it in enabled text field (while editing) ? setBackground works only for di

相关标签:
4条回答
  • 2021-01-03 10:47

    Ok, there is what i needed:

    Properties props = new Properties(); props.put("showFocusFrame", "off");     
    ((AbstractLookAndFeel)UIManager.getLookAndFeel()).getTheme().setProperties(prop‌​s);
    
    0 讨论(0)
  • 2021-01-03 10:52

    Just add an ActionListener on your textField, and then set the Background in the Listener.

    0 讨论(0)
  • 2021-01-03 10:54

    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

    0 讨论(0)
  • 2021-01-03 11:06

    I think it works with textField.setForeground(Color.RED) :)

    0 讨论(0)
提交回复
热议问题