Example usage for javax.swing JSpinner.DateEditor JSpinner.DateEditor

List of usage examples for javax.swing JSpinner.DateEditor JSpinner.DateEditor

Introduction

In this page you can find the example usage for javax.swing JSpinner.DateEditor JSpinner.DateEditor.

Prototype

private DateEditor(JSpinner spinner, DateFormat format) 

Source Link

Document

Construct a JSpinner editor that supports displaying and editing the value of a SpinnerDateModel with a JFormattedTextField.

Usage

From source file:components.SpinnerDemo.java

public SpinnerDemo(boolean cycleMonths) {
    super(new SpringLayout());

    String[] labels = { "Month: ", "Year: ", "Another Date: " };
    int numPairs = labels.length;
    Calendar calendar = Calendar.getInstance();
    JFormattedTextField ftf = null;

    //Add the first label-spinner pair.
    String[] monthStrings = getMonthStrings(); //get month names
    SpinnerListModel monthModel = null;
    if (cycleMonths) { //use custom model
        monthModel = new CyclingSpinnerListModel(monthStrings);
    } else { //use standard model
        monthModel = new SpinnerListModel(monthStrings);
    }/* w w w  . java  2 s  .  c om*/
    JSpinner spinner = addLabeledSpinner(this, labels[0], monthModel);
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setColumns(8); //specify more width than we need
        ftf.setHorizontalAlignment(JTextField.RIGHT);
    }

    //Add second label-spinner pair.
    int currentYear = calendar.get(Calendar.YEAR);
    SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
            currentYear - 100, //min
            currentYear + 100, //max
            1); //step
    //If we're cycling, hook this model up to the month model.
    if (monthModel instanceof CyclingSpinnerListModel) {
        ((CyclingSpinnerListModel) monthModel).setLinkedModel(yearModel);
    }
    spinner = addLabeledSpinner(this, labels[1], yearModel);
    //Make the year be formatted without a thousands separator.
    spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));

    //Add the third label-spinner pair.
    Date initDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -100);
    Date earliestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, 200);
    Date latestDate = calendar.getTime();
    SpinnerModel dateModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.YEAR);//ignored for user input
    spinner = addLabeledSpinner(this, labels[2], dateModel);
    spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy"));

    //Lay out the panel.
    SpringUtilities.makeCompactGrid(this, numPairs, 2, //rows, cols
            10, 10, //initX, initY
            6, 10); //xPad, yPad
}

From source file:components.SpinnerDemo3.java

public SpinnerDemo3(boolean cycleMonths) {
    super(new SpringLayout());

    String[] labels = { "Month: ", "Year: ", "Another Date: " };
    int numPairs = labels.length;
    calendar = Calendar.getInstance();
    JFormattedTextField ftf = null;

    //Add the first label-spinner pair.
    String[] monthStrings = getMonthStrings(); //get month names
    SpinnerListModel monthModel = null;
    if (cycleMonths) { //use custom model
        monthModel = new CyclingSpinnerListModel(monthStrings);
    } else { //use standard model
        monthModel = new SpinnerListModel(monthStrings);
    }/*from  w w  w .ja v  a2 s  . com*/
    JSpinner spinner = addLabeledSpinner(this, labels[0], monthModel);
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setColumns(8); //specify more width than we need
        ftf.setHorizontalAlignment(JTextField.RIGHT);
    }

    //Add second label-spinner pair.
    int currentYear = calendar.get(Calendar.YEAR);
    SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
            currentYear - 100, //min
            currentYear + 100, //max
            1); //step
    //If we're cycling, hook this model up to the month model.
    if (monthModel instanceof CyclingSpinnerListModel) {
        ((CyclingSpinnerListModel) monthModel).setLinkedModel(yearModel);
    }
    spinner = addLabeledSpinner(this, labels[1], yearModel);
    //Make the year be formatted without a thousands separator.
    spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));

    //Add the third label-spinner pair.
    Date initDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -100);
    Date earliestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, 200);
    Date latestDate = calendar.getTime();
    SpinnerDateModel dateModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.YEAR);//ignored for user input
    dateSpinner = spinner = addLabeledSpinner(this, labels[2], dateModel);
    spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy"));
    //Tweak the spinner's formatted text field.
    ftf = getTextField(spinner);
    if (ftf != null) {
        ftf.setHorizontalAlignment(JTextField.RIGHT);
        ftf.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 3));
    }
    spinner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    //XXX: No easy way to get to the buttons and change their border.
    setSeasonalColor(dateModel.getDate()); //initialize color

    //Listen for changes on the date spinner.
    dateSpinner.addChangeListener(this);

    //Lay out the panel.
    SpringUtilities.makeCompactGrid(this, numPairs, 2, //rows, cols
            10, 10, //initX, initY
            6, 10); //xPad, yPad
}

From source file:io.heming.accountbook.ui.MainFrame.java

