Hi, i am making an event calender but i'm stuck with making sure that a user choose a valid date (a user should'nt be able to choose a start date that is after the end date). Could anyone point me in the right direction please? My code is still a bit rough but i included it, if anyone want to have a better understanding.
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
public class TestarDat extends javax.swing.JFrame {
private JComboBox stDag;
private JComboBox stAr;
private JComboBox slDag;
private JComboBox slTim;
private JComboBox slAr;
private JComboBox slMan;
private JComboBox stTim;
private JComboBox stMan;
//private static String[] stDagar, stMånader, stÅr, slDagar, slMånader, slÅr;
private static int år, mån, dag;
private static DefaultComboBoxModel
stDagModel = new DefaultComboBoxModel(),
slDagModel= new DefaultComboBoxModel(),
stManModel= new DefaultComboBoxModel(),
slManModel= new DefaultComboBoxModel(),
stArModel= new DefaultComboBoxModel(),
slArModel= new DefaultComboBoxModel();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestarDat inst = new TestarDat();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public TestarDat() {
super();
setBackground(new Color(199,226,233));
GregorianCalendar cal = new GregorianCalendar();
år = cal.get(GregorianCalendar.YEAR);
mån = cal.get(GregorianCalendar.MONTH);
dag = cal.get(GregorianCalendar.DAY_OF_MONTH);
setDagar(år, mån, dag, år, mån , dag);
setMånader(år, mån, år , mån);
setÅr(år, år);
initGUI();
}
private void initGUI() {
try {
GridBagLayout thisLayout = new GridBagLayout();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.1};
thisLayout.rowHeights = new int[] {75, 42, 47, 7};
thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1};
thisLayout.columnWidths = new int[] {134, 140, 134, 7};
getContentPane().setLayout(thisLayout);
{
stDag = new JComboBox();
getContentPane().add(stDag, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
stDag.setModel(stDagModel);
}
{
stMan = new JComboBox();
getContentPane().add(stMan, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
stMan.setModel(stManModel);
stMan.addActionListener(new stArLys());
stMan.addActionListener(new stArLys());
}
{
stAr = new JComboBox();
getContentPane().add(stAr, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
stAr.setModel(stArModel);
stAr.addActionListener(new stArLys());
}
{
DefaultComboBoxModel stTimModel =
new DefaultComboBoxModel(
new String[] { "00:00", "01:00", "02:00" });
stTim = new JComboBox();
getContentPane().add(stTim, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
stTim.setModel(stTimModel);
}
{
slDag = new JComboBox();
getContentPane().add(slDag, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
slDag.setModel(slDagModel);
}
{
slMan = new JComboBox();
getContentPane().add(slMan, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
slMan.setModel(slManModel);
}
{
slAr = new JComboBox();
getContentPane().add(slAr, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
slAr.setModel(slArModel);
}
{
DefaultComboBoxModel slTimModel =
new DefaultComboBoxModel(
new String[] { "00:00", "01:00", "02:00" });
slTim = new JComboBox();
getContentPane().add(slTim, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
slTim.setModel(slTimModel);
}
pack();
this.setSize(579, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setDagar(int stye, int stmon, int stday, int slye, int slmon, int slday)
{
if((stDagModel.getSize() != 0) && (slDagModel.getSize() != 0))
{
stDagModel.removeAllElements();
slDagModel.removeAllElements();
}
GregorianCalendar stcal = new GregorianCalendar(stye, stmon, stday);
GregorianCalendar slcal = new GregorianCalendar(slye, slmon, 1);
int stdagsize = (stcal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) - stday) + 1;
int sldagsize = (slcal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) - slday) + 1;
if(stye == slye)
if(stmon == slmon)
for(int i = 0 ; i < stdagsize ; i++)
{
stDagModel.addElement(Integer.toString(stday + i));
slDagModel.addElement(Integer.toString(stday + i));
}
if(stmon != slmon)
{
for(int i = 0 ; i < stdagsize ; i++)
stDagModel.addElement(Integer.toString(stday + i));
for(int i = 0 ; i < sldagsize ; i++)
slDagModel.addElement(Integer.toString(stday + i));
}
}
public void setMånader(int stye, int stmon, int slye, int slmon)
{
stManModel.removeAllElements();
slManModel.removeAllElements();
GregorianCalendar stcal = new GregorianCalendar();
GregorianCalendar slcal = new GregorianCalendar(stye, stmon, 1);
int stmansize = (stcal.getActualMaximum(GregorianCalendar.MONTH) - mån) + 1;
int slmansize = (slcal.getActualMaximum(GregorianCalendar.MONTH) - mån) + 1;
if(stye == år)
for(int i = 0 ; i < stmansize ; i++)
stManModel.addElement(Integer.toString((stmon + i) +1));
if(stye > år)
for(int i = 0 ; i < 12 ; i++)
stManModel.addElement(Integer.toString(1 + i));
if(slye == stye)
for(int i = 0 ; i < slmansize ; i++)
slManModel.addElement(Integer.toString((slmon + i) +1));
else
for(int i = 0 ; i < slmansize ; i++)
slManModel.addElement(Integer.toString((stmon + i)) +1);
}
public void setÅr(int stye, int slye)
{
stArModel.removeAllElements();
slArModel.removeAllElements();
stArModel.addElement(Integer.toString(år));
stArModel.addElement(Integer.toString(år +1));
if(stye <= år)
{
stArModel.setSelectedItem(Integer.toString(år));
slArModel.addElement(Integer.toString(år));
slArModel.addElement(Integer.toString(år +1));
}
else if (stye > år)
{
stArModel.setSelectedItem(Integer.toString(år+1));
slArModel.addElement(Integer.toString(år+1));
}
else
{
slArModel.addElement(Integer.toString(stye));
stArModel.setSelectedItem(Integer.toString(år +1));
}
}
class stArLys implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int startÅr = Integer.parseInt((String) stAr.getSelectedItem());
int slutÅr = Integer.parseInt((String) slAr.getSelectedItem());
int startMån = Integer.parseInt((String) stMan.getSelectedItem());
int slutMån = Integer.parseInt((String) slMan.getSelectedItem());
setÅr(startÅr, slutÅr);
setMånader(startÅr, startMån, slutÅr, slutMån);
//setDagar(start, mån, dag, slut, mån, dag);
}
}
class stManLys implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int start = Integer.parseInt((String) stMan.getSelectedItem());
int slut = Integer.parseInt((String) slMan.getSelectedItem());
setÅr(start, slut);
setMånader(start, mån, slut, mån);
setDagar(start, mån, dag, slut, mån, dag);
}
}
}