views:

39

answers:

4

I am trying to figure out why my JComponent refreshes when I manually drag my window, but it doesn't refresh when I call repaint or revalidate. The data is ready to be displayed, but it just won't show until I manually resize. Can anybody give some suggestions about what I can do or does this sound like it isn't a Swing problem since I tried repaint and revalidate?

One weird things I've noticed is that if I have this code:

sp.setSize(sp.getSize().width, sp.getSize().height+1);
sp.setSize(sp.getSize().width, sp.getSize().height-1);

If the first line is used, then the JComponent will refresh itself. If I use none or both of these lines it will not, which seems bizarre to me.

I am basically just putting a JPanel in a JInternalFrame in a JDesktopPane. There are two main functions for what I am trying to do. One adds the new JPanel and the other tries to refresh it so the new data will show:

public void addNewSP()
 {
sp = new JInternalFrame("SP");
  sp.setClosable(true);
  sp.setLocation(700, 400); //this should be changed to something based on screen size
  sp.setResizable(true);
  sp.add(popUp);
  this.parentContainer.add(sp, JLayeredPane.DRAG_LAYER);
  sp.pack();
  sp.show();
  sp.setSize(500, 500);
  sp.setPreferredSize(new Dimension(500, 500));
}

public void refreshSP()
 {

  sp.repaint();
  sp.validate();
  sp.repaint();

  sp.validate();
  parentContainer.validate();
  parentContainer.repaint();

  sp.setSize(sp.getSize().width, sp.getSize().height+1);
  sp.setSize(sp.getSize().width, sp.getSize().height-1);
  }
 }

BTW parentContainer is the JDesktopPane

A: 

if your are modifying container's subcomponents you should call jcomponent.validate();

feridcelik
I am calling revaliadte, but it doesn't seem to fix the problem. I am still stumped as to why manually resizing the JPanel fixes the problem.
Try calling `validate` instead of `revalidate`? Might work...
Tikhon Jelvis
I changed them all to validate, but no change. Thanks for the suggestion though.
A: 

When changing the container's content, you have to call both:

  • revalidate() to make it recompute the layout for its content
  • repaint() to request a repaint for this container
jackrabbit
A: 

but it just won't show until I manually resize.

We don't know the context of your question, which is why a SSCCE should always be posted as suggested earlier.

In general a JComponent, does not have a preferred size, so I'm guessing Swing doesn't think it needs to paint the component. When you resize the frame, chances are the component was added to the center of a BorderLayout so it automatically gets sized to fill the entire space of the frame.

The solution is to give your component a "preferred size" so that any layout manager can use this information to display the component properly.

camickr
Preferred Size didn't change anything unfortunately
A: 

I assume parentContainer is the JDesktopPane?
What kind of changes are you making to sp that are not showing up?

Changing the size of sp will cause Swing to repaint from scratch. That's why the setSize() is fixing the display.

Most likely, the changes you are making are either not happening on the EDT, or are not invalidating the right container. For example, if you change the visibility of a component in sp, you'll need to call sp.invalidate() to rerun the layout manager.

Have you checked that you're only changing components (or their models) on the EDT?

A quick test for that is to run with the Substance LAF as it will complain if you change things on another thread.

Devon_C_Miller