Avatar of boardtc
boardtc
Flag 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.
Java

Avatar of undefined
Last Comment
ForceRs

8/22/2022 - Mon
Mayank S

Try calling revalidate ()
boardtc

ASKER
it's not recognised...
Mayank S

Sorry validate ()
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
boardtc

ASKER
Tried that already, makes no difference.
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?
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

mmake sure all gui calls are being made from the event dispatch thread
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.
Mayank S

>> 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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
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.
Mayank S

>> The Japplet reference is passed to the popup JDialog constructor

Is there a call to super () there?
boardtc

ASKER
Nice one, the constructor did indeed have no super(). But adding it did not make a difference :-(
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mayank S

Meaning which super () overload did you add? Did you pass the JApplet reference as an argument to it?
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?
Mayank S

>> 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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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);
Mayank S

Ah, yes it will not work because the JDialog expects a Frame parent whereas an Applet is not a Frame.
boardtc

ASKER
yeah...i thought you had cracked it for a minute :-(
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mayank S

Out of interest, does this work in case you run the JDialog on a JFrame instead of a JApplet, with the call to super?
Mayank S

Try with super ( null ) in the JApplet
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 )
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Mayank S

super ( null ) ;

?
boardtc

ASKER
like I say compiler cannot resolve...
Mayank S

Strange - it should accept null as an argument. What is the error message?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
boardtc

ASKER
reference is ambigous it says...
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
ForceRs

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;
}
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mayank S

super ( ( JDialog ) null ) ;
ForceRs

Sorry, forgot to show the code to display the sample I posted:

getDlSaveAs().setVisible(true);
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ForceRs

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);
}
}
boardtc

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

ASKER
this.getYourDialog() doesn't fly....i passed the referenvce to the JApplet
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
boardtc

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

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;
}
}
boardtc

ASKER
so how are dlYourDialog  & getYourDialog linked up?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
ForceRs

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
boardtc

ASKER
wow. that works :-) what's going on?
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
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.
ForceRs

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.