0

I know I'm missing something very simplem but for the life of me I can't see it. All I want to do is get "Paris" from the combo box, and when the button is pressed, show that "Paris" is selected.

public class assignment2try2 implements ActionListener {
    private JComboBox HolidayLocation;  
    private JComboBox HolidayDuration;
    private JButton PriceCheck; 

    public static void main(String[] args) {        
        JLabel Location = new JLabel(" Where do you want to go ? ");

        String[] HolidayLocations = {" ","Paris", "Crete", "Croatia"};
        JComboBox<String> LocationBox = new JComboBox<String>(HolidayLocations);
        LocationBox.setEditable(false);
        LocationBox.setPreferredSize(new Dimension( 160, 20 ));
        //LocationBox.setSelectedIndex(4);
        LocationBox.addActionListener(LocationBox);

        JButton PriceCheck = new JButton("Check Availability");
        PriceCheck.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("button works");
                //if(LocationBox.getSelectedItem().equals(HolidayLocations))
                {
                    //System.out.println("paris selected");
                }
            }
        });
    }
}
Martin M J
  • 830
  • 1
  • 5
  • 12
  • Why are you checking if `LocationBox.getSelectedItem().equals(HolidayLocations)`? You are checking if the selected item matches the *whole* array. – Martin M J May 02 '15 at 23:06
  • what should i do for its just to pull Paris from the comboBox ? – UpAllNight123 May 02 '15 at 23:07
  • You should pull the selected item and display it. – Martin M J May 02 '15 at 23:09
  • how would i achive this ? – UpAllNight123 May 02 '15 at 23:09
  • I'll try to come up with an answer in a second. – Martin M J May 02 '15 at 23:10
  • `what should i do for its just to pull Paris from the comboBox ?` read the section from the Swing tutorial on [How to Use Combo Boxes](http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html). It has a working example on how to use an ActionListener. Also, follow Java naming conventions. Variable names should NOT start with an upper case character. The tutorial also shows a better way to structure your code. `LocationBox.addActionListener(LocationBox);` - not sure what that code is for. You don't add the combo box as an ActionListener. – camickr May 02 '15 at 23:10
  • `"Paris".equals(locationBox.getSelectedItem())`?? – MadProgrammer May 02 '15 at 23:10
  • But why would you check what the input is, instead of displaying it regardless of what the selected item is? – Martin M J May 02 '15 at 23:11
  • i tried your way mad programmer and it asked me to make my variable locationBox to a final String – UpAllNight123 May 02 '15 at 23:15
  • Read this: http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – Martin M J May 02 '15 at 23:16

1 Answers1

0

EDIT: I just now noticed that your class implements ActionListener. With the below solution, you can remove the implements-statement from your code.

To fix your issues with the String having to be final, make a private class:

private class MyListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println(locationBox.getSelectedItem() + " selected.");
    }
}

Then, replace

PriceCheck.addActionListener(new ActionListener() { ... });

with

PriceCheck.addActionListener(new MyListener());

This should be able to print out the selected value after the button is pressed.

Note: I changed your variable name from LocationBox to locationBox to comply with naming conventions.

Martin M J
  • 830
  • 1
  • 5
  • 12