Reputation: 373
import java.awt.EventQueue;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ColorUIResource;
import javax.swing.text.Document;
import javax.swing.text.NumberFormatter;
public class Test extends JFrame{
private JFormattedTextField input, input2;
private NumberFormatter formatter;
public Test() {
formatter = new NumberFormatter(NumberFormat.getNumberInstance());
input = new JFormattedTextField(formatter);
input2 = new JFormattedTextField(formatter);
input.setColumns(4);
input2.setColumns(4);
input.setValue(0.0);
JPanel panel = new JPanel();
panel.add(input);
panel.add(input2);
add(panel);
pack();
setVisible(true);
}
public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIManager.put("nimbusBase", new ColorUIResource(0, 0, 0));
UIManager.put("FormattedTextField.background", Color.RED);
UIManager.put("control", new ColorUIResource(153, 76, 0));
UIManager.put("textForeground", new ColorUIResource(255, 153, 51));
break;
} }
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
EventQueue.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}
Simply put I'm attempting to change the background color of an Enabled
JFormattedTextField
from the default white to my RGB color. I used the table found here (Link in this blog) to find the appropriate name.
I realize the blog is a little bit outdated (6 years) and Nimbus
has been updated a ton since then, so this might be my issue.
How do I go about using UIManager
to change the background color of a JFormattedTextField
?
I updated the code above, it works as it should now. The issue was using ColorUIResource
instead of just Color
.
Upvotes: 1
Views: 583
Reputation: 14413
How do I go about using UIManager to change the background color of a JFormattedTextField?
You have to set this property FormattedTextField.background
as found here Nimbus Defaults
Something like this
UIManager.put("FormattedTextField.background", Color.RED);
Upvotes: 3