GUI – Refinements – ComboBoxes

  1. Zeller Pascal
  2. Zeller Java
  3. validDate Revisited – Python
  4. validDate Revisited – Java
  5. Java GUI
  6. GUI – Refinements – ComboBoxes
  7. GUI – Refinements – Error Messages
  8. GUI Refinements – Focus
  9. GUI Refinements – Today

The first refinement i wish to make is to make the inputs for Day and Month to be ComboBoxes. In order to do this I first need to create two arrays containing all the values for the days(0 – 31) and months(“—” to “December”). The arrays will both store these values as strings and then later I will use the index of the value as to be used for the calculation. The code for the arrays is below:

private String[] Days = {"---","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",
                             "29","30","31"};
private String[] Months = {"---","January","February","March","April",
                             "May","June","July","August","September",
                             "October","November","December"};

I then need to replace the JTextFields for Day and Month and create the JComboBox elements:

private JComboBox comboBoxDay = new JComboBox(Days);
private JComboBox comboBoxMonth = new JComboBox(Months);

I have had to tweak the layout slightly increasing the width to 266 pixels to accommodate the down arrows and the fact that the months are now the month names and not just their numbers. I have also had to change the positions of some of the elements they are displayed in the GUI the new table is below, the changes are in red:

x-posy-posWidthHeight
labelDay116020
labelMonth63110020
labelYear165110020
comboBoxDay1226020
comboBoxMonth632210020
textFieldYear1652210020
buttonCalculate16226340
labelOutput110426340

The next thing i need to do is change how the inputs are gathered, this is simply by getting the selected index of the ComboBoxes, as below:

int inputDay = comboBoxDay.getSelectedIndex();
int inputMonth = comboBoxMonth.getSelectedIndex();

The GUI is now:

This is exactly what i was looking for, onto the next refinement.