我希望能够将多个JComboBox与单个公共数据源一起使用。我希望组合框能够显示该列表中的项目或空白项目,最重要的是,如果当前由另一个组合框选择它,我希望它们不显示项目(但如果它未被选中则显示它)
我一直试图通过调用removeDuplicates()方法来解决这个问题,该方法应该将所有当前选定的项添加到列表中,从主列表中删除它,然后将其设置为组合框的列表。 / p>
这给了我一些有趣的问题。在我的程序中,主列表中没有任何选项显示,即使是最初,但如果我删除了我的代码以删除重复项(初始化已经过测试工作),它们就会出现。
即使它们是可编辑的组合框,我也无法将选定的项目写入它们,只要按下回车键,它就会被删除。
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);
}
}
}
上面我发布了一个SSCCE,它与我的问题没有完全相同,但无论如何它仍然有问题,并使用我用来尝试解决我的问题的相同方法。是否有我缺少的东西,或者这只是一个解决方案的不好方法?
答案 0 :(得分:3)
如图here所示,操作模型而不是视图。在这种情况下,不同的模型可以共享对知道模型的公共数据源的访问。
答案 1 :(得分:2)
一种方法是在组合的下拉列表中简单地“禁用”项目(即,使它们无法选择),如果它们已在另一个组合框中被选中的话。
以下是禁用JComboBox
项目(使用金属)的一种方法:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
public class JComboBoxDisabledItemsDemo implements ItemListener, Runnable
{
private static final String ITEMS[] =
{ "Black", "Blue", "Green", "Orange", "Purple", "Red", "White", "Yellow"};
private JComboBox jCombo;
private JCheckBox[] checkBoxes = new JCheckBox[ITEMS.length];
private boolean[] enabledFlags = new boolean[ITEMS.length];
public static void main(String args[])
{
SwingUtilities.invokeLater(new JComboBoxDisabledItemsDemo());
}
@SuppressWarnings("unchecked")
public void run()
{
JPanel pnlEnablers = new JPanel(new GridLayout(0,1));
pnlEnablers.setBorder(BorderFactory.createTitledBorder("Enabled Items"));
for (int i = 0; i < ITEMS.length; i++)
{
checkBoxes[i] = new JCheckBox(ITEMS[i]);
checkBoxes[i].setSelected(true);
checkBoxes[i].addItemListener(this);
enabledFlags[i] = true;
pnlEnablers.add(checkBoxes[i]);
}
jCombo = new JComboBox(ITEMS);
jCombo.setUI(new MetalComboBoxUI()
{
@Override
protected ComboPopup createPopup()
{
ComboPopup cp = new BasicComboPopup( comboBox )
{
@Override
protected void configureList()
{
super.configureList();
list.setSelectionModel(new DisabledItemSelectionModel());
}
};
return cp;
}
});
jCombo.setRenderer(new DisabledItemListCellRenderer());
JFrame f = new JFrame("Colors");
Container contentPane = f.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(pnlEnablers, BorderLayout.CENTER);
contentPane.add(jCombo, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(240, 280);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public void itemStateChanged(ItemEvent event)
{
JCheckBox checkBox = (JCheckBox) event.getSource();
int index = -1;
int selectedComboIndex = jCombo.getSelectedIndex();
for (int i = 0; i < ITEMS.length; i++)
{
if (ITEMS[i].equals(checkBox.getText()))
{
index = i;
break;
}
}
if (index != -1)
{
enabledFlags[index] = checkBox.isSelected();
jCombo.repaint();
if (index == selectedComboIndex)
{
jCombo.setSelectedIndex(-1);
}
}
}
public class DisabledItemListCellRenderer extends DefaultListCellRenderer
{
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
if (index < 0 || enabledFlags[index])
{
return super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
}
Component comp = super.getListCellRendererComponent(list, value, index,
false, false);
comp.setEnabled(false);
return comp;
}
}
public class DisabledItemSelectionModel extends DefaultListSelectionModel
{
/**
* No need to override addSelectionInterval(int index0, int index1)
* since we're using SINGLE_SELECTION mode for this demo.
*/
@Override
public void setSelectionInterval(int index0, int index1)
{
if (enabledFlags[index0])
{
super.setSelectionInterval(index0, index0);
}
else
{
/*
* The previously selected index is before this one,
* so walk forward to find the next selectable item.
*/
if (getAnchorSelectionIndex() < index0)
{
for (int i = index0; i < enabledFlags.length; i++)
{
if (enabledFlags[i])
{
super.setSelectionInterval(i, i);
return;
}
}
}
/*
* Otherwise, walk backward to find the next selectable item.
*/
else
{
for (int i = index0; i >= 0; i--)
{
if (enabledFlags[i])
{
super.setSelectionInterval(i, i);
return;
}
}
}
}
}
}
}