JComboBox with DefaultComboBoxModel doesn't allow to add or remove

Discussion in 'Java' started by Rotariu Mihai, Nov 21, 2012.

  1. 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!
    Rotariu Mihai, Nov 21, 2012
    #1
    1. Advertising

  2. Rotariu Mihai

    Roedy Green Guest

    On Wed, 21 Nov 2012 04:27:15 -0800 (PST), Rotariu Mihai
    <> 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.
    Roedy Green, Nov 21, 2012
    #2
    1. Advertising

  3. Rotariu Mihai

    markspace Guest

    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
    markspace, Nov 21, 2012
    #3
  4. In article <>,
    Rotariu Mihai <> 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>
    John B. Matthews, Nov 22, 2012
    #4
  5. 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/...ltcomboboxmodel-doesnt-allow-to-add-or-remove
    Rotariu Mihai, Nov 22, 2012
    #5
  6. 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/...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);
    }
    });
    }
    }

    --

    Knute Johnson
    Knute Johnson, Nov 22, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Casey Hawthorne
    Replies:
    2
    Views:
    419
    Darryl Pierce
    Aug 13, 2005
  2. RC
    Replies:
    2
    Views:
    7,899
    Paul Hamaker
    Feb 22, 2006
  3. Jeff
    Replies:
    2
    Views:
    934
    clintonG
    Sep 19, 2006
  4. Ryan Taylor
    Replies:
    1
    Views:
    675
    Ryan Taylor
    Sep 9, 2004
  5. Rotariu Mihai
    Replies:
    1
    Views:
    244
    Rotariu Mihai
    Nov 21, 2012
Loading...

Share This Page