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);
}
});