Question: Java Program Build a GUI that has the following A JComboBox with selections: Single and Multiple A JList with countries inside of it (an array

Java Program

Build a GUI that has the following

  1. A JComboBox with selections: Single and Multiple
  2. A JList with countries inside of it (an array of Strings). You don't have to use the same countries that I did.
  3. A JLabel that shows what is currently selected

Interactions

  1. When Single is selected then the JList changes so only one item can be selected.
  2. When Multiple is selected, the JList changes so multiple items can be selected
  3. When a country, or multiple countries, is selected the JLabel changes to reflect the new selections

Advice

Look at slides 37, 38 and 39 (GUI Access Example) to see how you can access other GUI items from within a listener.

The Lecture this week doesn't go through all of the examples. Look at the reading for this week for great examples on setting up JList and JComboBox.

Build an ActionListener for the JComboBox

  • When it changes, change the JList's selection mode

Build a ListSelectionListener for the Jlist

  • When the event occurs, call getSelectedIndices and getModel.getElementAt() to build a string listing everything selected
  • Set the JLabel to the string you built
  • Call .pack() on your base frame to resize it automatically

To try selecting multiple items in the list, hold down control or shift. By default the list will allow you to select multiple items.

Java Program Build a GUI that has the following A JComboBox with

My Layout

If you want the same layout I used, it was a combination of a Panel with FlowLayout and BorderLayout:

JComponent panel = new JPanel();

panel.setLayout(new FlowLayout());

panel.add(new JLabel("Choose Selection Mode:"));

panel.add(jcb);

this.add(panel,BorderLayout.NORTH);

this.add(jlst, BorderLayout.WEST);

this.add(jlbl,BorderLayout.SOUTH);

Choose Selection Mode: Single Bahamas Costa Rica Canada Cuba Hait Jamaica Mexico United States of America Selected items: Haiti

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!