Search code examples
arraysswingjframejbuttonjcombobox

Issue populating JComboBox with array


I've been working on this for a while but I can't seem to fix the one line that is giving me all the problems. This line "list = new JComboBox(measure);" underneath the array, seems to be giving me the error. I can suppress the error and it runs except the JComboBox does not work properly and gives the error "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to javax.swing.JButton at cube.actionPerformed(cube.java:132)"

Any help will be greatly appreciated!

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
//@SuppressWarnings("unchecked")
 public class cube extends JFrame implements ActionListener{
 public static JComboBox list;
 public static JTextField a;
 public static JTextField b;
 public static JTextField c;
 public static JTextField d;
 public static String measureFinal;
 public static String measurement;
 //public static String [] measurement = {"Inches" , "Meters", "Feet" , "Centimeters" , "Millimeters" , "Yards" };
 public static JFrame main = new JFrame("Volume of Cube");
 public static JPanel myPanel = new JPanel(new GridLayout (0,1));
 public static void main(String args[]){
     cube object = new cube();
    }
    cube(){
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myPanel.setPreferredSize(new Dimension(400,350));
    main.add(myPanel);
    a = new JTextField(3);
    b = new JTextField(3);
    c = new JTextField(3);
    d = new JTextField(3);
    JButton button1 = new JButton("Solve!");
    JButton button2 = new JButton("Back to shape selector");
    myPanel.add(new JLabel ("Select the unit of measurement"));

    String measure[] = {"Inches" , "Meters", "Feet" , "Centimeters" , "Millimeters" , "Yards" };
    list = new JComboBox(measure);
    list.setSelectedIndex(0);
    list.addActionListener(this);
    myPanel.add(list);
    myPanel.add(new JLabel ("Enter the height of the box"));
    myPanel.add(a);
    myPanel.add(new JLabel ("Enter the width of the box"));
    myPanel.add(b);
    myPanel.add(new JLabel ("Enter the length of the box"));
    myPanel.add(c);

    myPanel.add(button1);
    button1.addActionListener(this);
    myPanel.add(button2);
    button2.addActionListener(this);
    main.pack();
    main.setLocation(600,200);
    main.setVisible(true);




    //tring [] measurement = {"Inches" , "Meters", "Feet"};

    //double volume = height * width * length;
    //String answer = "The volume of the box is " +volume+ measurement;

}
public void CBSelect(ActionEvent e){
  int temp = 0;
  temp = list.getSelectedIndex();
  //Object temp = e.getSource();

  /*
  if (e.getSource() == list){
      temp = list.getSelectedIndex();
      switch(temp){
          case 0:
          measurement = "Inches";
          break;
          case 1:
          measurement = "Meters";
          break;
          case 2:
          measurement = "Feet";
          break;
          case 3:
          measurement = "Centimeters";
          break;
          case 4:
          measurement = "Millimeters";
          break;
          case 5:
          measurement = "Yards";
          break;

        }
    } 
    */
  if (temp == 0){
      measurement = "Inches";
    } else if (temp == 1){
      measurement = "Meters";
    }else if (temp == 2){
      measurement = "Feet";
    }else if (temp == 3){
      measurement = "Centimeters";
    }else if (temp == 4){
      measurement = "Millimeters";
    }else if (temp == 5){
      measurement = "Yards";
    }
}
  public void actionPerformed(ActionEvent e) {
  String actionCommand = ((JButton) e.getSource()).getActionCommand();
  //JComboBox cb = ((JComboBox) e.getSource());
  //measureFinal = (String)cb.getSelectedItem();
  if (actionCommand == "Solve!"){
    double height = Double.parseDouble(a.getText());
    double width = Double.parseDouble(b.getText());
    double length = Double.parseDouble(c.getText());
    double volume = height * width * length;
     try{
    final ImageIcon icon = new ImageIcon(new URL("http://wiki.fantasticcontraption.com/w/images/0/06/Solved.png"));
    JOptionPane.showMessageDialog(this, ("The volume of the box is " +volume+" "+ measurement),"Volume", JOptionPane.PLAIN_MESSAGE, icon);
   } catch (MalformedURLException ex){
    System.out.println("Image Not Found!");
   }
    }
  if (actionCommand == "Back to shape selector"){
      main.dispose();
      main.setVisible(false);
    }
}

}

UPDATE: Looking back at it I'm now thinking the problem lies within the CBSelect Action Listener.


Solution

  • The thing is, since you do:

    list.addActionListener(this);
    // some other stuff...
    button1.addActionListener(this);
    button2.addActionListener(this);
    

    Then source of the events passed to your method cube#actionPerformed can be list, button1 or button2.

    So, you have you check the type of the source before casting it to anything and access its properties in the body of your actionPerformed method. You could do it like this:

    final Object source = e.getSource();
    String actionCommand = null;
    if(source instanceof JButton) {
        actionCommand = ((JButton) e.getSource()).getActionCommand();
    } else if(source instanceof JComboBox<?>) {
        actionCommand = ((JComboBox) e.getSource()).getSelectedItem().toString();
    }
    

    Cheers!