Java Jdatechooser Foreground

927 Views Asked by At

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

thus

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.

as shown here

Any solution for this ?, preferably without having to disable the text box.

Thanks in advance!.

1

There are 1 best solutions below

3
Abra On

I propose the following solution.

Class JTextFieldDateEditor extends 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.

import com.toedter.calendar.IDateEditor;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class JdcTest0 implements Runnable {
    private JFrame  frame;
    private JDateChooser  jdate;

    @Override
    public void run() {
        showGui();
    }

    private JPanel createDateChooser() {
        JPanel panel = new JPanel();
        jdate = new JDateChooser();
        IDateEditor dateEditor = jdate.getDateEditor();
        if (dateEditor instanceof JTextFieldDateEditor) {
            JTextFieldDateEditor txtFld = (JTextFieldDateEditor) dateEditor;
            txtFld.setBackground(Color.BLACK);
            txtFld.addPropertyChangeListener("foreground", event -> {
                if (Color.BLACK.equals(event.getNewValue())) {
                    txtFld.setForeground(Color.WHITE);
                }
            });
        }
        panel.add(jdate);
        return panel;
    }

    private void showGui() {
        frame = new JFrame("JDC");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createDateChooser(), BorderLayout.PAGE_START);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new JdcTest0());
    }
}

Note that class JTextFieldDateEditor changes 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].