0

我正在尝试从 ComboBox 编辑器中获得响应。所以我document listener在组合框中添加了一个。问题是文档侦听器似乎根本没有工作。我认为听众没有连接到combobox editor. 我知道我错过了一些东西。这是代码:

    // constructor of GUI class
    ComboBoxDocumentHandler handle = new ComboBoxDocumentHandler();

    JTextComponent comboEditorComponent =(JTextComponent) InstantSearchBox.getEditor().getEditorComponent();

    doc = comboEditorComponent.getDocument();

    doc.addDocumentListener(handle);

   // Document Handler class and its method
private class ComboBoxDocumentHandler implements DocumentListener
{

    @Override
    public void changedUpdate(DocumentEvent event) {
    System.out.println("Inside"); // this statement is not printing when I add something to the combobox editor
    }
}

问候

4

2 回答 2

3
  • 所有这些事件都不会最终确定,直到派生的带有 JList 的 JPopup 可见,并且来自 Action 或 ItemListener 的所有事件都已完成,

  • 派生文档的使用使我对 AutoComplete JComboBox 有意义(基本上必须包含文档并带有偏差)

  • 有关所有相关事件和侦听器的示例

编辑

import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class ComboBoxHoverOver {

    private JComboBox combo = new JComboBox();

    public ComboBoxHoverOver() {
        combo.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXX");
        combo.setRenderer(new ComboToolTipRenderer(combo));
        combo.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                System.out.println("itemStateChanged");
            }
        });
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("actionPerformed");
            }
        });
        combo.addItem("");
        combo.addItem("Long text 4");
        combo.addItem("Long text 3");
        combo.addItem("Long text 2");
        combo.addItem("Long text 1");
        combo.setEditable(true);
        JTextComponent tc = (JTextComponent) combo.getEditor().getEditorComponent();
        tc.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLog(e, "inserted into");
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateLog(e, "inserted into");
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateLog(e, "inserted into");
            }

            public void updateLog(DocumentEvent e, String action) {
                Document doc = e.getDocument();
                int changeLength = e.getLength();
                System.out.println((changeLength + " character"
                        + ((changeLength == 1) ? " " : "s ")
                        + action + " " + doc.getProperty("name") + "."
                        + "  Text length = " + doc.getLength()));
            }
        });
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(combo);
        f.pack();
        f.setVisible(true);
    }

    private class ComboToolTipRenderer extends BasicComboBoxRenderer {

        private static final long serialVersionUID = 1L;
        private JComboBox combo;
        private JList comboList;

        ComboToolTipRenderer(JComboBox combo) {
            this.combo = combo;
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            System.out.println(value + ", " + index + ", " + isSelected + ", " + cellHasFocus);
            if (comboList == null) {
                comboList = list;
                KeyAdapter listener = new KeyAdapter() {

                    @Override
                    public void keyReleased(KeyEvent e) {
                        System.out.println("keyReleased " + e.getKeyCode());
                        if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP) {
                            int x = 5;
                            int y = comboList.indexToLocation(comboList.getSelectedIndex()).y;
                            System.out.println("keyReleased " + comboList.getSelectedIndex());
                        }
                    }
                };
                combo.addKeyListener(listener);
                combo.getEditor().getEditorComponent().addKeyListener(listener);
                comboList.addListSelectionListener(new ListSelectionListener() {

                    public void valueChanged(ListSelectionEvent e) {
                        if (e.getValueIsAdjusting()) {
                            JList list = (JList) e.getSource();
                            int item = list.getSelectedIndex();
                            if (item > -1) {
                                String string = list.getSelectedValue().toString();
                                System.out.println("valueChanged " + list.getSelectedValue().toString());
                            }
                        }
                    }
                });
            }
            if (isSelected) {
                System.out.println("isSelected " + value.toString());
            }
            return this;
        }
    }

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComboBoxHoverOver comboBoxHoverOver = new ComboBoxHoverOver();
            }
        });
    }
}
于 2013-02-04T17:39:27.970 回答
0

您的编辑器应该是 a BasicComboBoxEditor,然后您可以使用它的方法并使用该方法addActionListener(yourListener);

于 2013-02-04T17:24:49.260 回答