Set text color in Java textbox

How can I set the color of text in a Java Swing textbox at runtime? On startup, the color is gray, and when the user enters the textbox, I want 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 is still grayed out.

Additional code:
Declaration (as 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);
    }
});

      

0


a source to share


3 answers


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 to replace the order. You now set the foreground color before clearing the text.)

+1


a source


Are you sure the JTextBox is enabled? You can call setEnabled(true)

to be sure. Don't try to be rude, this is the most likely reason (there is code in Swing to force the cutout of disabled components).

If this doesn't fix it, you can also cause the repaint by calling txtScheduleInfo.repaint (), which can cause it to repaint.



If none of these things help, you can post some code so we can see what's going on.

+2


a source


Does Swing not work this behavior (changing color when the textbox receives focus for editing)? Try disabling all color changing code and see if it works fine. If you're willing to publish your code in compiled form to PasteBin , others can actually do full debugging.

Other things I can suggest:

  • Make sure java.awt.SystemColor.textText is actually the color you want (use the methods for that to get the hex color and then display it in the picker).
  • Remove the line txtScheduleInfo.setForeground(java.awt.SystemColor.textInactiveText);

    as it might somehow override the default if your focus handler is broken.
  • Replace
    if (currentClassIsNewClass() && txtScheduleInfo.getDocument().getText(0, txtScheduleInfo.getDocument().getLength()).equals(PASTE_SI_HERE))


    C if(true)

Your focus event listener can never trigger a color change due to a condition in an if statement. Also, you know that focus was obtained anyway this method is called.

0


a source







All Articles