Document Listener非活动Java

时间:2013-02-04 17:17:38

标签: java swing jcombobox documentlistener

我正在尝试从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
    }
}

此致

2 个答案:

答案 0 :(得分:3)

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

  • 派生文档的使用使我有了自动完成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();
            }
        });
    }
}

答案 1 :(得分:0)

您的编辑器应为BasicComboBoxEditor,然后您可以使用其方法并使用方法addActionListener(yourListener);