Example usage for javax.swing SpinnerDateModel setValue

List of usage examples for javax.swing SpinnerDateModel setValue

Introduction

In this page you can find the example usage for javax.swing SpinnerDateModel setValue.

Prototype

public void setValue(Object value) 

Source Link

Document

Sets the current Date for this sequence.

Usage

From source file:Main.java

public void makeUI() {
    String[] zones = { "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Calcutta", "Europe/Paris", "Europe/London",
            "America/New_York", "America/Los_Angeles" };
    JLabel[] labels = new JLabel[zones.length];
    SimpleDateFormat[] formats = new SimpleDateFormat[zones.length];
    JFrame frame = new JFrame();

    Calendar cal = Calendar.getInstance();
    Date date = cal.getTime();/*from  ww  w. ja va2 s  .  com*/
    SpinnerDateModel model = new SpinnerDateModel();
    model.setValue(date);
    JSpinner spinner = new JSpinner(model);
    spinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            Date date = (Date) ((JSpinner) e.getSource()).getValue();
            for (int i = 0; i < labels.length; i++) {
                labels[i].setText(formats[i].format(date));
            }
        }
    });
    SimpleDateFormat format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat();
    format.setTimeZone(TimeZone.getTimeZone(zones[0]));
    format.applyPattern("yyyy-MM-dd HH:mm:ss");
    JPanel panel = new JPanel(new GridLayout(zones.length, 2, 10, 10));
    for (int i = 0; i < zones.length; i++) {
        formats[i] = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
        formats[i].setTimeZone(TimeZone.getTimeZone(zones[i]));
        JLabel label = new JLabel(zones[i]);
        labels[i] = new JLabel(formats[i].format(date));
        panel.add(label);
        panel.add(labels[i]);
    }
    frame.setLayout(new BorderLayout());
    frame.add(spinner, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}