JTextComponent - setEditable background color
I have a subclass of JTextField. I am doing manipulations of the background color (red when validation failed). When the program sets the text field to be not editable I want the default disabled background color (transparent?) to be displayed.
However, in some cases I am getting a background of white when it is disabled.
What is setEditable doing to change the background from white to grey? If I have the background color set to RED how do I change it so that grey will be shown?
Thanks,
John
I am trying to understand what setEditable does WRT the background color / transpanency. In the below code, when you press the button the first time, the background of the textfield becomes grey. If you press it again it becomes white. Is this because setEditable doesn't do anything if the field is already editable?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class BackgroundTester extends JFrame {
private static final long serialVersionUID = 1;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JTextField jTextField = null;
private JButton jButton = null;
private Color origBackColor = null;
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 0;
gridBagConstraints2.gridy = 2;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
gridBagConstraints1.gridy = 1;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.gridx = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(getJTextField(), gridBagConstraints1);
jPanel.add(getJButton(), gridBagConstraints2);
}
return jPanel;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setColumns(10);
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Set Disabled");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextField.setBackground(origBackColor);
jTextField.setEditable(false);
}
});
}
return jButton;
}
///////////////////////////////////////////////////////////////////
/**
* @param args
*/
///////////////////////////////////////////////////////////////////
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BackgroundTester thisClass = new BackgroundTester();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public BackgroundTester() {
super();
initialize();
origBackColor = jTextField.getBackground();
jTextField.setBackground(Color.RED);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
}