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

时间:2014-02-19 12:51:42

标签: java swing combobox jlist glazedlists

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

    }

enter image description here

此处图片显示将组合框中的项目添加到jlist,然后项目隐藏或从组合框中删除。

如果你们知道这个,请在​​这里分享答案。&amp;谢谢你!

1 个答案:

答案 0 :(得分:2)

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

当然 - 您已经派生了一个关闭EventList来填充自动完成安装,但是GlazedLists真的希望您利用EventLists来保存您的对象,而不是在快速交换期间。像JComboBox和JList这样的纯Java Swing组件迫使您沿着数组和向量的路径走下去,但如果您坚持使用各种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());
        }
    }
}