4

I am trying to use JDatePicker to display a calendar. However, I only want to display months and years, not the days.

I tried poking around the model and JDatePickerImpl objects, without luck.

Here is the code I have to show the JDatePicker, from documentation:

   UtilCalendarModel model = new UtilCalendarModel();

    Properties p = new Properties();
    p.put("text.today", "Today");
    p.put("text.month", "Month");
    p.put("text.year", "Year");

    JDatePanelImpl datePanel = new JDatePanelImpl(model, p);

    JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateComponentFormatter());

Thanks in advance!

Note: Here is an image of what I mean.

enter image description here

Benoit Goderre
  • 527
  • 2
  • 9
  • 25

1 Answers1

4

After looking at the code from the JDatePicker project, I think this is possible by making a custom version of the JDatePanelImpl class (and perhaps some other classes). The functionality you want is not yet configurable in the standard classes, but it could be implemented as an enhancement and sent to the JDatePicker developers as a proposal (pull request).

Just to be sure of what you need for your application: you want to use a calendar similar to the example below? The user could change the selected month & year by clicking the next/previous month buttons, selecting a month from the month popup menu, or selecting a different year (using the year spinner):

Screenshot of possible month/year picker

Edit: added example with adapted versions of JDatePicker classes

I have added a modified example of your code and two adapted versions of JDatePicker classes. The normal component closes the popup when the user clicks a specific day, which is not possible in this case (since the days are hidden). I have added a small OK button to make it possible to close the date picker (see screenshot above). This is clearly a proof of concept only; the code really needs more work.

(Note: when I tried to add the two modified classes, my answer became to big. Therefore I forked the JDatePicker project on GitHub, rewrote the customizations from JDatePicker version 1.3.4 to version 1.3.4.1, and added links for these two files instead of all the code.)

// ExampleDatePickerWithoutDay class:

import java.text.*;
import javax.swing.*;
import org.jdatepicker.*;

public class ExampleDatePickerWithoutDay {
    public static void main(String[] arguments) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ExampleDatePickerWithoutDay().createAndShowGui();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();

        // Set two date formats and a text label.
        DateFormat dateFormat = new SimpleDateFormat("MMMM yyyy");
        ComponentFormatDefaults.Key formatKey;
        formatKey = ComponentFormatDefaults.Key.SELECTED_DATE_FIELD;
        ComponentFormatDefaults.getInstance().setFormat(formatKey, dateFormat);
        formatKey = ComponentFormatDefaults.Key.TODAY_SELECTOR;
        ComponentFormatDefaults.getInstance().setFormat(formatKey, dateFormat);
        ComponentTextDefaults.Key textKey = ComponentTextDefaults.Key.TODAY;
        ComponentTextDefaults.getInstance().setText(textKey, "Current month");

        // Create the date picker.
        UtilCalendarModel calendarModel = new UtilCalendarModel();
        CustomDatePanel datePanel = new CustomDatePanel(calendarModel);
        CustomDatePicker datePicker = new CustomDatePicker(datePanel);

        panel.add(datePicker);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }
}

CustomDatePanel class: CustomDatePanel.java (on GitHub)

CustomDatePicker class: CustomDatePicker.java (on GitHub)

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
  • Thank you Freek for your answer! What you show would work, although I think it would look better if all months of the year were in one window instead of having to scroll with the arrows. That being said, I don't know if it is possible. – Benoit Goderre Dec 18 '15 at 23:58
  • This option requires the least amount of changes to the existing classes. By hiding the table with the days of the month, you have a quick basic month and year picker that could easily be a configurable part of the JDatePicker code. Your idea probably offers better usability, but requires more modifications (by my estimate). I can finish and add my code, and you could decide to improve it or use the basic implementation. – Freek de Bruijn Dec 19 '15 at 00:09
  • If it is too much trouble, don't bother. I thought there might be an easy way, but it is ok if there is not :). – Benoit Goderre Dec 19 '15 at 00:11