public class AddPlanePanel extends JPanel implements MyObserver
    {
        private JLabel planeLabel = new JLabel("New Plane ");
        private JLabel nameForPlane = new JLabel("Name:");
        public JTextField inputPlaneName = new JTextField(15);
        private JButton button = new JButton("Add");
        private JLabel status = new JLabel();
        private JLabel typeLabel = new JLabel("Type:");        
        private String[] typeStrings = {"Antonov", "Cessna"};
        private JComboBox typeList = new JComboBox(typeStrings);
        private Planes planes;


        public AddPlanePanel(Planes planes)
        {
            this.planes = planes;    
            planes.attach(this);
            setup();
            build();
        }

        private void setup()
        {
            setBorder(BorderFactory.createLineBorder(Color.blue));
            planeLabel.setForeground(Color.magenta);
            button.addActionListener(new Listener());
            typeList.setEditable(true);
            typeList.addActionListener(new Listener());

        }

        private void build()
        {   
            add(planeLabel);
            add(nameForPlane);
            add(inputPlaneName);
            add(typeLabel);
            add(typeList);
            add(button);
            add(status);
        }
        public String name()
        {
            return inputPlaneName.getText();
        }
        public void update()
        {
            status.setText(name() + " added");
        }

        private class Listener implements ActionListener
        {    
            public void actionPerformed(ActionEvent e)
            {         
                int type = Integer.parseInt((String)typeList.getSelectedItem());
                planes.add(name(), type);
            }
        }
    }

this is my code to add plane, i have problem at the JCombobox string array which is i need to convert string to int, so i can add the type to my plane.add method. specifically, the problem is in the actionPerformed that i cant change the value of the type which is string to int.

Thanks all.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You wrote this:

private String[] typeStrings = {"Antonov", "Cessna"};
private JComboBox typeList = new JComboBox(typeStrings);

How is it possible to change this "Antonov" or this "Cessna" into an int ?

this code is in the model folder. to adding the type to plane.add method, i have to change the type to int, since in the package folder my type value is int.

honestly, i dont know much about java.. hate it a lot..

this code is in the model folder. to adding the type to plane.add method, i have to change the type to int, since in the package folder my type value is int.

honestly, i dont know much about java.. hate it a lot..

What you said, doesn't make much sense. But the answer is the same. In the select box you have these value and you get these value:
"Antonov, Cessna". Those are not numbers. Maybe you need to assign to each one of them an id number. When you get the selected item see which one it is and save that.

integer.parseInt()

Completely wrong and read the previous posts. He is already using that method. The problem is that he is passing as argument Strings that are not numbers:
"Antonov, Cessna"

Integer.parseInt(var);
var is the string...
make sure the var is a number, error will occur if not...

i think the best way to run ur code is refering to every element in your array by there
index rather that their actual name, i hope this helps

Hi
This topic have no thing to do with the convertion; the error is as mentionned by javaAddict's post and ou did notwant to use getSelectedSelection because it return a string value, and the data in you model is of type string.
You maybe need to call getSelectedIndex that return an int value. in that case you do not need to make any cast.

int type = typeList.getSelectedIndex();

Instead of

int type = Integer.parseInt((String)typeList.getSelectedItem());

Hope it helps.

This article has been dead for over six months. Start a new discussion instead.