-1

My humble goal is to have a date-picker in java swing that does not have hard-coded colors in it. A date-picker that delegates it's appearance to the chosen LAF.

Sob...

I am using com.toedter.calendar.JDateChooser version 1.4

With this answer i was able to get rid of green and red and black of the jtextfield.

Java Jdatechooser Foreground

With this, i was able to remove other annoying colors:

myDC.getJCalendar().setWeekdayForeground(null);
myDC.getJCalendar().setDecorationBackgroundColor(null);
myDC.getJCalendar().setSundayForeground(null);

Now i am stuck with the year spinner and it's unwanted colors. See:

enter image description here

enter image description here

Any clue on how to get rid of those?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
BabaNew
  • 884
  • 1
  • 13
  • 27
  • 1
    If I remember correctly, `JDateChooser` requires that you use `Date` and/or `Calendar`, the old date-time classes that were supplanted by java.time soon to be 10 years ago. So I would start by finding a date picker that supports java.time. There are some. And then consider how I can get the L&F that I want. – Ole V.V. Feb 11 '23 at 18:39
  • I am sorry if the LAF part of the question misled you. My goal is to have a date picker that does not enforce hardcoded colors, like this green and red you can see in the pictures. My question covers all colors of JDateChooser but the year spinner. Is there anybody that can fix the spinner? – BabaNew Feb 11 '23 at 19:07

1 Answers1

1

The effect illustrated is a feature of the CaretListener implemented by JTextFieldDateEditor and the JYearChooser parent class, JSpinField. Omit the addition of the listener to eliminate the effect; in outline:

public JTextFieldDateEditor(…) {
    …
    //addCaretListener(this);
    …
}
public JSpinField(…) {
    …
    //textField.addCaretListener(this);
    …
}

Less radically, it is also possible to invoke removeCaretListener() on the editor.

Calendar c = Calendar.getInstance();
JDateChooser jdc = new JDateChooser(c.getTime());
JTextFieldDateEditor editor = (JTextFieldDateEditor) jdc.getDateEditor();
editor.removeCaretListener(editor);

Also override the caretUpdate() method inherited by JYearChooser to condition the effect based on a suitable boolean value.

JYearChooser jyc = new JYearChooser() {
    boolean update;

    @Override
    public void caretUpdate(CaretEvent e) {
        if (update) {
            super.caretUpdate(e);
        }
    }
};
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • My Problem is with JYearChooser. I already got rid of other colors. There is no removeCaretListener() method in JYearChooser class, nor in parent JSpinField. It's also impossible to override the JspinField setForeGround method, as it is not possible to set a JYearChooser instance to a JDateChooser instance – BabaNew Mar 14 '23 at 10:06
  • Right, `removeCaretListener()` is a `JTextComponent` method. Does omitting the call to `addCaretListener()` do what you want? – trashgod Mar 14 '23 at 11:35
  • How would you comment out that line of the compiled jar? Also, trying to extend JSpinField to alter it's behaviour it's useless since you cannot set the modified JSpinField class to a JDateChooser object – BabaNew Mar 14 '23 at 14:08
  • I don't know that you can; I wanted to verify that this is the desired behavior. I suggested alternatives above. – trashgod Mar 14 '23 at 18:06
  • Can you edit your answer to show how to set the jyc variable to jdc variable? – BabaNew Mar 15 '23 at 09:19
  • As `Date::getYear` is deprecated, I'd probably use the alternative suggested in the API or use `java.time`. How are you doing it? – trashgod Mar 15 '23 at 11:52
  • You provided a wrong and misleading answer. Edit your answer to remove color from the jyearchooser of a jdatechooser instance or just remove the answer – BabaNew Mar 15 '23 at 12:55
  • Ah, I see—you want to alter the `JYearChooser` instance held by the `JCalendar` that gets shown by the `JDateChooser` button listener. As my approach works in isolation, I'll leave the answer for now. – trashgod Mar 15 '23 at 22:24
  • Ok, but... aside from your isolation approach, that does not solve the problem, can you think of something else to remove the color from the JYearChooser of a JDateChooser instance? Or is it just not possible? – BabaNew Mar 16 '23 at 10:20
  • It's possible—instantiate `JDateChooser` with a custom `JCalendar` that uses the `JYearChooser` shown above. – trashgod Mar 16 '23 at 12:19
  • Sadly, you'd still need to access the display panel reflectively. Why not just fork and modify the source? – trashgod Mar 17 '23 at 18:28
  • Thanks for the followup you gave me so far. I haven't had the time to test your approach yet. I will try to test it as soon as i can to give you my opinion – BabaNew Mar 18 '23 at 11:34
  • I tried to move some things around but yeah... given these classes hierarchy and these classes constructors it is impossible to alter this bad hardcoded logic inside JSpinField. Modifying the source appears to be the last option... but , at least for my, it's not really worth... probably better to re-implement it from scratch, and adding a couple of improvements that it needs anyway. Shame that such a component wasn't developed by oracle in the first place – BabaNew Mar 20 '23 at 17:30