Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Which are non projected Teaching aids in advance learning system?

Answered: 1 week ago