R
Rotariu Mihai
This is my first post and I think I am doing it right.
I have a program that takes the user input from a AutoComplete jComboBox and then send's the input to be stored into a text file.(AutoComplete is done using the library glazedlists_java15/1.8.0).
After using the Autocomplet feature I had to set the jComboBox to DefaultComboBoxModel.
When the user presses the Enter key, the jComboBox should update the list with the new Item typed from the keyboard, so the user can see the last typed item in the jComboBox list.
This is done by removing all the items from the jComboBox and then inserting them again from the text file.
The problem is that before having the AutoComplete feature I could just say jComboBox1.removeAllItems(); but now because of the model I have to do it with model.removeAllElements();
view plaincopy to clipboardprint?
final javax.swing.JComboBox jComboBox1;
final DefaultComboBoxModel model = new DefaultComboBoxModel();
jComboBox1 = new javax.swing.JComboBox(model);
jComboBox1.setModel(model);
jComboBox1.getEditor().getEditorComponent().addKeyListener(
new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
String ip = (String) jComboBox1.getEditor().getItem();
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
if (ip == null || ip.trim().isEmpty()) {
jComboBox1.setSelectedItem("Please insert a keyword!");
} else {
/*
* Configuration is the class where I have the method that writes to the file and read from the text file
*/
Configuration.WriteIP();
}
/*
* here I remove the items from the comboBox
*/
model.removeAllElements();
/*
* here I set the items from the text file(including the last typed) to the jComboBox
*/
for (Object s : Configuration.getArrayss()) {
model.addElement(s);
}
jComboBox1.setSelectedItem(ip);
//jComboBox1.getEditor().getEditorComponent().requestFocus();
//jComboBox1.addFocusListener(focusHandler);
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
}
The problem is that model.removeAllElements(); and model.addElement(s); is not working so I can not update the jComboBox.
Can you please help me find a solution.
Thanks!
I have a program that takes the user input from a AutoComplete jComboBox and then send's the input to be stored into a text file.(AutoComplete is done using the library glazedlists_java15/1.8.0).
After using the Autocomplet feature I had to set the jComboBox to DefaultComboBoxModel.
When the user presses the Enter key, the jComboBox should update the list with the new Item typed from the keyboard, so the user can see the last typed item in the jComboBox list.
This is done by removing all the items from the jComboBox and then inserting them again from the text file.
The problem is that before having the AutoComplete feature I could just say jComboBox1.removeAllItems(); but now because of the model I have to do it with model.removeAllElements();
view plaincopy to clipboardprint?
final javax.swing.JComboBox jComboBox1;
final DefaultComboBoxModel model = new DefaultComboBoxModel();
jComboBox1 = new javax.swing.JComboBox(model);
jComboBox1.setModel(model);
jComboBox1.getEditor().getEditorComponent().addKeyListener(
new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
String ip = (String) jComboBox1.getEditor().getItem();
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
if (ip == null || ip.trim().isEmpty()) {
jComboBox1.setSelectedItem("Please insert a keyword!");
} else {
/*
* Configuration is the class where I have the method that writes to the file and read from the text file
*/
Configuration.WriteIP();
}
/*
* here I remove the items from the comboBox
*/
model.removeAllElements();
/*
* here I set the items from the text file(including the last typed) to the jComboBox
*/
for (Object s : Configuration.getArrayss()) {
model.addElement(s);
}
jComboBox1.setSelectedItem(ip);
//jComboBox1.getEditor().getEditorComponent().requestFocus();
//jComboBox1.addFocusListener(focusHandler);
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
}
The problem is that model.removeAllElements(); and model.addElement(s); is not working so I can not update the jComboBox.
Can you please help me find a solution.
Thanks!