gpt4 book ai didi

java - 将该项目添加到 java 中的 jlist 后,从自动完成组合框中删除该项目

转载 作者:行者123 更新时间:2023-11-30 09:11:54 26 4
gpt4 key购买 nike

将项目添加到 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);

Action 执行代码在这里..

 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);

}

enter image description here

这里的图像显示从组合框中添加一个项目到 jlist,然后该项目隐藏或从组合框中删除。

如果你们知道这件事,请在这里分享答案..谢谢 !!!

最佳答案

从您的描述和代码中我可以看出,您只是使用 GlazedLists 便捷方法来设置初始自动完成组件,但您没有使用 GlazedLists 的基本部分将各种元素拼接在一起:事件列表。

当然 - 您派生了一个一次性的 EventList 来填充自动完成安装,但 GlazedLists 确实希望您在整个过程中而不是在快速交换期间使用 EventLists 来保存您的对象。 JComboBox 和 JList 等纯 Java Swing 组件迫使您走上数组和 vector 的道路,但如果您坚持将各种 EventList 实现作为基本集合类,GlazedLists 会提供大量帮助器类。它有用于组合框和 jlist 模型的类,以及选择模型类,这些类可以方便地连接 EventLists 和 Swing 组件,一旦你沿着这条路走下去,你就可以真正简化你的代码。

这是我认为您要实现的目标的一个非常粗略的示例。我认为代码不言自明,而不是我再胡扯了。 注意:我使用的是 GlazedLists 1.8。

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.swing.AutoCompleteSupport;
import ca.odell.glazedlists.swing.EventComboBoxModel;
import ca.odell.glazedlists.swing.EventListModel;
import ca.odell.glazedlists.swing.EventSelectionModel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

/**
*
* @author Andrew Roberts
*/
public class GlazedListsAutocompleteTest {

private JFrame mainFrame;
private JComboBox availableItems;
private EventList<Book> books = new BasicEventList<Book>();
private EventList<Book> selectedBooks;

public GlazedListsAutocompleteTest() {
populateAvailableBooks();
createGui();
mainFrame.setVisible(true);
}

private void populateAvailableBooks() {
books.add(new Book("A Tale of Two Cities"));
books.add(new Book("The Lord of the Rings"));
books.add(new Book("The Hobbit"));
books.add(new Book("And Then There Were None"));
}

private void createGui() {

mainFrame = new JFrame("GlazedLists Autocomplete Example");
mainFrame.setSize(600, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//EventComboBoxModel<Book> comboModel = new EventComboBoxModel<Book>(books);
availableItems = new JComboBox();
final SortedList<Book> availableBooks = new SortedList<Book>((BasicEventList<Book>) GlazedLists.eventList(books), new BookComparitor());

selectedBooks = new SortedList<Book>(new BasicEventList<Book>(), new BookComparitor());

AutoCompleteSupport autocomplete = AutoCompleteSupport.install(availableItems, availableBooks);

JButton addButton = new JButton("+");
addButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
EventComboBoxModel<Book> comboModel = (EventComboBoxModel<Book>) availableItems.getModel();
try {
Book book = (Book) comboModel.getSelectedItem();
selectedBooks.add(book);
availableBooks.remove(book);
} catch (ClassCastException ex) {
System.err.println("Invalid item: cannot be added.");
}

}
});

final EventListModel listModel = new EventListModel(selectedBooks);
final EventSelectionModel selectionModel = new EventSelectionModel(selectedBooks);

JButton removeButton = new JButton("Remove");
removeButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
EventList<Book> selectedListItems = selectionModel.getSelected();
for (Book book : selectedListItems) {
selectedBooks.remove(book);
availableBooks.add(book);
}
}
});

JPanel panel = new JPanel(new BorderLayout());
panel.add(availableItems, BorderLayout.CENTER);
panel.add(addButton, BorderLayout.EAST);


JList selectedItemsList = new JList(listModel);
selectedItemsList.setSelectionModel(selectionModel);
mainFrame.setLayout(new BorderLayout());
mainFrame.getContentPane().add(panel, BorderLayout.NORTH);
mainFrame.getContentPane().add(selectedItemsList, BorderLayout.CENTER);
mainFrame.getContentPane().add(removeButton, BorderLayout.SOUTH);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GlazedListsAutocompleteTest();
}
});
}

class Book {
private String title;

public Book() {
}

public Book(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return getTitle();
}
}

class BookComparitor implements Comparator<Book> {

@Override
public int compare(Book b1, Book b2) {
return b1.getTitle().compareToIgnoreCase(b2.getTitle());
}
}
}

关于java - 将该项目添加到 java 中的 jlist 后,从自动完成组合框中删除该项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com