1

I made a class that extends jframe. I have a border layout for the jframe and some components and a jpanel with absolute layout to hold some buttons and a jcombobox. The combo box holds some values from a table. And the table takes the values from a mysql query. I delete or add some records through the buttons and then I update the table.

All works great except the combo box that doesn't refresh the values. In order to work I have to close the jframe and open it again. I read many articles and tutorials about repaint etc... but nothing seems to work in my case. The action listener is also the same class that extends the jframe.

Any way to do it ? Please keep it simple as I am new to java programing. Thank you!

kleopatra
  • 49,346
  • 26
  • 88
  • 189
Vagelism
  • 601
  • 1
  • 13
  • 27
  • 2
    Put up some code to see what's going on. An SSCCE would be ideal (http://sscce.org) – Guillaume Polet Mar 09 '12 at 07:46
  • Are you reloading the values in the combo box after your operation ? – prajeesh kumar Mar 09 '12 at 07:51
  • have you tried `revalidate()` on the JComboBox after you changed its model? – Guillaume Polet Mar 09 '12 at 08:03
  • @Vagelism : Please see [CellEditorListener](http://docs.oracle.com/javase/7/docs/api/javax/swing/event/CellEditorListener.html). I don't know much about JTables, but seems like this can be used to update the values of your `JComboBox` as the contents of the `JTable` are updated. Or the other approach can be, as you get values from your `Database` and you putting them to your `JTable` you can add them to a `List`, which you can use to update the contents of your `JComboBox`, once you are done updating `JTable`. – nIcE cOw Mar 09 '12 at 11:02

1 Answers1

3

Did you hear about SwingUtilities.invokeLater(new Runnable()) or java.awt.EventQueue.invokeLater(new Runnable()) ?

Reading this thread could be useful: Concurrency in Swing

Sample code snippet that you can add to your main method:

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        JFrame frame = new JFrame();
        frame.setVisible(true);
                ...
    }
});
Juvanis
  • 25,000
  • 3
  • 61
  • 84
  • Ok...I read it and to be honest I dont understand many things. Any example on how to use this code? – Vagelism Mar 09 '12 at 08:02
  • @GagandeepBali i think your material is better, i just wanted to give an idea to the OP. – Juvanis Mar 09 '12 at 08:06
  • @Vagelism : You need to read about SwingWorker too, how to use SwingWorker to access data from Database and show it in your GUI. What ever changes you are making to your GUI at runtime, needs to be done on Event Dispatcher Thread. – nIcE cOw Mar 09 '12 at 08:09
  • Yes This is what I do now but not understanding many things :( – Vagelism Mar 09 '12 at 08:25