Manipulating ComboBox items

206 Views Asked by At

I am trying to create a code that calculates the student's GPA using two comboBox that holds grades and units. grade

String [] grade = {"A", "B", "C", "D", "E", "F"};

unit

String[] unit = {"1", "2", "3", "4", "5", "6"};

For each course there will be a corresponding comboBox comboGrade and comboUnit components holding the values of the array grade and unit respectively. Given that there will be 10 courses, the comboBox grade and unit will be 10 each. I managed this using two ArrayList ArrayList<JComboBox> courseGrade; and ArrayList<JComboBox> courseUnit; to hold the 10 comboBoxes using the following code:

//grade
    String[] grades = {"A","B","C","D","E","F"};
        courseGrade = new ArrayList<>();
        for (int i=0;i<10;i++){
            JComboBox comboGrade = new JComboBox(grade);
        courseGrade.add(comboUnit); //adds the comboBox to the ArrayList
    }

    //unit
    String[] unit = {"1","2","3","4","5","6"};
        courseUnit = new ArrayList<>();
        for (int i=0;i<10;i++){
            JComboBox comboUnit = new JComboBox(unit);
        courseUnit.add(comboUnit); //adds the comboBox to the ArrayList
    }

I got the selected item using this ItemListener:

public void itemStateChanged(ItemEvent evt){
    String item;
    if (evt.getStateChange()==ItemEvent.SELECTED){
        item = (String) evt.getItem();

for each of the comboBox and then added it to a new ArrayList ArrayList<Integer> gradeSelected;

switch (item) {
                        case "A":
                            gradeSelected.add(A);
                            break;
                        case "B":
                            gradeSelected.add(B);
                            break;
                        case "C":
                            gradeSelected.add(C);
                            break;
                        case "D":
                            gradeSelected.add(D);
                            break;
                        case "E":
                            gradeSelected.add(E);
                            break;
                        case "F":
                            gradeSelected.add(F);
                            break;
                    }

where A, B, C, D, E, F are integer constants representing 5,4,3,2,1,0 respectively

Now here is my problem....

What if the user DESELECTS the SELECTED item and then selects a new item from the comboBox ?

//I thought of `ItemEvent.DESLECTED`;

How do i respond to such event? given that once the user has already selected an item it has been added to the ArrayList "gradeSelected". How do i remove the already added item and replace it with the current selection in exactly the same

index as the previous selection?

Perhaps am approaching this the wrong way.

1

There are 1 best solutions below

1
On

I agree with Ironluca, don't register the user's input until he is done making all of his selections and is ready to submit his selections, and so thus I think that the best solution is to allow your GUI to be changeable and to have none of the inputs be binding until the user performs an action that causes the input to be submitted. This could be on the press of a submit button, or on trying to advance to a next view of your GUI or on trying to close a dialog window. It doesn't matter so much what this step is, so long as it is easily recognizable for the user. At this point your data should be validated, and the program should proceed to the next step based on whether the inputted data has been deemed to be valid and complete or not.

Otherwise if you wanted to be able to remove items from a collection on deselection, you could always use a Stack for your collection such as a LinkedList, and then push and pop items on the stack as they are added or removed.

Also, rather than a switch statement, consider consolidating your grade information, the letter and the number, into an enum. Something like:

public enum Grade {
   A("A", 5), B("B", 4), C("C", 3), D("D", 2), E("E", 1), F("F", 0);

   private String text;
   private int value;

   private Grade(String text, int value) {
      this.text = text;
      this.value = value;
   }

   public String getText() {
      return text;
   }

   public int getValue() {
      return value;
   }

   @Override
   public String toString() {
      return text;
   }
}

Then it could be used easily:

  JComboBox<Grade> gradeCombo = new JComboBox<>(Grade.values());
  gradeCombo.setSelectedIndex(-1);
  JOptionPane.showMessageDialog(null, gradeCombo);

  Grade grade = (Grade) gradeCombo.getSelectedItem();
  if (grade != null) {
     System.out.printf("Grade is %s with a value of %d%n", grade, grade.getValue());
  }