Question

public class ListComboBox extends JFrame {

private String MIS = "MULTIPLE_INTERVAL_SELECTION";
private String SIS = "SINGLE_INTERVAL_SELECTION";
private String SS = "SINGLE_SELECTION";
final int COUNTRIES = 9;
private String[] countries = {"Canada", "China", "Denmark",
    "France", "Germany", "India", "Norway", "United Kingdom",
    "United States of America"};
private JList<String> jlst = new JList<String>(countries);
private JLabel comboLabel = new JLabel("Choose Selection Mode: ");
private JComboBox jcbo = new JComboBox();
//to hold country labels
private JLabel countryLabel = new JLabel();

public static void main(String[] args) {

    ListComboBox frame = new ListComboBox();
    frame.setSize(400, 200);
    frame.setTitle("Exercise 17.14");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public ListComboBox() {

    //Adding selection option to combobox
    jcbo.addItem(MIS);
    jcbo.addItem(SIS);
    jcbo.addItem(SS);

    // Register listener combobox
    jcbo.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getItem() == MIS) {
                jlst.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            if (e.getItem() == SIS) {
                jlst.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            }
            if (e.getItem() == SS) {
                jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            }
        }
    });

    //Combobox panel
    JPanel combopanel = new JPanel(new FlowLayout());
    combopanel.add(comboLabel);
    combopanel.add(jcbo);
    add(combopanel, BorderLayout.NORTH);

    //List panel
    JScrollPane listpanel = new JScrollPane(jlst);
    add(listpanel, BorderLayout.CENTER);

    //Bottom label panel
    final JPanel labelpanel = new JPanel();
    labelpanel.add(countryLabel);
    add(labelpanel, BorderLayout.SOUTH);

    //List listener
    jlst.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            StringBuilder sb = new StringBuilder(64);
            int[] indices = jlst.getSelectedIndices();
            int i;
            for (i = 0; i < indices.length; i++) {
                if (sb.length() > 0) {
                    sb.append(", ");
                }
                sb.append(countries[indices[i]]);
            }
            countryLabel.setText(sb.toString());
        }
    });
}
}

Good day, I need your help.

What I need code to do: Add selected country names from the list with scroll bar to the label below the list, remove them as they are unselected in the list.

List selection mode can be switched in JComboBox on top. Everything works fine, but I cannot figure out the way for country names to properly appear inside the label.

Any tips on how I might accomplish that?

Thanks!

UPDATED!

Was it helpful?

Solution

setName is used for internal identification of the component. Imagin you've been give a list of components, all you know is you need to find the one with some unique identifier, that identifier is supplied via the name property. It has no effect on the output of the component.

You need to use the setText method to change what is displayed on the screen.

The next problem you'll have is setText is a replacement method. That is, it will replace what ever was previously applied with the new value. What might need to do is build a temporary String of the values you want to display and then apply that value to the label, for example...

StringBuilder sb = new StringBuilder(64);
for (i = 0; i < indices.length; i++) {
    if (sb.length() > 0) {
        sb.append(", ");
    }
    sb.append(countries[indices[i]]);
}        
countryLabel.setText(sb.toString());

OTHER TIPS

To set text on JLabel use countryLabel.setText instead of countryLabel.setName. Another issue is that in the posted code countryLabel is not added to the frame. I assume it should go into labelpanel, but this part is commented out.

Some other observations:

Do not mix light weight and heavy weight components. See Mixing Heavyweight and Lightweight Components. Instead of ScrollPane use JScrollPane, for example:

JScrollPane listpanel = new JScrollPane(jlst);

Also there is no need to revalidate() the container when you set text on JLabel. The label will be refreshed as a result of setText() method.

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