Im having a difficult time finding info on how to set up a combobox listener for my project. My book has 1 example and it uses audio files.(no help) My project consists of a GUI panel with a splitpane that that has a list of info on the left for a city (restaurants, hotels, etc.) the right diplays a .gif of the city. I want to learn how to write the listener code so that when a city is chosen from the dropdown, it will call my constructor for that particular city and the appropriate city.gif. Thanks very much.

String[] cityNames = { "Population: " + population,
                                        "\n\n",
                              "Climate: " + climate,
                                        "\n\n",
                              "Attractions: " + attractions,
                                        "\n\n",
                              "Popular restaurants: " + restaurants,
                                        "\n\n",
                              "Hotels/Motels: " + hotels,
                                        "\n\n",
                              "Lakes/Waterways: " + water,
                                        "\n\n",
                              "Founded: " + founded,
                                        "\n\n"};

String[] cities = {"", "New York City", "Washington D.C.", "Orlando", "Nashville", "Dallas",
                                  "Aspen", "Las Vegas", "Chicago", "Hollywood", "Quit"};

        list = new JList (cityNames);
        add (list);
        JPanel boxPanel = new JPanel();
        boxPanel.setPreferredSize (new Dimension (150, 60));                          
        destination = new JLabel("Choose a city to view");
        boxPanel.add(destination);
        Destinationbox = new JComboBox (cities);
        add (boxPanel);
        Destinationbox.addActionListener(new CityListener());
        boxPanel.add(Destinationbox);   
   }
   private class CityListener implements ActionListener
   {
      public void actionPerformed (ActionEvent event)
      {

      }

Recommended Answers

All 2 Replies

Instead of an ActionListener you need a ListSelectionListener, which will be called whenever the user selects a city in the list.
list.addListSelectionListener(new MyListSelectionListener())
Check out the API JavaDoc for details of the ListSelectionListener interface and the ListSelectionEvent that it uses.

thanks, i'll check that out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.