Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JComboBox with DefaultComboBoxModel doesn't allow to add or remove

62 views
Skip to first unread message

Rotariu Mihai

unread,
Nov 21, 2012, 7:27:15 AM11/21/12
to
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!

Roedy Green

unread,
Nov 21, 2012, 9:31:41 AM11/21/12
to
On Wed, 21 Nov 2012 04:27:15 -0800 (PST), Rotariu Mihai
<rotariu...@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>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.
--
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish
as couch potatoes who hire others to go to the gym for them.

markspace

unread,
Nov 21, 2012, 1:41:06 PM11/21/12
to
On 11/21/2012 4:27 AM, Rotariu Mihai wrote:
> 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

John B. Matthews

unread,
Nov 21, 2012, 8:45:48 PM11/21/12
to
In article <6b564835-30c6-4fd7...@googlegroups.com>,
Rotariu Mihai <rotariu...@gmail.com> wrote:

> This is my first post and I think I am doing it right.
> [...]

Cross-posted here: <http://stackoverflow.com/q/13491053/230513>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Rotariu Mihai

unread,
Nov 22, 2012, 7:03:16 AM11/22/12
to
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/13491053/jcombobox-with-defaultcomboboxmodel-doesnt-allow-to-add-or-remove

Knute Johnson

unread,
Nov 22, 2012, 1:37:42 PM11/22/12
to
On 11/22/2012 4:03 AM, Rotariu Mihai wrote:
>>
>> 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/13491053/jcombobox-with-defaultcomboboxmodel-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);
}
});
}
}

--

Knute Johnson
0 new messages