Java ComboBox可编辑每次重新列出任何键时获取字符串

cl25kdpy  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(177)

我需要有一个comboBox,每次该字母,数字或空格写(释放键),一个字符串是返回的文本写在comboBox内,而无需按Enter键。
例如:我按下de“a”键,然后释放键“a”,我的字符串需要返回“a”接下来,我按下“B”键,我意识到,我的字符串返回“ab”等。
我使用的代码来自:http://www.java2s.com/Tutorials/Java/Swing_How_to/JComboBox/Add_key_listener_to_Editable_JcomboBox_TextField.htm
我在Key Released事件中添加了:

Object value = combobox.getSelectedItem();
System.out.println("String: " + value);

在这种情况下,只有当我按Enter键时才能工作,但我不希望这样
艾米的建议
谢谢

import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
//w ww.j  a v  a2  s  .  c om
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class SwingListenerDemo extends JFrame implements KeyListener {
    JComboBox<String> combobox;
  JComboBox<String> combobox1 = new JComboBox(new String[]{"b","b","c"});
  public static void main(String[] args) {
    new SwingListenerDemo();
  }

  private SwingListenerDemo() {
    String[] options = {"Red", "Green", "Blue"};
    combobox = new JComboBox<String>(options);
    combobox.setEditable(true);
    combobox.setSelectedIndex(-1);
  
    combobox1.setEditable(true);
    JTextField editor = (JTextField) combobox.getEditor().getEditorComponent();
    System.out.println("editor" + editor);
    editor.addKeyListener(this);
    setLayout(new FlowLayout());
    add(combobox);
    add(combobox1);
    pack();
    setVisible(true);
  }

  public void keyTyped(KeyEvent arg0) {
    System.out.println("Key Typed " + arg0.getKeyCode());
  }

  public void keyPressed(KeyEvent arg0) {
    System.out.println("Key Pressed " + arg0.getKeyCode());
  }

  public void keyReleased(KeyEvent arg0) {
    System.out.println("Key Released " + arg0.getKeyCode());
    Object value = combobox.getSelectedItem();
    System.out.println("String: " + value);

  }
}
46qrfjad

46qrfjad1#

不要使用KeyListener作为文本字段的监视器输入,因为它不适合这个任务,原因有很多。
使用DocumentListener。参见How to Write a Document Listener
下面的例子使用了一个自定义的ComboBoxEditor,你不“需要”进行这种扩展,但是它确实隔离了核心功能,这使得它更容易重用。

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBorder(new EmptyBorder(32, 32, 32, 32));
            JComboBox<String> comboBox = new JComboBox<>(new String[]{"Test", "Testy", "Testing", "More testing"});
            comboBox.setEditor(new MonitorableComboBoxEditor(new MonitorableComboBoxEditor.Observer() {
                @Override
                public void editorTextDidChange(String text) {
                    System.out.println("Text did change " + text);
                }
            }));
            comboBox.setEditable(true);

            add(comboBox);
        }
    }

    public class MonitorableComboBoxEditor implements ComboBoxEditor {

        public interface Observer {
            public void editorTextDidChange(String text);
        }

        private JTextField editor = new JTextField();

        public MonitorableComboBoxEditor(Observer observer) {
            editor.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void insertUpdate(DocumentEvent e) {
                    observer.editorTextDidChange(editor.getText());
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    observer.editorTextDidChange(editor.getText());
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    observer.editorTextDidChange(editor.getText());
                }
            });
        }

        @Override
        public Component getEditorComponent() {
            return editor;
        }

        @Override
        public void setItem(Object anObject) {
            if (!(anObject instanceof String)) {
                return;
            }
            editor.setText((String)anObject);
        }

        @Override
        public Object getItem() {
            return editor.getText();
        }

        @Override
        public void selectAll() {
            editor.selectAll();
        }

        @Override
        public void addActionListener(ActionListener l) {
            editor.addActionListener(l);
        }

        @Override
        public void removeActionListener(ActionListener l) {
            editor.removeActionListener(l);
        }
    }
}

同样,您可以使用工厂方法来配置预先存在的JComboBox,但这取决于您。

相关问题