Link to home
Start Free TrialLog in
Avatar of boardtc
boardtcFlag for Ireland

asked on

Refreshing DefaultListModel does not repaint

I have a popup JDialog descendant. I right click to open the popup and that displays fine. When the dialog is refreshed by clicking the option again is has the effect of clearing the list. I show the code outline below. If I debug through the code _or_ include a simple showMessageDialog within the loop the dialog repaints correctly.

Any ideas what's going on?

public class MyDialog extends JDialog {

    private MyList sampleModel;
   
    ...

    public void Show(String msg){
        MyList.clear();
        while(msg.length()>0){
          // populate list
          ...
          // refreshes correctly if include this message
          //JOptionPane.showMessageDialog(null,String.format("msg = %s",msg));
        }
        pack();
        setVisible(true);
    }
}

Thanks, Tom.
Avatar of Mayank S
Mayank S
Flag of India image

Try calling revalidate ()
Avatar of boardtc

ASKER

it's not recognised...
Sorry validate ()
Avatar of boardtc

ASKER

Tried that already, makes no difference.
Avatar of boardtc

ASKER

Looking at the console when I refresh I get :

Exception in thread "AWT-EventQueue-2" java.lang.ArrayIndexOutOfBoundsException: 2
      at javax.swing.plaf.basic.BasicListUI.updateLayoutState(Unknown Source)
      at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(Unknown Source)
      at javax.swing.plaf.basic.BasicListUI.getPreferredSize(Unknown Source)
      at javax.swing.JComponent.getPreferredSize(Unknown Source)
      at javax.swing.ScrollPaneLayout.layoutContainer(Unknown Source)
      at java.awt.Container.layout(Unknown Source)
      at java.awt.Container.doLayout(Unknown Source)
      at java.awt.Container.validateTree(Unknown Source)
      at java.awt.Container.validate(Unknown Source)
      at javax.swing.RepaintManager.validateInvalidComponents(Unknown Source)
      at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)

What does this mean?
Avatar of ForceRs
ForceRs

I'm just guessing, but I think that the parent container for this dialog is not set.  When you show the dialog, include the parent in the call.
mmake sure all gui calls are being made from the event dispatch thread
Avatar of boardtc

ASKER

It's a popup dialog...not sure I follow about a parent container. Are you talking about the layout for the dialog?

I know little about event dispatch thread but my understanding is that any gui actions performed from an event listener are done via this, this would be the case for my right click popup option. I am "refreshing" by just right clicking and selecting the popup again.
>> not sure I follow about a parent container.

Are you not calling this from a JFrame or something? Where do you create an object of the dialog? What do you pass in the constructor? Do you pass the JFrame as the parent?
Avatar of boardtc

ASKER

I've inherited this project. It's called from a JApplet. The dialog object is a public member of the Japplet extension. I know it's far from ideal code. The Japplet reference is passed to the popup JDialog constructor and certain propertied are used to populate values on the popup, etc.
>> The Japplet reference is passed to the popup JDialog constructor

Is there a call to super () there?
Avatar of boardtc

ASKER

