codice:
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class prova{
private final String[] MESI = {"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"};
private final String[] ANNI = {"2004", "2005", "2006", "2007", "2008", "2009", "2010"};
private String[] GIORNI = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"};
private JComboBox giorni, mesi, anni;
public static void main(String[] args){
new prova();
}
public prova(){
giorni = new JComboBox(GIORNI);
mesi = new JComboBox(MESI);
anni = new JComboBox(ANNI);
mesi.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
String press = e.getItem().toString();
if(press.equals(MESI[2])){
giorni.removeItem("31");
giorni.removeItem("30");
giorni.removeItem("29");
}
else
if(press.equals(MESI[1]) || press.equals(MESI[3]) || press.equals(MESI[5]) || press.equals(MESI[7]) || press.equals(MESI[8]) || press.equals(MESI[10]) || press.equals(MESI[12])) {
giorni.removeItem("31");
giorni.removeItem("30");
giorni.removeItem("29");
giorni.insertItemAt("29", 29);
giorni.insertItemAt("30", 30);
giorni.insertItemAt("31", 31);
}
else
{
giorni.removeItem("31");
giorni.removeItem("30");
giorni.removeItem("29");
giorni.insertItemAt("29", 29);
giorni.insertItemAt("30", 30);
}
}
});
JPanel panel = new JPanel();
panel.add(giorni);
panel.add(mesi);
panel.add(anni);
JFrame jf = new JFrame();
jf.setContentPane(panel);
jf.pack();
jf.setVisible(true);
}
}
E manca anche il controllo per l'anno bisesto!