private void initToolBar() {
    toolbar = new JToolBar("ToolBar", JToolBar.HORIZONTAL);

    // Add Button
    addButton = new JButton();
    addButton.setActionCommand("");
    addButton.setToolTipText("");
    addButton.setIcon(new ImageIcon(getClass().getResource("edit-add-3.png")));

    // Search date range radio buttons
    ButtonGroup group = new ButtonGroup();
    yearToggleButton = new JToggleButton();
    yearToggleButton.setToolTipText("");
    yearToggleButton.setIcon(new ImageIcon(getClass().getResource("year-s.png")));
    group.add(yearToggleButton);/*from  ww w  .  j  av  a  2  s . c  o m*/
    monthToggleButton = new JToggleButton();
    monthToggleButton.setToolTipText("");
    monthToggleButton.setIcon(new ImageIcon(getClass().getResource("month-s.png")));
    group.add(monthToggleButton);
    dayToggleButton = new JToggleButton();
    dayToggleButton.setToolTipText("");
    dayToggleButton.setIcon(new ImageIcon(getClass().getResource("day-s.png")));
    group.add(dayToggleButton);
    customToggleButton = new JToggleButton();
    customToggleButton.setToolTipText("");
    customToggleButton.setIcon(new ImageIcon(getClass().getResource("all-s.png")));
    group.add(customToggleButton);

    // ??Checkbox
    monthToggleButton.setSelected(true);

    Calendar earliestCalendar = Calendar.getInstance();
    earliestCalendar.add(Calendar.YEAR, -100);
    java.util.Date earliestDate = earliestCalendar.getTime();

    Calendar latestCalendar = Calendar.getInstance();
    latestCalendar.add(Calendar.YEAR, 100);
    java.util.Date latestDate = latestCalendar.getTime();
    // 3?
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    java.util.Date initDate = calendar.getTime();

    SpinnerDateModel startDateModel = new SpinnerDateModel(DateUtil.getStartOfWeek(), earliestDate, latestDate,
            Calendar.MONTH);
    startDateSpinner = new JSpinner(startDateModel);
    JSpinner.DateEditor startDateEditor = new JSpinner.DateEditor(startDateSpinner, "yyyy-MM-dd");
    startDateSpinner.setEditor(startDateEditor);

    calendar.add(Calendar.MONTH, 1);
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    initDate = calendar.getTime();
    SpinnerDateModel endDateModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.MONTH);
    endDateSpinner = new JSpinner(endDateModel);
    JSpinner.DateEditor endDateEditor = new JSpinner.DateEditor(endDateSpinner, "yyyy-MM-dd");
    endDateSpinner.setEditor(endDateEditor);

    // Keyword text field
    keywordTextField = new JTextField();
    keywordTextField.setPreferredSize(new Dimension(250, 20));
    keywordTextField.setMaximumSize(keywordTextField.getPreferredSize());

    // Search Button
    searchButton = new JButton();
    searchButton.setActionCommand("?");
    searchButton.setToolTipText("?");
    searchButton.setIcon(new ImageIcon(getClass().getResource("edit-find-5.png")));

    // Layout
    cards = new JPanel();
    cards.setLayout(new CardLayout());
    Box box = Box.createHorizontalBox();
    box.add(Box.createHorizontalGlue());

    startDateSpinner.setPreferredSize(new Dimension(90, 20));
    startDateSpinner.setMaximumSize(startDateSpinner.getPreferredSize());
    box.add(startDateSpinner);

    box.add(Box.createHorizontalStrut(6));

    endDateSpinner.setPreferredSize(new Dimension(90, 20));
    endDateSpinner.setMaximumSize(endDateSpinner.getPreferredSize());
    box.add(endDateSpinner);

    cards.add(new JPanel(), HIDE_DATE_CHOOSER);
    cards.add(box, SHOW_DATE_CHOOSER);

    Box dateRangeBox = Box.createHorizontalBox();
    dateRangeBox.add(Box.createHorizontalGlue());
    dateRangeBox.add(cards);

    toolbar.add(addButton);
    toolbar.add(dateRangeBox);
    toolbar.add(customToggleButton);
    toolbar.add(yearToggleButton);
    toolbar.add(monthToggleButton);
    toolbar.add(dayToggleButton);
    toolbar.add(keywordTextField);
    toolbar.add(searchButton);

    add(toolbar, BorderLayout.NORTH);

    // Event listener
    addButton.addActionListener(e -> showAddRecordDialog());

    ActionListener listener = e -> searchRecords();
    yearToggleButton.addActionListener(listener);
    monthToggleButton.addActionListener(listener);
    dayToggleButton.addActionListener(listener);
    customToggleButton.addActionListener(listener);
    keywordTextField.addActionListener(listener);
    searchButton.addActionListener(listener);

    customToggleButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isRightMouseButton(e)) {
                chooseDateRange();
            }
        }
    });
}