简体   繁体   中英

Change JCalendar backgroundcolor

I am trying to change the JCalendar component background color from the component properties itself with no success. While changing the foreground color from the component proprieties is working fine.

I also tried menuCalendar.setBackground(new Color(135,239,251)) with no success.

Can anyone help?

Setting the background color for JCalendar is surprisingly complicated. There is one convenience method which allows to change part of the calendar's background. You could see if this does what you want first: menuCalendar.setDecorationBackgroundColor(new Color(135,239,251));

This probably won't do what you want. Changing the color of the background behind the day buttons and the colors of the buttons themselves can be changed with the following code. It has to be done this way as JCalendar uses native Swing components which are nested in one another and convenience methods seem to be lacking:

for (int i = 0; i < menuCalendar.getComponentCount(); i++)  {
    if (menuCalendar.getComponent(i) instanceof JDayChooser) {
        JDayChooser chooser = ((JDayChooser) menuCalendar.getComponent( i ) );
        JPanel panel = (JPanel) chooser.getComponent(0);
        // the following line changes the color of the background behind the buttons
        panel.setBackground(Color.BLACK);
        // the for loop below changes the color of the buttons themselves
        for (int y = 0; y < panel.getComponentCount(); y++) {
            panel.getComponent(y).setBackground(Color.BLACK);
        }
        break; // leave the for loop, we're done
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM