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

[529 byte] By [BaltimoreJohna] at [2007-10-2 12:24:31]
# 1

> However, in some cases I am getting a background of white when it is disabled.

How are we supposed to guess what these cases are?

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

camickra at 2007-7-13 9:16:46 > top of Java-index,Desktop,Core GUI APIs...
# 2

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;

}

}

BaltimoreJohna at 2007-7-13 9:16:46 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Is this because

> setEditable doesn't do anything if the field is

> already editable?

>

Yes. If the editable property changes, the textfield fires a PropertyChangeEvent that notifies the UI delegate which in turn updates the background accordingly. But calling setEditable with the current value of the editable property is a no-op. No events are fired, and no one knows it happened (except for you :-)

So the second time you press the button you set the background to the original background color (which happens to be white). But the following call to setEditable is a no-op, so the white background remains.

To illustrate this further, change your ActionListener like so:

jTextField.setBackground(origBackColor);

jTextField.setEditable(true);

jTextField.setEditable(false);

happy_hippoa at 2007-7-13 9:16:46 > top of Java-index,Desktop,Core GUI APIs...
# 4
Coolo! That will do the trick.Thanks.
BaltimoreJohna at 2007-7-13 9:16:47 > top of Java-index,Desktop,Core GUI APIs...
# 5
You are welcome.
happy_hippoa at 2007-7-13 9:16:47 > top of Java-index,Desktop,Core GUI APIs...