这是我的第一篇文章,我认为我做得对。
我有一个程序从AutoComplete jComboBox获取用户输入,然后发送要存储到文本文件中的输入。(使用库glazedlists_java15 / 1.8.0完成AutoComplete。)
使用自动填充功能后,我必须将jComboBox设置为DefaultComboBoxModel。
当用户按 Enter键时, jComboBox应使用键盘输入的新项目更新列表,因此用户可以看到最后一个jComboBox列表中的类型项。
通过删除jComboBox中的所有项和然后重新插入来完成。
问题是之前具有自动完成功能我可以说 jComboBox1.removeAllItems(); 但现在因为模型我必须使用 model.removeAllElements();
public class Test {
final static DefaultComboBoxModel model = new DefaultComboBoxModel();
static JComboBox c = new JComboBox(model);
private static final long serialVersionUID = 1L;
private static JButton b = new JButton();
static JFrame f = new JFrame();
/**
* @param args
*/
public static void TestFrame() {
String[] a = {"hi1" , "hi2", "hi3", "hi4","hi5"};
AutoCompleteSupport support = AutoCompleteSupport.install(c,
GlazedLists.eventListOf(a));
JPanel test = new JPanel();
test.add(b);
test.add(c);
model.addElement(a);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.removeAllElements();
}
});
f.add(test);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(500,500);
}
问题是 model.removeAllElements(); 和 model.addElement(s); 无法正常工作,因此我无法更新jComboBox。你能否抽出时间帮助我找到解决方案。谢谢!
答案 0 :(得分:2)
编辑:
我不知道你的问题出在哪里,这对我来说非常有用
final DefaultComboBoxModel model = new DefaultComboBoxModel();
JComboBox c = new JComboBox(model);
private static final long serialVersionUID = 1L;
private JButton b = new JButton();
public TestFrame() {
JPanel test = new JPanel();
test.add(b);
test.add(c);
model.addElement("hi");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.removeAllElements();
}
});
this.add(test);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(500,500);
}
也许你没有达到你的keylistener
答案 1 :(得分:0)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;
public class TestFrame
{
private static JComboBox c = new JComboBox();
private static JButton b = new JButton();
private static JFrame f = new JFrame();
private static String[] a = {"hi1", "hi2", "hi3", "hi4", "hi5"};
public static void TestFrame()
{
final EventList<String> items = GlazedLists.eventListOf(a);
AutoCompleteSupport.install(c, items);
JPanel test = new JPanel();
test.add(b);
test.add(c);
c.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c = (JComboBox) e.getSource();
if (e.getActionCommand().equals("comboBoxEdited"))
{
items.clear();
}
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
items.clear();
}
});
f.add(test);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(500, 500);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TestFrame();
}
});
}
}