将该项目添加到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添加项目,然后从组合框隐藏或删除该项目。

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

最佳答案

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

当然-您已经从EventList派生了一个来填充自动完成安装,但是GlazedLists确实希望您能够利用EventList在整个过程中而不是在快速交换期间保存对象。像JComboBox和JList这样的纯Java Swing组件会迫使您沿着数组和向量的路线前进,但是如果坚持使用各种EventList实现作为基本集合类,则GlazedLists将提供许多帮助程序类。它具有用于combobox和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());
        }
    }
}

10-02 08:12