Actualizar un JComboBox

I have a combo box as you can see in the code that takes the values from a table. So after I click ok the values of the table change. How can I see these new values to the compobox without to close and open the jframe ? I made a lot of study today about java.awt.EventQueue.invokeLater and othe staff but I can not make it work, I am new to java and to programing general. So here is the code:

public class Compo extends JFrame implements ActionListener
{//start of class Compo 
//start of variables
private JComboBox<String> CompoBox;
private String array[];
private JButton okButton;
private JPanel panel;
//end of variables
public Compo ()
{//start of Compo method
  super("Example"); 
    panel=new JPanel(null);

   //table =  new String[3];
   array= new String[3];
   array[0]="alpha";
   array[1]="beta";
   array[2]="charlie";

   CompoBox= new JComboBox<>(array);
   CompoBox.setBounds(50, 70, 100, 20);
   panel.add(CompoBox);
   okButton=new JButton("ok");
   okButton.setBounds(50, 120, 70, 30);
   okButton.setActionCommand("ok");
   okButton.addActionListener(this);
   panel.add(okButton);
   add(panel);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setSize(200, 200);
   setVisible(true);


}//end of compo method
@Override
public void actionPerformed(ActionEvent event)
    {//start of actionperformed
    String testString=event.getActionCommand();
    if (testString.equals("ok"))
    {//start of if
        for (int i = 0; i < array.length; i++)
        {
             String sample= array[i];
             array[i]=sample+"aa";

        }
    }//end of if
}//end of aciton performed


}//end of class Compo 

preguntado el 09 de marzo de 12 a las 14:03

How can I imporve it in 3 days that I am subscribed here? -

por favor, aprenda las convenciones de nomenclatura de Java y cúmplalas -

The problem now is that the button does its job for only the first click! -

2 Respuestas

this should be the answer you are looking for, hope you'll accept it:

    if (testString.equals("ok")) {
        CompoBox.removeAllItems();
        for (int i = 0; i < array.length; i++) {
            String sample = array[i];
            CompoBox.addItem(sample + "aa");
        }
    }

btw. the generic comqo-pox is done like this:

CompoBox = new JComboBox<String>(array);

respondido 09 mar '12, 14:03

I will try it just write now!Thank you! - Vagelismo

Puedes usar el addItem(...) y removeItem(...) methods on the combobox directly or provide your own ComboBoxModel and change the data in that model.

Although I'd generally like to use the latter option (i.e. my own model), for your skill level I'd suggest starting with the add/remove methods first.

Later you might want to work on the default model (which is of class DefaultComboBoxModel e implementos MutableComboBoxModel). Keep in mind though, that you'd need a cast and be sure you get a model of the correct type in this case.

respondido 09 mar '12, 14:03

Thank you for your answer. Finally it works with the code that is up. - Vagelismo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.