views:

1555

answers:

3

How does one set the color of text in a Java Swing textbox at run-time? At startup, the color is grayish and when the user enters the textbox, I wish to change the color to the normal text color. I am currently using the following code:

private void txtScheduleInfoFocusGained(java.awt.event.FocusEvent evt)                                            
    {                                                
        try
        {
            if (currentClassIsNewClass() && txtScheduleInfo.getDocument().getText(0, txtScheduleInfo.getDocument().getLength()).equals(PASTE_SI_HERE))
            {
                txtScheduleInfo.setText("");
                txtScheduleInfo.setForeground(java.awt.SystemColor.textText);
            }
        }
        catch (BadLocationException ex)
        {
            JOptionPane.showMessageDialog(this, "BLE\nContact Zian", "Unexpected Problem", JOptionPane.ERROR_MESSAGE);
        }
    }

At this time, when the code runs, the text still shows up in gray.

Additional Code:
Declaration (as a field):

   private javax.swing.JTextPane txtScheduleInfo;

Instantiation:

txtScheduleInfo = new javax.swing.JTextPane();

Initialization:

txtScheduleInfo.setForeground(java.awt.SystemColor.textInactiveText);
txtScheduleInfo.setText("Paste schedule information here");
txtScheduleInfo.addFocusListener(new java.awt.event.FocusAdapter() {
    public void focusGained(java.awt.event.FocusEvent evt) {
        txtScheduleInfoFocusGained(evt);
    }
    public void focusLost(java.awt.event.FocusEvent evt) {
        txtScheduleInfoFocusLost(evt);
    }
});
+2  A: 

Did you make sure the JTextBox is enabled? You can call setEnabled(true) on it to make sure. Not trying to be rude, that's just the most likely cause (there's code in Swing to force graying-out of disabled components).

If that doesn't fix it, you can also trigger a repaint by calling txtScheduleInfo.repaint(), which might cause it to repaint.

If neither of these things helps, you could post some code so we can see what's going on.

BobMcGee
Unfortunately, neither of those solutions fixed the problem. I've posted additional code.
Zian Choy
A: 

Doesn't Swing normally perform this behavior (changing color when the textbox gains focus for editing)? Try disabling all your color changing code and see if it works normally. If you're willing to post your code in a compilable form on PasteBin, others can actually do full debugging too.

Other Things I can suggest:

  • Check that java.awt.SystemColor.textText is really the color you want (use methods on it to get the hex color and then display it in a color picker)
  • Remove the line txtScheduleInfo.setForeground(java.awt.SystemColor.textInactiveText); as it may somehow be overriding the default painting if your focus handler is broken.
  • Replace
    if (currentClassIsNewClass() && txtScheduleInfo.getDocument().getText(0, txtScheduleInfo.getDocument().getLength()).equals(PASTE_SI_HERE))
    WITH if(true)

Your focus event listener may never be triggering to change colors due to a condition in that if statement. Besides, you know that focus has been gained anyway when that method is called.

BobMcGee
+1  A: 

try this instead

private void txtScheduleInfoFocusGained(java.awt.event.FocusEvent evt)                                            
    {                                                
        try
        {
            if (currentClassIsNewClass() && txtScheduleInfo.getDocument().getText(0, txtScheduleInfo.getDocument().getLength()).equals(PASTE_SI_HERE))
            {
                txtScheduleInfo.setForeground(java.awt.SystemColor.textText);
                txtScheduleInfo.setText("");
            }
        }
        catch (BadLocationException ex)
        {
            JOptionPane.showMessageDialog(this, "BLE\nContact Zian", "Unexpected Problem", JOptionPane.ERROR_MESSAGE);
        }
    }

(The only change is swapping the order. Now you're setting the foreground colour before clearing the text.)

Catchwa