将该项目添加到 jlist 后从自动完成组合框中删除该项目
在这里我添加了一个名为glazedlists_java15-1.9.0.jar的 jar 文件
这是向jpanel添加字段的代码
DefaultComboBoxModel dt=new DefaultComboBoxModel();
comboBook = new JComboBox(dt);
comboBook.addItemListener(this);
List<Book>books=ServiceFactory.getBookServiceImpl().findAllBook();
Object[] elementBook = new Object[books.size()];
int i=0;
for(Book b:books){
elementBook[i]=b.getCallNo();
// dt.addElement(elementBook[i]);
i++;
}
AutoCompleteSupport.install(comboBook, GlazedLists.eventListOf(elementBook));
comboBook.setBounds(232, 151, 184, 22);
issuePanel.add(comboBook);
btnAdd = new JButton("+");
btnAdd.addActionListener(this);
btnAdd.setBounds(427, 151, 56, 23);
issuePanel.add(btnAdd);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(232, 184, 184, 107);
issuePanel.add(scrollPane);
v=new Vector<String>();
listBooks = new JList(v);
scrollPane.setViewportView(listBooks);
btnRemove = new JButton("-");
btnRemove.addActionListener(this);
btnRemove.setBounds(427, 185, 56, 23);
issuePanel.add(btnRemove);
操作在这里执行代码..
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnAdd){
DefaultComboBoxModel dcm = (DefaultComboBoxModel) comboBook.getModel();
dcm.removeElementAt(index);
// Add what the user types in JTextField jf, to the vector
v.add(selectedBook);
// Now set the updated vector to JList jl
listBooks.setListData(v);
// Make the button disabled
jb.setEnabled(false);
}
else if(e.getSource()==btnRemove){
// Remove the selected item
v.remove(listBooks.getSelectedValue());
// Now set the updated vector (updated items)
listBooks.setListData(v);
}
此处的图像显示将组合框中的项目添加到 jlist,然后将项目从组合框中隐藏或删除。
如果你们知道这一点,请在这里分享答案..&谢谢!