@camickr
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;
public class TestDuplicatesItems {
private Vector<String> vec = new Vector<String>();
private String[] degrees = {"AAS1", "AAS2", "AAS1", "AAS1"};
private JComboBox combo = new JComboBox(vec);
private JComboBox combo1 = new JComboBox(degrees);
private JTextField txt = new JTextField(10);
private JFrame frame = new JFrame("JComboBox with Duplicates Items");
private JPanel panel = new JPanel();
public TestDuplicatesItems() {
vec.add("AAS1");
vec.add("AAS1");
vec.add("AAS1");
vec.add("AAS1");
//combo.setEditable(true);
//combo.setBackground(Color.gray);
//combo.setForeground(Color.red);
//combo.setEditable(true);
//combo1.setBackground(Color.gray);
//combo1.setForeground(Color.red);
txt.setText("1");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(combo.getSelectedIndex());
System.out.println(combo.getSelectedItem().toString());
txt.setText(String.valueOf(combo.getSelectedIndex()));
}
});
combo1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() == ItemEvent.SELECTED)) {
System.out.println(combo1.getSelectedIndex());
System.out.println(combo1.getSelectedItem().toString());
txt.setText(String.valueOf(combo1.getSelectedIndex()));
}
}
});
panel.add(combo);
panel.add(combo1);
panel.add(txt);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestDuplicatesItems tdi = new TestDuplicatesItems();
}
});
}
}
and based on primitive array
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class SelectedComboBoxID {
private JComboBox combo = new JComboBox();
private JFrame frame = new JFrame("MyComboEg");
private JTextField txt = new JTextField(10);
private JPanel panel = new JPanel();
public SelectedComboBoxID() {
combo.addItem(new Item(1, "-"));
combo.addItem(new Item(2, "Snowboarding"));
combo.addItem(new Item(3, "Rowing"));
combo.addItem(new Item(4, "Knitting"));
combo.addItem(new Item(5, "Speed reading"));
combo.addItem(new Item(6, "Pool"));
combo.addItem(new Item(7, "None of the above"));
//comboBox.setMaximumRowCount(3);
combo.setPrototypeDisplayValue(" None of the above ");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
Item item = (Item) comboBox.getSelectedItem();
System.out.println(item.getId() + " : " + item.getDescription());
txt.setText(String.valueOf(combo.getSelectedIndex()));
}
});
//comboBox.setRenderer(new ItemRenderer());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(combo);
panel.add(txt);
frame.add(panel);
frame.pack();
//frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class ItemRenderer extends BasicComboBoxRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
Item item = (Item) value;
setText(item.getDescription().toUpperCase());
}
if (index == -1) {
Item item = (Item) value;
setText("" + item.getId());
}
return this;
}
}
private class Item {
private int id;
private String description;
public Item(int id, String description) {
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return description;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SelectedComboBoxID selectedComboBoxID = new SelectedComboBoxID();
}
});
}
}