good day
I am trying to change the color of the box text (jtextfile) that brings the built-in jdatechooser
I am developing an application where the white background of the jdatechooser does not match at all, I have changed the background color of both the jtextfile and the button of the jdatechooser with the following code:
for( Component c : jdate.getComponents()){
((JComponent)c).setBackground(new Color(20,25,34));
}
My current problem is that the background color that I need is very dark, and the date text is black, currently in the image there is a selected date, you just don't see anything.
I have tried to change the text to white without success.
try the same method without getting the solution
for( Component text : jdate.getComponents()){
((JComponent)text).setForeground(new Color(255,255,255));
}
I also tried removing the code that I put at the beginning for the background thinking that this could be preventing the change of the text color but it did not work either.
Try these other ways to get the change:
JTextFieldDateEditor dateChooserEditor = ((JTextFieldDateEditor)jdate.getDateEditor());
dateChooserEditor.setForeground(new Color(255, 255, 255));
Of course the easy way didn't work either:
jdate.setForeground(Color.WHITE);
The only way I have managed to change the color of the text is to disable the text field and leave the button enabled to choose the date for the button and it cannot be written in the text box.
jdate.getDateEditor().setEnabled(false);
((JTextFieldDateEditor) jdate.getDateEditor ())
.setDisabledTextColor(Color.WHITE);
The problem with this, in addition to not being allowed to write the date manually, I also lose the desired background color.
Any solution for this ?, preferably without having to disable the text box.
Thanks in advance!.
I propose the following solution.
Class
JTextFieldDateEditorextends JFormattedTextField. Hence the foreground color is a bound property which means that you can listen for changes to it. Therefore you can add a PropertyChangeListener. If the new foreground color is black, simply change it to white.Here is a small application that demonstrates.
Note that class
JTextFieldDateEditorchanges its foreground color to red if the date is invalid and to green if the date is valid. If you want to handle those colors as well, then the red color is java.awt.Color#red, i.e. [255, 0, 0] and the green is a custom color [0, 150, 0].