Mi sono visto il framework, e ho provato a fare qualche esempio in base alla tua esigenza:
codice:
/**
* @(#)JCalendarExample.java
*
*
* @author Vincenzo
* @version 1.00 2012/4/30
*/
import com.toedter.calendar.*;
import javax.swing.*;
import java.awt.Component;
import java.awt.event.*;
import java.util.*;
public class JCalendarExample extends JFrame {
private final JCalendar calendar;
public JCalendarExample() {
super("JCalendar");
this.calendar = new JCalendar();
JDayChooser dc = this.calendar.getDayChooser();
JPanel dcp = dc.getDayPanel();
for(Component c : dcp.getComponents()) {
if( c instanceof JButton ) {
JButton button = (JButton)c;
if( !button.getText().equals("") && Character.isDigit(button.getText().charAt(0)) )
button.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
Calendar c = new GregorianCalendar();
c.set(Calendar.DATE, Integer.parseInt(((JButton)e.getSource()).getText()) );
c.set(Calendar.MONTH, calendar.getMonthChooser().getMonth() );
c.set(Calendar.YEAR, calendar.getYearChooser().getYear() );
System.out.println("Data selezionata:" + c.getTime() );
}
}
);
}
}
this.add( this.calendar );
this.setDefaultCloseOperation( super.EXIT_ON_CLOSE );
this.setResizable(true);
this.pack();
this.validate();
this.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
SwingUtilities.invokeAndWait( new Runnable(){
@Override
public void run() {
new JCalendarExample();
}
} );
}
}
In sostanza, associa ad ogni pulsante nel JDayChooser l'ActionListener da me definito che, quindi consente di ottenere la data selezionata alla pressione di un pulsante.
Invece, se vuoi fare una cosa meno elaborata ma ottenendo più o meno lo stesso risultato, puoi utilizzare il JDateChooser chiedendo esplicitamente all'utente quando ha selezionato la data:
codice:
/**
* @(#)JDateChooserExample.java
*
*
* @author Vincenzo
* @version 1.00 2012/4/30
*/
import com.toedter.calendar.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JDateChooserExample extends JFrame{
private final JDateChooser dc;
public JDateChooserExample() {
super("JDateChooser");
this.dc = new JDateChooser();
JButton button = new JButton("Show");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.out.println( dc.getDate() );
}
}
);
this.add(dc, BorderLayout.NORTH);
this.add(button, BorderLayout.SOUTH);
this.setDefaultCloseOperation( super.EXIT_ON_CLOSE );
this.setResizable(true);
this.pack();
this.validate();
this.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
SwingUtilities.invokeAndWait( new Runnable(){
@Override
public void run() {
new JDateChooserExample();
}
} );
}
}