underscore_
underscore_

Reputation: 87

Java: getSelectedItem() is undefined for type string

I am having a problem with my java program, i am trying to get some information for an if statement using JComboBox and a JButton. The problem is that the .getSelectedItem() is undefined and i do not know what to do about this. These are the combo boxes:

static String JCBDestinations, JCBNights, JCBAccomodation;
static String[] places, nights, stay;

//Destination drop down menu
    String[] JCBDestinations = { " ", "Paris", "Crete", "Croatia"};
    JComboBox places = new JComboBox(JCBDestinations);
    places.setSelectedIndex(4);
    places.addActionListener(this);

//Number of nights radio buttons
    String[] JCBNights = { " ", "7", "10", "14"};
    JComboBox nights = new JComboBox(JCBNights);
    nights.setSelectedIndex(4);
    nights.addActionListener(this);

//Accommodation type drop down menu
    String[] JCBAccomodation = {" ", "Hotel", "Villa", "Youth Hostel", "Bed & Breakfast"};
    JComboBox stay = new JComboBox(JCBAccomodation);
    stay.setSelectedIndex(4);
    stay.addActionListener(this);

//Find deal button
    JBFind = new JButton("Find Deal"); //Adding option 1 button
    window.add(JBFind);
    JBFind.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {

        }
    });

This is the If statement:

public void actionPerformed(ActionEvent e) 
{

    if (e.getSource() == JBFind);
    {
        System.out.println("Calculating cost");

        JLBeforeVAT.setText("£499");
        JTAfterVAT.setText("£589");
    }   
            if (JCBDestinations.getSelectedItem().equals("Paris"))
            {
                if (JCBNights.getSelectedItem().equals("7"))
                {
                    if (JCBAccomodation.getSelectedItem().equals("Hotel"))
                    {
                        JLBeforeVAT.setText("£499");
                        JTAfterVAT.setText("£589");
                    }

                }

            }
}

Upvotes: 1

Views: 687

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

The error message is telling you exactly what you're doing wrong, and so key here is to read it carefully, and fix what it is showing you is in error. You get the selection from the JComboBox, not from a String array. The combo box variable is named places. So

if (places.getSelectedItem().equalsIgnoreCase("Paris"))

Other problem:

String[] JCBNights = { " ", "7", "10", "14"};
JComboBox nights = new JComboBox(JCBNights);
nights.setSelectedIndex(4);  // *****
nights.addActionListener(this);

You're setting the selected index to 4 when it only goes up to 3: 0, 1, 2, 3.

Upvotes: 2

Related Questions