Question

I'm doing a homework assignment using HashMap with GUI. The user will enter a Country, and the Capital of that country, and press Add, this will add those two Strings to the map, and it should add the Capital to the list on the right. The search function is next. The user will be able to type the name of the country, and it will highlight that countries capital in blue on the right among the other capitals that were added. Here is what it should look like.

enter image description here

How can I copy the contents of my hashMap to my JList? I only need the Capitals to display so I can search them.

Any help is appreciated, thank you.

P.S. Sorry for the generated code, the variable declarations are at the bottom because of it.

import java.util.ArrayList;
import java.util.HashMap;

public class MapPanel extends javax.swing.JPanel {

HashMap<String, String> hashMap=new HashMap<String,String>();
ArrayList<String> array = new ArrayList<String>();

/**
 * Creates new form MapPanel
 */
public MapPanel() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    searchPanel = new javax.swing.JPanel();
    countrySearch = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jButton2 = new javax.swing.JButton();
    inputPanel = new javax.swing.JPanel();
    countryInput = new javax.swing.JTextField();
    capitalInput = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    listPanel = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    capitalList = new javax.swing.JList();

    searchPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    jLabel3.setText("Search Country");

    jButton2.setText("Search");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout searchPanelLayout = new javax.swing.GroupLayout(searchPanel);
    searchPanel.setLayout(searchPanelLayout);
    searchPanelLayout.setHorizontalGroup(
        searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(searchPanelLayout.createSequentialGroup()
            .addGroup(searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(searchPanelLayout.createSequentialGroup()
                    .addGroup(searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(searchPanelLayout.createSequentialGroup()
                            .addGap(14, 14, 14)
                            .addComponent(jLabel3))
                        .addGroup(searchPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(countrySearch, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, searchPanelLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
    );
    searchPanelLayout.setVerticalGroup(
        searchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(searchPanelLayout.createSequentialGroup()
            .addGap(10, 10, 10)
            .addComponent(jLabel3)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(countrySearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    countryInput.setToolTipText("");
    countryInput.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            countryInputFocusGained(evt);
        }
    });

    jLabel1.setText("Country");

    jLabel2.setText("Capital");

    jButton1.setText("Add");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout inputPanelLayout = new javax.swing.GroupLayout(inputPanel);
    inputPanel.setLayout(inputPanelLayout);
    inputPanelLayout.setHorizontalGroup(
        inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(inputPanelLayout.createSequentialGroup()
            .addGap(15, 15, 15)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(inputPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(countryInput)
                .addComponent(capitalInput)
                .addGroup(inputPanelLayout.createSequentialGroup()
                    .addGap(8, 8, 8)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inputPanelLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
    );
    inputPanelLayout.setVerticalGroup(
        inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(inputPanelLayout.createSequentialGroup()
            .addGap(19, 19, 19)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(countryInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(23, 23, 23)
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(capitalInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
            .addContainerGap())
    );

    jScrollPane1.setViewportView(capitalList);

    javax.swing.GroupLayout listPanelLayout = new javax.swing.GroupLayout(listPanel);
    listPanel.setLayout(listPanelLayout);
    listPanelLayout.setHorizontalGroup(
        listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(listPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
            .addContainerGap())
    );
    listPanelLayout.setVerticalGroup(
        listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, listPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(inputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(searchPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(listPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(listPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(inputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(searchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String text = countryInput.getText();
    String text2 = capitalInput.getText();
    hashMap.put(text,text2);

}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
hashMap.get(countrySearch.getText());
}                                        

private void countryInputFocusGained(java.awt.event.FocusEvent evt) {                                         
    countryInput.requestFocusInWindow();
}                                        


// Variables declaration - do not modify                     
private javax.swing.JTextField capitalInput;
private javax.swing.JList capitalList;
private javax.swing.JTextField countryInput;
private javax.swing.JTextField countrySearch;
private javax.swing.JPanel inputPanel;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel listPanel;
private javax.swing.JPanel searchPanel;
// End of variables declaration                   
}
Was it helpful?

Solution

Look into the documentation of JList and it's many example use cases.

You can do something like this by modifying your adding values event using setListData() method:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
    String text = countryInput.getText();
    String text2 = capitalInput.getText();
    hashMap.put(text,text2);
    capitalList.setListData(hashMap.values().toArray());
}

I've successfully tested it:

MapPanel mp = new MapPanel();
JFrame frame = new JFrame("Countries and capitals");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mp);
frame.pack();
frame.setVisible(true);

After adding a country, its capital is shown up in the right list. Problem solved ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top