0

Am kind of a beginner. I have an application in which I have a combo box. The combo box has two strings/items 1 being "Add" and 2nd being "Minus". If I select "Add" from the combo box my jText field should display the word "Addition" and if I select "Minus" from the combo box the jText field should display the word "Subtraction". My issue is it only displays "Addition" if I select add, but if I selects "Minus" it doesn't display "Subtraction".

Is there something wrong with my if statement??

    String display ="";

   if (comboBxOperator.getSelectedItem().equals("ADD"))
    {
        display = "Addition";
    }
  else  if (comboBxOperator.getSelectedItem().equals("Minus"))
    {
        display = "Subtraction";
    }
    txtDisplay.setText(display);

Update: After I implemented the acionListener for my combo Box I get"UnsupportedOperationException":

comboBxOperator.addActionListener(this);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
babaysteps
  • 393
  • 1
  • 5
  • 14

1 Answers1

3

You are not using an EventListener, there is no way your program can detect the changes if you don't tell it the selection has changed. Check out this tutorial.

Here is a simple generic example:

combo.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        doSomething();
    }
});
Community
  • 1
  • 1
0x6C38
  • 6,796
  • 4
  • 35
  • 47