Question

is there a way to set Key-Value String Map/Pair in JCombobox Netbeans Swing Matisse using GUI only ? Below screenshot allow to insert single list JComboBox List Data

but is there a way to insert Map/Key-Value String Pair using Matisse GUI instead of code like

Value - Display
_____   _____
ITEM1 - Item 1
ITEM2 - Item 2
ITEM3 - Item 3
ITEM4 - Item 4

as in HTML select option tag stored value and display value.

Was it helpful?

Solution

So that why i am asking about using GUI not Code

Don't depend on an IDE to write/generate your code for you. The code will never be portable.

Maybe you can create a text file of all you key/value pairs. Then you create a simple routine that reads each file parses the data and adds a custom object to the ComboBoxModel.

For an example of such a custom object check out Combo Box With Hidden Data. It is a simple object that overrides the toString() method to display the value in the combo box.

And for those that suggest you should be using a custom Renderer, well they are only half right. Check out Combo Box With Custom Renderer which allows you to use a custom renderer without breaking the default functionality of the combo box.

OTHER TIPS

" using GUI only ? "

I assume you mean from design view. I don't think so. Just hand code it. It's not that difficult.

Here's an example using a Student object as the map value, and the Student id as the map key. The key is the display value in the JComboBox. The value is retrieved from the selection by using get(id) from the map.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class MapCombo {

    public MapCombo() {
        Map<Integer, Student> map = createMap();
        JComboBox cbox = createComboBox(map);
        cbox.setBorder(new EmptyBorder(20, 20, 20, 20));

        JFrame frame = new JFrame("Map ComboBox");
        frame.add(cbox);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private Map<Integer, Student> createMap() {
        Map<Integer, Student> map = new HashMap<>();
        Student s1 = new Student(23, "Micheal Jordan");
        Student s2 = new Student(6, "Lebron James");
        Student s3 = new Student(3, "Chris Paul");
        Student s4 = new Student(8, "Kobe Briant");
        Student s5 = new Student(21, "Tim Duncan");

        map.put(s1.getId(), s1);
        map.put(s2.getId(), s2);
        map.put(s3.getId(), s3);
        map.put(s4.getId(), s4);
        map.put(s5.getId(), s5);

        return map;
    }

    private JComboBox createComboBox(final Map<Integer, Student> map) {
        final JComboBox cbox = new JComboBox();
        for (Integer id : map.keySet()) {
            cbox.addItem(id);
        }

        cbox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Integer id = (Integer)cbox.getSelectedItem();
                System.out.println(map.get(id));
            }
        });

        return cbox;
    }

    public class Student {

        String name;
        Integer id;

        public Student(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public Integer getId() {
            return id;
        }

        @Override
        public String toString() {
            return "Name: " + name + " - Stud ID: " + id;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MapCombo();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top