import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class FilteringComboBoxDemo {
private final JComboBox comboBox1;
private final JComboBox comboBox2;
private final JComboBox comboBox3;
public FilteringComboBoxDemo() {
final JPanel contentPane = new JPanel();
String[] values = new String[]{"a", "b", "c", "d", "e", "f", "g"};
//create combobox 1
FilteringComboBoxModel model = new FilteringComboBoxModel(values);
comboBox1 = new JComboBox(model);
contentPane.add(comboBox1);
//create combobox 2
model = new FilteringComboBoxModel(values.clone());
comboBox2 = new JComboBox(model);
contentPane.add(comboBox2);
//create combobox 3
model = new FilteringComboBoxModel(values.clone());
comboBox3 = new JComboBox(model);
contentPane.add(comboBox3);
comboBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
filter(1);
}
});
comboBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
filter(2);
}
});
comboBox3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
filter(3);
}
});
final JFrame f = new JFrame("FilteringComboBox Demo");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(new Dimension(400, 300));
f.setLocationRelativeTo(null);
f.setContentPane(contentPane);
f.setVisible(true);
}
private void filter(final int i) {
Object selectedItem1 = comboBox1.getSelectedItem();
Object selectedItem2 = comboBox2.getSelectedItem();
Object selectedItem3 = comboBox3.getSelectedItem();
FilteringComboBoxModel model1 = (FilteringComboBoxModel) comboBox1.getModel();
FilteringComboBoxModel model2 = (FilteringComboBoxModel) comboBox2.getModel();
FilteringComboBoxModel model3 = (FilteringComboBoxModel) comboBox3.getModel();
if (i == 1) {
model2.filter(selectedItem1, selectedItem3);
model3.filter(selectedItem1, selectedItem2);
} else if (i == 2) {
model1.filter(selectedItem2, selectedItem3);
model3.filter(selectedItem1, selectedItem2);
} else {
model1.filter(selectedItem2, selectedItem3);
model2.filter(selectedItem1, selectedItem3);
}
}
private static class FilteringComboBoxModel extends DefaultComboBoxModel {
private final String[] values;
private List<String> fileredValues = new ArrayList<String>();
FilteringComboBoxModel(final String[] values) {
this.values = values.clone();
fileredValues.addAll(Arrays.asList(values));
}
public void filter(final Object selected1, final Object selected2) {
if (selected1 == null && selected2 == null) {
return;
}
final String newValue1 = (String) selected1;
final String newValue2 = (String) selected2;
final int oldSize = fileredValues.size();
if (oldSize != 0) {
fileredValues.clear();
}
for (final String test : values) {
if (newValue1 != null && newValue2 != null) {
if (!test.equals(newValue1) && !test.equals(newValue2)) {
fileredValues.add(test);
}
} else if (newValue1 != null) {
if (!test.equals(newValue1)) {
fileredValues.add(test);
}
} else {
if (!test.equals(newValue2)) {
fileredValues.add(test);
}
}
}
fireContentsChanged(this, 0, fileredValues.size() - 1);
}
@Override
public Object getElementAt(final int index) {
return fileredValues.get(index);
}
@Override
public int getSize() {
return fileredValues.size();
}
}
public static void main(final String... args) {
Runnable gui = new Runnable() {
@Override
public void run() {
FilteringComboBoxDemo filteringComboBoxDemo = new FilteringComboBoxDemo();
}
};
SwingUtilities.invokeLater(gui);
}
}