Why is my ItemListener program not allowing me to select an option from two different JComboBoxes in Java?

StackOverflow https://stackoverflow.com/questions/22795604

Вопрос

My program will only do one JcomboBox at a time when I am trying to get both numbers to be added together to display the final number. It allows me to run the program, but it will not display the final price because it does not want to select two things at once.

Here is the code for my program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CottageRental2 extends JFrame implements ItemListener{
  // Declare all instance data  (primitives and objects used) 
  private int WIDTH = 676;
  private int HEIGHT = 321; 
  Container con; 
  private JLabel label1;
  private JLabel label2;
  private JLabel label3;
  JComboBox  cbox1;
  JComboBox cbox2;
  String [ ] roomChoice = {"1 Bedroom: $600","2 Bedroom: $800","3 Bedroom: $1000"};
 String [ ] activityChoice = {"Horse Back Riding: $60","Rafting: $40","Row Boat Rental: $50"};
  ImageIcon icon1 = new ImageIcon("C:\\Users\\Coding\\Desktop\\cottage.jpeg");
  Font f1 = new Font("Ariel", Font.BOLD, 30);

//constructor 
  public CottageRental2(){
    super("Cottage Rental"); 
    con = getContentPane(); 
    con.setLayout(new BorderLayout()); 
    con.setBackground(Color.GRAY);
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
}

  public void createGUI(){
    label1 = new JLabel("Woodberry Cottage Rental", JLabel.CENTER);
    label1.setFont(f1);
    label1.setForeground(Color.WHITE);
    label2 = new JLabel("Rental Amount Due: $660", JLabel.CENTER);
    label2.setFont(f1);
    label2.setForeground(Color.WHITE);
    label3 = new JLabel(icon1);
    cbox1 = new JComboBox();
    cbox1.addItem(roomChoice[0]);
    cbox1.addItem(roomChoice[1]);
    cbox1.addItem(roomChoice[2]);
    cbox1.addItemListener(this);
    cbox2 = new JComboBox();
    cbox2.addItem(activityChoice[0]);
    cbox2.addItem(activityChoice[1]);
    cbox2.addItem(activityChoice[2]);
    cbox2.addItemListener(this);
    con.add(label1, BorderLayout.NORTH);
    con.add(label2, BorderLayout.SOUTH);
    con.add(label3, BorderLayout.CENTER);
    con.add(cbox1, BorderLayout.WEST);
    con.add(cbox2, BorderLayout.EAST);
  }

  public void itemStateChanged(ItemEvent event){
    Object source = event.getSource();
    int price1 = 0;
    int price2 = 0;
    if(source == cbox1){
      int roomIndex = cbox1.getSelectedIndex();
      if(roomIndex == 0){
        price1 = 600;
      }
      if(roomIndex == 1){
        price1 = 800;
      }
      if(roomIndex == 2){
        price1 = 1000;
      }
    }
    if(source == cbox2){
      int activityIndex = cbox2.getSelectedIndex();
      if(activityIndex == 0){
        price2 = 60;
      }
      if(activityIndex == 1){
        price2 = 40;
      }
      if(activityIndex == 2){
        price2 = 50;
      }
    }
    label2.setText("Rental Amount Due: $" + (price1 + price2));
  }

  public static void main(String[] args){
    CottageRental2 object = new CottageRental2(); 
    object.createGUI();
    object.setSize(675, 320);
  }
}
Это было полезно?

Решение

Your problem is your if (source == ...) if blocks which prevent the program from getting the selected item from the other JComboBox, the one that isn't being actively selected.

One solution: get rid of the offending if blocks that test for the source of the event in the listener:

public void itemStateChanged(ItemEvent event) {
  // Object source = event.getSource();
  int price1 = 0;
  int price2 = 0;
  // if(source == cbox1){
  int roomIndex = cbox1.getSelectedIndex();
  if (roomIndex == 0) {
     price1 = 600;
  } else if (roomIndex == 1) {
     price1 = 800;
  } else if (roomIndex == 2) {
     price1 = 1000;
  }
  // }
  // if(source == cbox2){
  int activityIndex = cbox2.getSelectedIndex();
  if (activityIndex == 0) {
     price2 = 60;
  } else if (activityIndex == 1) {
     price2 = 40;
  } else if (activityIndex == 2) {
     price2 = 50;
  }
  // }
  label2.setText("Rental Amount Due: $" + (price1 + price2));
}

Also, it would be safer if you use some else if blocks in your item selection statements.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top