Tôi có thể chỉnh sửa JComboBox
và muốn thêm giá trị cho nó từ đầu vào của nó, es khi tôi gõ một cái gì đó trong JComboBox
và nhấn Enter Tôi muốn văn bản xuất hiện trong JComboBox
danh sách:có thể chỉnh sửa JComboBox
public class Program extends JFrame
implements ActionListener {
private JComboBox box;
public static void main(String[] args) {
new Program().setVisible(true);
}
public Program() {
super("Text DEMO");
setSize(300, 300);
setLayout(new FlowLayout());
Container cont = getContentPane();
box = new JComboBox(new String[] { "First", "Second", "..." });
box.setEditable(true);
box.addActionListener(this);
cont.add(box);
}
@Override
public void actionPerformed(ActionEvent e) {
box.removeActionListener(this);
box.insertItemAt(box.getSelectedItem(), 0);
box.addActionListener(this);
}
}
không may khi tôi nhấn nhập hai giá trị được chèn vào thay vì một giá trị.
Tại sao?
Tôi đã sửa đổi bài đăng của bạn, vui lòng hoàn nguyên nếu ... – mKorbel