我在java中使用glazedlists
用于JComboBox,使JComboBox可搜索和可排序。但有一种情况我无法解决。
我有一个附加有 glazedlists
的 JComboBox
。 Gladedlists 采用字符串数组来填充此组合框并使其可像这样搜索
String[] Values = {"ABC", "DEF", "GHI", "JKL", "MNO"};
JComboBox cmb = new JComboBox();
AutoCompleteSupport.install(cmb , GlazedLists.eventListOf(Values));
这很好用,但问题是我想添加ID以及来自数据库的值,为此我正在实现我自己的自定义ListCellRenderer,如下所示
class MyListRenderer extends JLabel implements ListCellRenderer
{
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Object[] itemData = (Object[])value;
setText((String)itemData[1]);
return this;
}
}
cmb.setRenderer(new MyListRenderer());
为了在 JComboBox 中增加值(value),我必须
while(rs.next()){
int id=rs.getInt("company_id");
String category=rs.getString("company_name");
Object[] itemData = new Object[] {id, category};
cmb.addItem(itemData);
}
现在如何在设置我自己的自定义渲染器时使用 glazedlists
实现我的 JComboBox
?
最佳答案
按照你的方法没有取得任何成功,但我在早期的项目中找到了解决方案。您可以通过执行以下操作来设置 JComboBox 的模型:
//load the list of objects to use
ContainerClass[] allOptions = ContainerClass.getAll();
//create an EventList with this list and set is as the combobox model
final EventList<ContainerClass> eventList = GlazedLists.eventList(Arrays.asList(allOptions));
comboBox.setModel(new DefaultComboBoxModel<ContainerClass>(allOptions));
//finally initialize the combobox by SwingUtilities if done on a non-UI thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AutoCompleteSupport<ContainerClass> install =
AutoCompleteSupport.install(comboBox, eventList);
install.setFilterMode(TextMatcherEditor.CONTAINS);
install.setHidesPopupOnFocusLost(true);
install.setSelectsTextOnFocusGain(false);
install.setCorrectsCase(false);
}
});
并且使用 ContainerClass,例如:
class ContainerClass{
int id;
String company;
//helper method to get all the objects needed
static ContainerClass[] getAll(){
ContainerClass test = new ContainerClass();
test.id = 2;
test.company = "abc";
return new ContainerClass[]{test,test,test};
}
@Override
//this String is what actually will be displayed in the combobox
public String toString(){return "(" + id + ") " + company;}
}
我假设您的 JComboBox 具有以下类型:
JComboBox<ContainerClass> comboBox;
(我必须混淆所有变量的名称,因此代码中可能存在错误。请告诉我,我会更正它们)
所以回顾一下。 GlazedLists 使用模型来获取名称,它再次向 ContainerClass 询问它的 toString() 方法,该方法将返回要在 JComboBox 中显示的名称。
请注意,当您调用 comboBox.getSelectedItem() 时,它将返回 ContainerClass 类型的对象,如果不是有效的选择,则返回 null。
更新
如果您希望能够控制顺序和名称,则需要为 ComboBox 实现您自己的模型。找到this这似乎很好地解释了这一点:
class MyComboBoxModel extends AbstractListModel implements ComboBoxModel {
String[] ComputerComps = { "Monitor", "Key Board", "Mouse", "Joy Stick",
"Modem", "CD ROM", "RAM Chip", "Diskette" };
String selection = null;
public Object getElementAt(int index) {
return ComputerComps[index];
}
public int getSize() {
return ComputerComps.length;
}
public void setSelectedItem(Object anItem) {
selection = (String) anItem; // to select and register an
} // item from the pull-down list
// Methods implemented from the interface ComboBoxModel
public Object getSelectedItem() {
return selection; // to add the selection to the combo box
}
}
关于java - java中带有ID的gladedlists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26303006/