java JTextField 中的下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1216357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drop down list within JTextField
提问by C.L.S
I wnt that when i enter some letter in the textfield then the related items should be picked up from my database and should appear as a drop down list. For Example: I typed 'J' in text Field, in my database is having names such as {"Juby','Jaz','Jasmine','Joggy'....} Theses names should appear as a list. So that i could select one from them.and so on for other leters as well. Is there any predefined control in awt?? Thnx
我认为当我在文本字段中输入一些字母时,应该从我的数据库中选取相关项目,并应显示为下拉列表。例如:我在文本字段中输入了“J”,在我的数据库中有诸如 {"Juby','Jaz','Jasmine','Joggy'....} 这些名字应该显示为一个列表。所以我可以从他们中选择一个。等等。其他字母也一样。awt中有任何预定义的控件吗??Thnx
回答by OMG Ponies
Why not just use a JComboBox? By default, when the user types a keystroke in a read-only combobox and an item in the combobox starts with the typed keystroke, the combobox will select that item.
为什么不直接使用 JComboBox?默认情况下,当用户在只读组合框中键入按键并且组合框中的项目以键入的按键开头时,组合框将选择该项目。
Or you could set the JComboBox to be editable using setEditable(true), and use a KeySelectionManager. The link explains selecting an item in a JComboBox component with multiple keystrokes.
或者您可以使用 将 JComboBox 设置为可编辑setEditable(true),并使用KeySelectionManager。该链接解释了如何通过多次击键在 JComboBox 组件中选择一个项目。
回答by C.L.S
This is a small example implementing what ( i think) you asked for.. the database in this example is a vector of strings.
这是一个小例子,实现了(我认为)你所要求的……这个例子中的数据库是一个字符串向量。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Answer extends JFrame {
public static final int MAXITEMS = 100;
JPanel panel = new JPanel();
JTextField textField = new JTextField(10);
String[] myDataBase = { "Juby", "Jaz", "Jasmine", "Joggy", "one", "dog","cat", "parot" };
String[] listItems;
JList theList = new JList();
public Answer() {
this.add(panel);
panel.setPreferredSize(new Dimension(500, 300));
panel.add(textField);
panel.add(theList);
textField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
String compareString = ("" + textField.getText() + ke.getKeyChar());
listItems = new String[MAXITEMS];
if (compareString.trim().length() > 0 ){
int counter = 0;
for (int i = 0; i < myDataBase.length; i++) {
if (counter < MAXITEMS) {
if (myDataBase[i].length() >= compareString.length() &&
myDataBase[i].substring(0, compareString.length()).equalsIgnoreCase(compareString)) {
listItems[counter] = myDataBase[i];
counter++;
}
}
}
}
theList.setListData(listItems);
}
});
}
public static void main(String[] args) {
final Answer answer = new Answer();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
answer.pack();
answer.setVisible(true);
}
});
}
}
回答by akarnokd
One option is to use GlazedLists, as it has some support for auto-completion.
一种选择是使用GlazedLists,因为它对自动完成有一定的支持。

