Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that meets the following requirements: Write an application

ID: 3764328 • Letter: W

Question

Write a Java program that meets the following requirements:


Write an application that allows a user to select a country from a list box that contains at least seven options. After the user makes a selection, display the country’s capital city. Save the file as JCapitals.java.


Purpose:
Create a java program from detailed directions.
Ability to:
Follow established programming conventions for writing java programs
Use inheritance
Use a FlowLayout
Use the JFrame class
To create arrays
Use Label, Text Field, and Loops
To create a ComboBox
Add an event

Explanation / Answer

Answer :

import java.util.*;
import javax.swing.*;
import javax.swing.event.*;


public class JListExample extends JFrame {

private JList<String> countryList;

public JListExample() {
  
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement("USA");
listModel.addElement("India");
listModel.addElement("Vietnam");
listModel.addElement("Canada");
listModel.addElement("Denmark");
listModel.addElement("France");
listModel.addElement("Japan");

  
countryList = new JList<>(listModel);
countryList.addListSelectionListener(new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
List<String> selectedValuesList = countryList.getSelectedValuesList();
// System.out.println(selectedValuesList);
       Iterator<String> CrunchifyIterator =selectedValuesList.iterator();
       while (CrunchifyIterator.hasNext()) {
           String selectedValues=CrunchifyIterator.next();
           if(selectedValues.equals("USA"))
           System.out.println("Washington");
           if(selectedValues.equals("India"))
           System.out.println("New Delhi");
           if(selectedValues.equals("Vietnam"))
           System.out.println("Hanoi");
           if(selectedValues.equals("Canada"))
           System.out.println("Ottawa");
           if(selectedValues.equals("Denmark"))
           System.out.println("Copenhagen");
           if(selectedValues.equals("France"))
           System.out.println("Paris");
           if(selectedValues.equals("Japan"))
           System.out.println("Tokyo");
       }
      
      
      
}
}
});

add(new JScrollPane(countryList));

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JList Example");
this.setSize(200, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
  
public void run() {
new JListExample();
}
});
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote