User123
User123

Reputation: 71

How to set Label as Display and value as value in JCombobox in Swing?

My code dynamically creates Jcombobox and populates items from a query Result. This query returns an Object which includes label and value. I want to display label and returns value when calling combo.getSelectedItem(). I saw this Example while searching, but I didn't get the idea.

JComboBox<String> jComboBox=new JComboBox<String>();

if(dataSourceAttributeObjs!=null && dataSourceAttributeObjs.size()>0)
{
    jComboBox.addItem("Select");
    for(DataSourceAttributeObj dataSourceAttributeObj:dataSourceAttributeObjs)
    {
        jComboBox.addItem(dataSourceAttributeObj.getLabel());
    }
}

Upvotes: 0

Views: 6062

Answers (3)

Manoj Mohan
Manoj Mohan

Reputation: 662

You can use enumeration if u want just the name and value to be used. For Example

  enum label
  {
     label1(0),label2(1);

  int value;
  label(int value)
       {
       this.value=value;
       }

 }

and assign the comboBox,

   `JComboBox box=new JComboBox(label.values());`

if you want the value of it, get the label object and take the value from it samp = box.SelectedItem().value

Upvotes: 0

Programmer
Programmer

Reputation: 453

This will help you

public class Java extends JFrame {

private JComboBox cb;
private JLabel l;

private static String[] fileName = { "Image1.jpg", "Image2.jpg" };
private Icon[] pics = { new ImageIcon(getClass().getResource(fileName[0])),
        new ImageIcon(getClass().getResource(fileName[1])) };

Java() {



    super("Title");
    setLayout(new FlowLayout());

    cb = new JComboBox(fileName);

    cb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {

            if(e.getStateChange() == ItemEvent.SELECTED)
                l.setIcon(pics[cb.getSelectedIndex()]);

        }
    });

    add(cb);
    l = new JLabel(pics [0]);
    add(l);

}

}

Upvotes: 0

Freek de Bruijn
Freek de Bruijn

Reputation: 3622

The example you mentioned (Set Value and Label to JComboBox) describes the possibility to define a custom renderer for a combo box, which seems to be the right approach in your case.

The answer from nachokk in a piece of code:

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

public class CustomComboBoxRenderer {
    public static void main(final String[] arguments) {
        new CustomComboBoxRenderer().launchGui();
    }

    private void launchGui() {
        final JFrame frame = new JFrame("Stack Overflow: custom combo box renderer");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JComboBox<Concept> comboBox = getComboBox();
        final JLabel label = new JLabel("Please make a selection...");

        comboBox.addActionListener(actionEvent -> {
            final Object selectedItem = comboBox.getSelectedItem();
            if (selectedItem instanceof Concept)
                label.setText(((Concept) selectedItem).getValue());
        });

        final JPanel panel = new JPanel(new BorderLayout());
        panel.add(comboBox, BorderLayout.NORTH);
        panel.add(label, BorderLayout.CENTER);
        frame.getContentPane().add(panel);

        frame.setVisible(true);
    }

    private JComboBox<Concept> getComboBox() {
        final List<Concept> concepts = Arrays.asList(new Concept("label 1", "value 1"),
                                                     new Concept("label 2", "value 2"),
                                                     new Concept("label 3", "value 3"));

        final JComboBox<Concept> comboBox = new JComboBox<>(new Vector<>(concepts));

        comboBox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(final JList<?> list,
                                                          final Object value,
                                                          final int index,
                                                          final boolean isSelected,
                                                          final boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected,
                                                   cellHasFocus);
                if (value instanceof Concept)
                    setText(((Concept) value).getLabel());

                return this;
            }
        });

        return comboBox;
    }
}

And the Concept class could look like this:

public class Concept {
    private final String label;
    private final String value;

    public Concept(String label, String value) {
        this.label = label;
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public String getValue() {
        return value;
    }
}

Upvotes: 3

Related Questions