如何看到jcombobox的弹出菜单

时间:2023-01-29 12:23:09

How to visible popup menu of editable JComboBox. That is when I enter any text in JComboBox it should display its popup list items. Here is my code. I have added a key Listner in which I have added invoked showpopup() and setpopupvisible(true). But it does nothing.

如何显示可编辑JComboBox的弹出菜单。那是当我在JComboBox中输入任何文本时,它应该显示其弹出列表项。这是我的代码。我添加了一个键Listner,其中添加了调用的showpopup()和setpopupvisible(true)。但它什么都不做。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

private JPanel contentPane;
private JComboBox comboBox;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test frame = new Test();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    comboBox = new JComboBox(new Object[] {"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
    comboBox.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent arg0) {
            comboBox.showPopup();
            comboBox.setPopupVisible(true);
        }
    });
    comboBox.setEditable(true);
    comboBox.setBounds(10, 11, 414, 20);
    contentPane.add(comboBox);
}
}

1 个解决方案

#1


1  

I did some work and somehow made it work. But this is not the recommended way. Please add following code after

我做了一些工作,并以某种方式使它工作。但这不是推荐的方式。请在之后添加以下代码

comboBox = new JComboBox(new Object[] {"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});

comboBox = new JComboBox(new Object [] {“Ester”,“Jordi”,“Jordina”,“Jorge”,“Sergi”});

Component[] comps = comboBox.getComponents();
for(Component comp : comps){
    if(comp instanceof CellRendererPane){
        JComboBox co = (JComboBox) ((CellRendererPane)comp).getParent();
        co.getEditor().getEditorComponent().addKeyListener(new KeyAdapter()      {
            @Override
            public void keyPressed(KeyEvent arg0) {
                comboBox.showPopup();
                comboBox.setPopupVisible(true);
            }
        });
    }
}

This is working fine.But it's better to use decorator and have new components, also have a look into following article,

这工作正常。但是最好使用装饰器并有新的组件,也可以看看下面的文章,

JComboBox autocomplete

JComboBox自动完成

#1


1  

I did some work and somehow made it work. But this is not the recommended way. Please add following code after

我做了一些工作,并以某种方式使它工作。但这不是推荐的方式。请在之后添加以下代码

comboBox = new JComboBox(new Object[] {"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});

comboBox = new JComboBox(new Object [] {“Ester”,“Jordi”,“Jordina”,“Jorge”,“Sergi”});

Component[] comps = comboBox.getComponents();
for(Component comp : comps){
    if(comp instanceof CellRendererPane){
        JComboBox co = (JComboBox) ((CellRendererPane)comp).getParent();
        co.getEditor().getEditorComponent().addKeyListener(new KeyAdapter()      {
            @Override
            public void keyPressed(KeyEvent arg0) {
                comboBox.showPopup();
                comboBox.setPopupVisible(true);
            }
        });
    }
}

This is working fine.But it's better to use decorator and have new components, also have a look into following article,

这工作正常。但是最好使用装饰器并有新的组件,也可以看看下面的文章,

JComboBox autocomplete

JComboBox自动完成