Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   JComboBox with DefaultComboBoxModel doesn't allow to add or remove (http://www.velocityreviews.com/forums/t954713-jcombobox-with-defaultcomboboxmodel-doesnt-allow-to-add-or-remove.html)

Rotariu Mihai 11-21-2012 12:27 PM

JComboBox with DefaultComboBoxModel doesn't allow to add or remove
 
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().addKey Listener(
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().reques tFocus();
//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 11-21-2012 02:31 PM

Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove
 
On Wed, 21 Nov 2012 04:27:15 -0800 (PST), Rotariu Mihai
<rotariu.mihai7@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 11-21-2012 06:41 PM

Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove
 
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 11-22-2012 01:45 AM

Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove
 
In article <6b564835-30c6-4fd7-86fb-d89693675be4@googlegroups.com>,
Rotariu Mihai <rotariu.mihai7@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 11-22-2012 12:03 PM

Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove
 
On Wednesday, 21 November 2012 19:41:10 UTC+1, markspace wrote:
> 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





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/1...-add-or-remove

Knute Johnson 11-22-2012 06:37 PM

Re: JComboBox with DefaultComboBoxModel doesn't allow to add or remove
 
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/1...-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


All times are GMT. The time now is 05:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.