codice:
import javax.swing.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JSpinnerDemo extends JFrame implements ActionListener {
private JSpinner date;
private JTextArea ta;
private JButton getDate;
SimpleDateFormat sdf;
public void actionPerformed (ActionEvent ae) {
Date d = (Date)date.getValue();
String formatted = sdf.format(d);
ta.append(formatted+"\n");
}
public JSpinnerDemo() {
super("JSpinner Demo");
this.setSize(400,300);
this.getContentPane().setLayout(new BorderLayout());
sdf = new SimpleDateFormat("dd/MM/yyyy");
date = new JSpinner(new SpinnerDateModel());
date.setEditor(new JSpinner.DateEditor(date, "dd-MM-yyyy"));
JPanel north = new JPanel();
north.setLayout(new GridLayout(1,2));
ta = new JTextArea();
getDate = new JButton("Data");
getDate.addActionListener(this);
north.add(date);
north.add(getDate);
this.getContentPane().add(north, BorderLayout.NORTH);
this.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main (String[] args) {
JSpinnerDemo dm = new JSpinnerDemo();
}
}
Buon weekend