将keyEvent应用于方法

时间:2013-12-10 07:52:43

标签: java swing netbeans jcombobox keyevent

嘿伙计们我创造了这个方法..

public void comboS() {

    ResultSet rset = null;
    String sch = ((JTextField) ComboItemName.getEditor().getEditorComponent()).getText();
    try {
        rset = new JDBC.DB().getData("SELECT * FROM item_reg WHERE id LIKE '" + sch + "%';");
    } catch (Exception e) {
        System.out.println(e);
    }
    ComboItemName.removeAllItems();
    try {
        while (rset.next()) {
            String item = rset.getString("id");
            ComboItemName.addItem(item);
        }
    } catch (SQLException ex) {
        Logger.getLogger(jfilechooser.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(sch);
    ComboItemName.setSelectedItem(null);
    ComboItemName.setPopupVisible(true);
    ((JTextField) ComboItemName.getEditor().getEditorComponent()).setText(sch);

}

我在操作中调用此方法执行JComboBox(ComboItemName)..当我键入HDD并按Enter键时,它显示数据库中的所有硬盘ID。但是我想在键入HDD时显示它。我认为为此,keyEvent应该适用于这样的方法。

public void comboS(java.awt.event.KeyEvent evt)

但是我不知道如何调用这个方法以及在组合框中的位置。给我一些帮助。谢谢。 喜欢这个

enter image description here

2 个答案:

答案 0 :(得分:2)

为此目的在编辑器组件上使用DocumentListener,以下是示例:

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;

public class Example extends JFrame {


    public Example() {
        JComboBox comboBox = new JComboBox(new String[]{"1","2"});
        comboBox.setEditable(true);
        ((JTextComponent)comboBox.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void removeUpdate(DocumentEvent arg0) {
                validateHDD(arg0);
            }

            @Override
            public void insertUpdate(DocumentEvent arg0) {
                validateHDD(arg0);
            }

            @Override
            public void changedUpdate(DocumentEvent arg0) {
                validateHDD(arg0);
            }

            private void validateHDD(DocumentEvent arg0) {
                try {
                    String text = arg0.getDocument().getText(0, arg0.getDocument().getLength());
                    if(text.equals("HDD")){
                        System.out.println("HDD entered");
                        //call your method here
                    }
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }

        });
        add(comboBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Example frame = new Example();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

validateHDD验证您是否已将HDD输入JComboBox的编辑器,然后在其为真时执行某些逻辑。

答案 1 :(得分:1)

要在ComboBox上附加KeyListener,首先必须可编辑您的Combo。

    jComboBox1.setEditable(true);

然后只需像这样附加KeyListener:

    jComboBox1.getEditor().getEditorComponent().addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
            System.out.println(e.getKeyChar() + " KEY_TYPED DETECTED. \n");
        }
        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println(e.getKeyChar() + " KEY_PRESS DETECTED. \n");
        }
        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println(e.getKeyChar() + " KEY_RELEASE DETECTED. \n");
        }
    });

enter image description here

就这么简单:)

<强>更新

正如@ alex2410发现的那样,最好用DocumentListener来实现。以下是:

    final JTextComponent tc = (JTextComponent) jComboBox1.getEditor().getEditorComponent();
    tc.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            System.out.println("Combo Value is: " + tc.getText() + " . \n");
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            System.out.println("Combo Value is: " + tc.getText() + " . \n");
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            System.out.println("Combo Value is: " + tc.getText() + " . \n");
        }
    });

enter image description here

作为旁注,请坚持Java code Conventions