JComboBox with DefaultComboBoxModel doesn't allow to add or remove

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!
 
R

Roedy Green

model.removeAllElements(); and model.addElement(s); is not workin

What are the symptoms it is not working?

Perhaps you need a manual repaint after you have done your changes.
 
M

markspace

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();


Why do you have to do that? If you're using the right kind of model,
shouldn't removeAllItems() call the model appropriately?

Please, please, please show us an SSCCE -- a short, self-contained
compilable example. We can't really tell what is going on unless you
provide one.

sscce.org
 
R

Rotariu Mihai

Why do you have to do that? If you're using the right kind of model,

shouldn't removeAllItems() call the model appropriately?



Please, please, please show us an SSCCE -- a short, self-contained

compilable example. We can't really tell what is going on unless you

provide one.



sscce.org




I updated my post on another java forum where I have a very simple example why is not working. If you have the time and the patience please check

http://stackoverflow.com/questions/...ltcomboboxmodel-doesnt-allow-to-add-or-remove
 
K

Knute Johnson

I updated my post on another java forum where I have a very simple
example why is not working. If you have the time and the patience
please check

http://stackoverflow.com/questions/...ltcomboboxmodel-doesnt-allow-to-add-or-remove

That doesn't really cut it as an SSCCE because it doesn't compile. But
I was intrigued enough to write one for you.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class test extends JPanel {
static final String[] items = {
"one","two","three","four","five","six" };

private final Vector<String> vector = new Vector<>();
private final JComboBox<String> box;

public test() {
box = new JComboBox<String>(vector);
box.setEditable(true);
for (String item: items)
box.addItem(item);
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String item = (String)box.getSelectedItem();
// if item is not in list and item is not empty
if (!vector.contains(item) && !item.equals("")) {
box.addItem(item);
// attempt to blank entry field
Component c = box.getEditor().getEditorComponent();
if (c instanceof JTextComponent)
((JTextComponent)c).setText("");
}
}
});

JButton rem = new JButton("Remove All");
rem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
vector.removeAllElements();
Component c = box.getEditor().getEditorComponent();
if (c instanceof JTextComponent)
((JTextComponent)c).setText("");
}
});

add(box);
add(rem);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("test");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
test t = new test();
f.add(t,BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top