Nice one, the constructor did indeed have no super(). But adding it did not make a difference :-(
Meaning which super () overload did you add? Did you pass the JApplet reference as an argument to it?
Avatar of boardtc

ASKER

i just read that the compiler puts in a call to super() if you leave it out. I just tried to pass the JApplet reference but the compiler could not resolve it?
>> the compiler puts in a call to super() if you leave it out

That would be just for the default constructor. For the parameterized constructor, you need to put a call passing the arguments explicitly. JDialog has a constructor JDialog ( Frame owner ). You need to call it like:

public MyDialog ( Frame owner ) // assuming this is your constructor and that the JApplet reference is passed to 'owner'
{
  super ( owner ) ; // calls the constructor in JDialog with the parameter
Avatar of boardtc

ASKER

Thanks, maybe I am doing something brain dead. I have :

public class MyApplet extends JApplet {

public class MyDialog extends JDialog {
...
    public MyDialog (P...MyApplet powner){
       super(powner);

Cannot resolve method super(powner);
Ah, yes it will not work because the JDialog expects a Frame parent whereas an Applet is not a Frame.
Avatar of boardtc

ASKER

yeah...i thought you had cracked it for a minute :-(
Out of interest, does this work in case you run the JDialog on a JFrame instead of a JApplet, with the call to super?
Try with super ( null ) in the JApplet
Avatar of boardtc

ASKER

I have no jframes in the project. Are you suggesting I could wrap the JApplet in a JFrame?
Compiler does not like super ( null )
super ( null ) ;

?
Avatar of boardtc

ASKER

like I say compiler cannot resolve...
Strange - it should accept null as an argument. What is the error message?
Avatar of boardtc

ASKER

reference is ambigous it says...
Avatar of boardtc

ASKER

reference to JDialog is ambiguous, both method JDialog(java.swing.Frame) in javax.swing,JDialog and method JDialog(java.awt.Dialog) in javax.swing.JDialog match
If this helps, this is how I do JDialogs and it seems to work:

public JDialog getDlSaveAs() {
            if(dlSaveAs == null) {
                  //dlSaveAs = new JDialog();
                  JOptionPane opSaveAs = new JOptionPane();
                  dlSaveAs = opSaveAs.createDialog(getParentApplet(), "Save As");
                  dlSaveAs.setSize(536, 125);
                  dlSaveAs.setLocation(300, 323);
                  dlSaveAs.setModal(true);
                  dlSaveAs.setContentPane(getPnSaveAsMain());
            }
            return dlSaveAs;
}
super ( ( JDialog ) null ) ;
Sorry, forgot to show the code to display the sample I posted:

getDlSaveAs().setVisible(true);
Avatar of boardtc

ASKER

Sorry I never got an email to say it had been updated :-( Just saw this now. the compiler like super ( ( JDialog ) null ) ; but the dialog does not popup. The console tells me that there is a null owner window.

ForeRs, thanks for the post but I wasn't sure how your code was relevant to me.
If you're willing, simply change your dialog class to extend JPanel, then use a getter as my sample does.  This may resolve your issue.  It may not be real pretty, but may work.  I, too, have had a lot of troubles trying to extend JDialog (especially when working with applets).  I don't recommend clobbering your existing class; create a quick test using this approach and see if it works.

yourDialog extends JPanel {

private JDialog dlYourDialog = null;

public JDialog getYourDialog(JApplet parentApplet) {
          if(dlYourDialog == null) {
               JOptionPane opYourDialog = new JOptionPane();
               dlYourDialog = opYourDialog.createDialog(parentApplet, "Your Title");
               dlYourDialog.setModal(true);
               dlYourDialog.setContentPane(getYourMainPanel());
          }
          return dlYourDialog;
}

public void Show(String msg){
        MyList.clear();
        while(msg.length()>0){
          // populate list
          ...
          // refreshes correctly if include this message
          //JOptionPane.showMessageDialog(null,String.format("msg = %s",msg));
        }
        this.getYourDialog().pack();
        this.getYourDialog().setVisible(true);
}
}
Avatar of boardtc

ASKER

ok ta. having a play. what's getYourMainPanel?
Avatar of boardtc

ASKER

this.getYourDialog() doesn't fly....i passed the referenvce to the JApplet
Avatar of boardtc

ASKER

The code is too coupled really for this to be straighforward. Also JPanel does not have a title bar.
Sorry:

yourDialog extends JPanel {

private JDialog dlYourDialog = null;
private JPanel mainPanel = null;

public JDialog getYourDialog(JApplet parentApplet) {
          if(dlYourDialog == null) {
               JOptionPane opYourDialog = new JOptionPane();
               dlYourDialog = opYourDialog.createDialog(parentApplet, "Your Title");
               dlYourDialog.setModal(true);
               dlYourDialog.setContentPane(getYourMainPanel());
          }
          return dlYourDialog;
}

public void Show(String msg){
        MyList.clear();
        while(msg.length()>0){
          // populate list
          ...
          // refreshes correctly if include this message
          //JOptionPane.showMessageDialog(null,String.format("msg = %s",msg));
        }
        dlYourDialog.pack();
        dlYourDialog.setVisible(true);
}

private JPanel getYourMainPanel() {
    if(mainPanel == null) {
         mainPanel = new JPanel();
         mainPanel.setLayout(new BorderLayout());
         // you may need to myList in a scrollpane or something
         mainPanel.add(myList, BorderLayout.CENTER);
    }
    return mainPanel;
}
}
Avatar of boardtc

ASKER

so how are dlYourDialog  & getYourDialog linked up?
Avatar of boardtc

ASKER

wow, lots of messing to reinvent the jdialog :-) Has anyone else had problems rereshing a JDialog and found a solution?
ASKER CERTIFIED SOLUTION
Avatar of ForceRs
ForceRs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boardtc

ASKER

wow. that works :-) what's going on?
Avatar of boardtc

ASKER

Within the run I had to do myDialog.Show(msg); it required these 2 variables to be declared final so I had to create temporary final variable and assign the value to them beforehand.
Avatar of boardtc

ASKER

i wrapped the show in the dialog class with the invokeLater, works great. reading about invokeLater at http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)

nice man, thank you.
The clue was that it worked when you added the deliberate pause via tempporary dialog.  Event queue is a real dog's breakfast.  

Glad to help.