简体   繁体   中英

Show only months and years with JDatePicker

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.

在此输入图像描述

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):

可能的月/年选择器的屏幕截图

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)

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