I have a JDialog dlg, created by a JFrame frm, that contains a JList list. When I modify the list (through the ListModel), the list itself is repainted but not the JDialog. This means that, if I delete a line, the list remains with an empty line while if I add a line, this new line won't be shown (because there is no space in the dialog) until I manually force repainting of dlg (doubleclicking in frm).
Following advices in this post : How to make repaint for JDialog in Swing?
and in this post: Force repaint after button click
I tried to call, from my controller class (which is where updates to list are made), the following line:
SwingUtilities.getWindowAncestor(dlg).repaint();
but it didn't work. I also tried:
dlg.repaint();
No luck either...
Any clue? Thank you very much.
EDIT: The organization of my classes is as follows: a controller class that contains a reference to the main JFrame, frm. I also extended JDialog into MyDialog, which contains a JList. When a doubleclick on frm is detected, I show the instance of MyDialog (or create, if it is the first time I show it) and the JList is filled with the data passed to the DefaultListModel. MyDialog is painted so that the list has only the space that it needs. Now, when a specific event is detected by the controller, I get the specific MyDialog, get the ListModel from JList and update it. Here the JList is indeed updated, but Dialog remains the same. I use a code like this:
MyDialog dlg = group.getDlg();
if(dlg != null){
DefaultListModel listModel = ((DefaultListModel) dlg.getMyJList().getModel());
listModel.addElement(idStock);
SwingUtilities.getWindowAncestor(dlg).repaint();
}
This doesn't repaint dlg. I also tried:
SwingUtilities.getWindowAncestor(dlg.getMyJList()).repaint();
but it doesn't work.
I checked with the debugger that the lines are actually executed.
I don't have much more code to show, really.....
I think that you going wrong way, define
DefaultListModel
that accesible throught all Java methods and Classes, thisModel
would holds yourObjects
, then putJList
to theJDialog
orJOptionPane
, for example