The content of my JComponent is only updated after manually resizing

I am trying to understand why my JComponent is updated when I manually drag my window, but it does not update when I call redraw or re-evaluate. The data is ready for display, but it just won’t be displayed until I manually resize it. Can anyone give any suggestions on what I can do, or does it sound like it is not a problem with Swing since I tried redrawing and revalidate?

One weird thing I 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 JComponent will be updated. If I do not use either or both of these lines, it will not, which seems strange to me.

Basically, I just put the JPanel in a JInternalFrame in JDesktopPane. There are two main functions for what I am trying to do. One adds a new JPanel, and the other tries to update it so that new data is displayed:

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's potential parent is JDesktopPane

+3
source share
4 answers

When changing the contents of the container, you must call both:

  • revalidate()to double-check the layout of its contents.
  • repaint()to request a redraw for this container.
+7
source

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

We do not know the context of your question, so SSCCE should always be published, as suggested earlier.

JComponent , , Swing . , , BorderLayout, , .

, " ", .

+1

, jcomponent.validate();

0

, parentContainer - JDesktopPane?
sp, ?

sp Swing . setSize() .

, , , EDT, . , sp, sp.invalidate(), .

, ( ) EDT?

- Substance LAF, , .

0

Source: https://habr.com/ru/post/1762338/


All Articles