I want to be able to use multiple JComboBoxes with a single common data source. I want the combo boxes to be able to display either an item from that list or a blank item, and most importantly I want them to not display an item if it is currently selected by another combo box (but show it if it become unselected).
I have been trying to go about this by calling a removeDuplicates() method which should add all the currently selected items to a list, remove that from the master list, and then set that as the list for the combo boxes.
This has been giving me some interesting problems. In my program none of the options from the master list show up, even initially, though they do if I get rid of my code to remove duplicates (the initialization is tested working).
Also even though they are editable combo boxes I cannot write a selected item to them, it gets deleted as soon as I press the enter button.
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Main extends JFrame implements ActionListener
{
ArrayList<String> commonItemList = new ArrayList<String>(Arrays.asList("Dog", "Cat", "Fish", "Bear", "Lion"));
ArrayList<JComboBox<Object>> comboBoxes = new ArrayList<>();
public Main()
{
this.setSize(new Dimension(500, 150));
this.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JComboBox<Object> comboBox1 = new JComboBox<Object>(commonItemList.toArray());
comboBox1.setEditable(true);
comboBox1.addActionListener(this);
JComboBox<Object> comboBox2 = new JComboBox<Object>(commonItemList.toArray());
comboBox2.setEditable(true);
comboBox2.addActionListener(this);
JComboBox<Object> comboBox3 = new JComboBox<Object>(commonItemList.toArray());
comboBox3.setEditable(true);
comboBox3.addActionListener(this);
this.add(comboBox1);
comboBoxes.add(comboBox1);
this.add(comboBox2);
comboBoxes.add(comboBox2);
this.add(comboBox3);
comboBoxes.add(comboBox3);
}
public static void main(String[] args)
{
Main main = new Main();
main.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
removeDuplicates();
}
private void removeDuplicates()
{
ArrayList<String> currentlyUsedItems = new ArrayList<>();
ArrayList<String> availableItems = commonItemList;
// Add all currently selected items to usedItems list
for (JComboBox<Object> comboBox : comboBoxes)
{
currentlyUsedItems.add((String) comboBox.getSelectedItem());
}
// For every string in currentlyUsedItems remove it from availableItems
for (String string : currentlyUsedItems)
{
availableItems.remove(string);
}
// Remove all items from combobox, then add back all available Items, while disabling actionListener
for (JComboBox<Object> comboBox : comboBoxes)
{
comboBox.removeActionListener(this);
comboBox.removeAllItems();
for (String string : availableItems)
{
comboBox.addItem(string);
}
comboBox.addActionListener(this);
}
}
}
Above I have posted a SSCCE, which does not have exactly the same issues as mine but it still bugged regardless, and uses the same method I used to attempt to solve my issue. Is there something I am missing or is this just a poor way to go about a solution?