英文:
Auto-suggestion ComboBox
问题
我有一个JComboBox,这个JComboBox有很多项。我想在JComboBox上添加“自动建议”功能。
就像这样:我有3个项,“Artpop”,“Born This Way”和“Chromatica”。如果我键入“Way”,然后自动完成会选择“Born This Way”项。如果我键入“matica”,然后自动完成会选择“Chromatica”项。
我在StackOverflow上寻找过,但所有的答案都已过时(我正在使用Swing,而答案是为AWT编写的。当我尝试答案时,出现了多个错误)。
如何在Swing JComboBox上实现自动建议?
英文:
I have a JComboBox and the JComboBox has so many items. I want to add “auto-suggestion” feature on the JComboBox.
Like this: I have 3 items, “Artpop”, “Born This Way”, and “Chromatica”. If I type “Way” then the autocomplete selects the “Born This Way" item. If I type "matica" then the autocomplete selects the "Chromatica" item.
I was looking for it on StackOverflow, but all the answers are outdated (I'm using Swing, while the answers are for AWT. When I do the answers, I've got several errors).
How to make auto-suggestion Swing JComboBox?
答案1
得分: 0
以下是翻译好的内容:
需要一个 JTextField
和一个 JComboBox
来创建自动建议的组合框。
这里是我准备的一个示例。不幸的是,GUI 图片不能正确显示 JComboBox。
我创建了一个简单的 GUI,其中包含一个 JTextField
和一个 JComboBox
。
当您在 JTextField
中输入一个字符时,JComboBox
的选项会被减少到包含该字符的 Strings
。搜索不区分大小写。如果您输入一个 w,它将返回所有带有 w 或 W 的 Strings
。
以下是我使用的完整可运行代码。
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class AutoSuggestionExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new AutoSuggestionExample());
}
private DefaultComboBoxModel<String> comboBoxModel;
private JTextField autoSuggestionField;
private List<String> masterList;
public AutoSuggestionExample() {
String[] masterList = { "Artpop", "Born This Way", "Chromatica" };
this.masterList = Arrays.asList(masterList);
this.comboBoxModel = new DefaultComboBoxModel<>(masterList);
}
@Override
public void run() {
JFrame frame = new JFrame("Auto Suggestion Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
autoSuggestionField = new JTextField(20);
autoSuggestionField.addKeyListener(new AutoSuggestionListener());
panel.add(autoSuggestionField);
JComboBox<String> comboBox = new JComboBox<>(comboBoxModel);
comboBox.setSelectedIndex(0);
panel.add(comboBox);
return panel;
}
public List<String> createSelectedList(String search) {
if (search.isEmpty()) {
return masterList;
}
search = search.toLowerCase();
List<String> selectedList = new ArrayList<>();
for (String s : masterList) {
if (s.toLowerCase().contains(search)) {
selectedList.add(s);
}
}
return selectedList;
}
public void updateComboBox(List<String> selectedList) {
comboBoxModel.removeAllElements();
comboBoxModel.addAll(selectedList);
if (selectedList.size() > 0) {
comboBoxModel.setSelectedItem(selectedList.get(0));
}
}
public class AutoSuggestionListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent event) {
String text = autoSuggestionField.getText() + event.getKeyChar();
updateComboBox(createSelectedList(text));
}
}
}
英文:
It takes a JTextField
and a JComboBox
to create an auto-suggestion combo box.
Here's an example I put together. Unfortunately, the GUI picture doesn't display the JComboBox correctly.
I created a simple GUI with a JTextField
and a JComboBox
.
When you type a character in the JTextField
, the JComboBox
options are reduced to the Strings
that contain that character. The search is not case sensitive. If you type a w, it will return all Strings
with a w or a W.
Here's the complete, runnable code I used.
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class AutoSuggestionExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new AutoSuggestionExample());
}
private DefaultComboBoxModel<String> comboBoxModel;
private JTextField autoSuggestionField;
private List<String> masterList;
public AutoSuggestionExample() {
String[] masterList = { "Artpop", "Born This Way", "Chromatica" };
this.masterList = Arrays.asList(masterList);
this.comboBoxModel = new DefaultComboBoxModel<>(masterList);
}
@Override
public void run() {
JFrame frame = new JFrame("Auto Suggestion Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
autoSuggestionField = new JTextField(20);
autoSuggestionField.addKeyListener(new AutoSuggestionListener());
panel.add(autoSuggestionField);
JComboBox<String> comboBox = new JComboBox<>(comboBoxModel);
comboBox.setSelectedIndex(0);
panel.add(comboBox);
return panel;
}
public List<String> createSelectedList(String search) {
if (search.isEmpty()) {
return masterList;
}
search = search.toLowerCase();
List<String> selectedList = new ArrayList<>();
for (String s : masterList) {
if (s.toLowerCase().contains(search)) {
selectedList.add(s);
}
}
return selectedList;
}
public void updateComboBox(List<String> selectedList) {
comboBoxModel.removeAllElements();
comboBoxModel.addAll(selectedList);
if (selectedList.size() > 0) {
comboBoxModel.setSelectedItem(selectedList.get(0));
}
}
public class AutoSuggestionListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent event) {
String text = autoSuggestionField.getText() + event.getKeyChar();
updateComboBox(createSelectedList(text));
}
}
}
答案2
得分: 0
如Andrew Thompson所评论:“当一个组合框处于焦点状态,并且用户键入包含在其中的某个项目的首字母时,该项目将被选中”
这种行为在公共方法JComboBox.selectWithKeyChar(char keyChar)中定义。
您可以扩展JComboBox
以覆盖selectWithKeyChar(char keyChar)
方法以定义不同的行为。
英文:
As Andrew Thompson commented: "When a combo box is in focus and the user types the first letters of an item it contains, that item will become selected" <br/>
This behavior is defined in the public method JComboBox.selectWithKeyChar(char keyChar).
You can extend JComboBox
to override selectWithKeyChar(char keyChar)
to define a different behavior.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论