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
- A JComboBox with selections: Single and Multiple
- A JList with countries inside of it (an array of Strings). You don't have to use the same countries that I did.
- A JLabel that shows what is currently selected
Interactions
- When Single is selected then the JList changes so only one item can be selected.
- When Multiple is selected, the JList changes so multiple items can be selected
- 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.
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started