如何从渲染器中的JCombobox获取数据?

时间:2013-04-19 17:45:50

标签: java swing autocomplete jcombobox glazedlists

我需要使用弹出列表开发自动填充组合框。该列表的每一行都应突出显示键入自动填充数据 我使用glazedlists来自动完成。但是最让我烦恼的是如何突出渲染渲染器中的组合框数据?
它看起来应该是这样的 1

更新:
我只想知道如何从jcombobox编辑器向列表渲染器发送键入的字符串。我使用釉面列表并不重要。

不知何故,我发现我可以将组合框发送到渲染器并从渲染器中的组合框获取信息。但问题是我需要比现在更频繁地调用getListCellRendererComponent。有谁知道,我怎么能更频繁地使用组合框调用渲染器getListCellRendererComponent?

1 个答案:

答案 0 :(得分:0)

这是可能的。但有点辛苦工作要做 这是我发现的更改JList元素的示例代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class ListDemo extends JPanel {
    private JList list;
    private DefaultListModel listModel;
    public String match = null;

    private static final String hireString = "Highlight";
    private JTextField employeeName;

    public ListDemo() {
        super(new BorderLayout());

        listModel = new DefaultListModel();
        listModel.addElement("Test1");
        listModel.addElement("Test2");
        listModel.addElement("Test3");

        list = new JList(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.setVisibleRowCount(5);
        list.setCellRenderer(new MyListCellRenderer());
        JScrollPane listScrollPane = new JScrollPane(list);

        JButton hireButton = new JButton(hireString);
        HireListener hireListener = new HireListener(hireButton);
        hireButton.setActionCommand(hireString);
        hireButton.addActionListener(hireListener);
        hireButton.setEnabled(false);

        employeeName = new JTextField(10);
        employeeName.addActionListener(hireListener);
        employeeName.getDocument().addDocumentListener(hireListener);

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane,
                                           BoxLayout.LINE_AXIS));
        buttonPane.add(Box.createHorizontalStrut(5));
        buttonPane.add(employeeName);
        buttonPane.add(hireButton);
        buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        add(listScrollPane, BorderLayout.CENTER);
        add(buttonPane, BorderLayout.PAGE_END);
    }
    class MyListCellRenderer extends JLabel implements ListCellRenderer { // extended from JLabel. You can use an appropriate JComponent.
        public MyListCellRenderer() {
            setOpaque(true);
        }
        public Component getListCellRendererComponent(JList paramlist, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            setText(value.toString());
            if (value.toString().equals(match)) {
                setBackground(Color.BLUE);
                SwingWorker worker = new SwingWorker() {
                    @Override
                    public Object doInBackground() {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) { /*Who cares*/ }
                        return null;
                    }
                    @Override
                    public void done() {
                        match = null;
                        list.repaint();
                    }
                };
                worker.execute();
            } else
                setBackground(Color.RED);
            return this;
        }
    }
    class HireListener implements ActionListener, DocumentListener {
        private boolean alreadyEnabled = false;
        private JButton button;
        public HireListener(JButton button) {
            this.button = button;
        }
        public void actionPerformed(ActionEvent e) {
            String name = employeeName.getText();
            if (listModel.contains(name)) {
                match = name;
                list.repaint();
                employeeName.requestFocusInWindow();
                employeeName.selectAll();
                return;
            }
            if (name.equals("")) {
                Toolkit.getDefaultToolkit().beep();
                employeeName.requestFocusInWindow();
                employeeName.selectAll();
                return;
            }
            int index = list.getSelectedIndex();
            if (index == -1)
                index = 0;
            else
                index++;
            listModel.insertElementAt(employeeName.getText(), index);
            employeeName.requestFocusInWindow();
            employeeName.setText("");
            list.setSelectedIndex(index);
            list.ensureIndexIsVisible(index);
        }
        public void insertUpdate(DocumentEvent e) {
            enableButton();
        }
        public void removeUpdate(DocumentEvent e) {
            handleEmptyTextField(e);
        }
        public void changedUpdate(DocumentEvent e) {
            if (!handleEmptyTextField(e))
                enableButton();
        }
        private void enableButton() {
            if (!alreadyEnabled)
                button.setEnabled(true);
        }
        private boolean handleEmptyTextField(DocumentEvent e) {
            if (e.getDocument().getLength() <= 0) {
                button.setEnabled(false);
                alreadyEnabled = false;
                return true;
            }
            return false;
        }
    }
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("ListDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = new ListDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() { createAndShowGUI(); }
        });
    }
}

如果你擅长java,你会清楚地知道这里做了什么 (抱歉,我找不到此代码的链接。只需复制我的本地代码。) 您可以使用glazedists的getComboBox()方法获取相应的JComboBox和;

JList l = new JList();
l.setModel(glazedlist1.getComboBox().getModel()); // glazedlist1 -> your auto complete list

会将该组合框的模型分配给新的JList 您可以将class MyListCellRenderer extends JLabel implements...的扩展名更改为JTextPaneJEditorPane以单独格式化文本。
希望这会有所帮助。
对不起,代码不完整。