1

I have a JPanel and a Jlist, when the user selects a different item in the list, the corresponding component will be added to the panel, while the previous component will be removed. Here is the portion of the code:

depictorPanel.removeAll();
depictorPanel.invalidate();
depictorPanel.repaint();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
depictorPanel.add(viewer, constraints);
viewer.setSize(depictorPanel.getSize());
depictorPanel.invalidate();
depictorPanel.repaint();

where the depictorPanel is the JPanel, and the viewer is the component (btw. the viewer is of type VisualizationView coming from the JUNG library, which inherits also JPanel).

When I resize the JPanel (done by resizing the whole window, such that all the components in the window are resized), or minimize the window and restore it, the viewer component is gone, I can tell since I set different background colors of the depictorPanel and the viewer.

I have also handled the componentResized listener of the depictorPanel to invalidate and repaint the viewer, but no luck.

Weixiang Guan
  • 457
  • 2
  • 6
  • 16
  • 3
    For better help & sooner post an [SSCCE](http://sscce.org/) – Reimeus Sep 26 '13 at 16:45
  • There are two other strategies you might pursue. 1) If `viewer` is often shown, with different details, instead just have one `viewer`, but refresh the details. 2) If the `viewer` might also be an `editor` or `generalSettings` (i.e. a limited (less than 50) number of specific components), use a `CardLayout`. – Andrew Thompson Sep 27 '13 at 06:00
  • @AndrewThompson Thank you for the alternatives! You are right, I think using a single viewer but different details is a better approach. Using a CardLayout is not preferable, since different graphs to be displayed could be any (large) number. Besides, I still want to know why my current code does not work. – Weixiang Guan Sep 27 '13 at 07:51
  • *"I still want to know why my current code does not work."* Those that are expert in the subject are possibly still waiting on that SSCCE as advised by @Reimeus. ;) – Andrew Thompson Sep 27 '13 at 08:27
  • @AndrewThompson OK. I am wondering why nowadays many people recommend SSCCE instead of posting answers directly here. In this case, are we transferring the platform to SSCCE? Then stackoverflow would be unusable. – Weixiang Guan Sep 27 '13 at 08:31
  • *"SSCCE instead of posting answers directly here."* Huh? Given an SSCCE (if it exists) is *supposed* to be posted directly here - as an [edit to the question](http://stackoverflow.com/posts/19033949/edit), that collection of words reads like nonsense (to me). Can you explain it another way? BTW - I typically find it quite valuable to add an SSCCE in my [own questions](http://stackoverflow.com/users/418556/andrew-thompson?tab=questions), so I can vouch for that advice from personal experience ( entirely separate from having authored the document.. ;) ). – Andrew Thompson Sep 27 '13 at 08:42
  • @AndrewThompson OK I see, you mean I can post my code in SSCCE and add a reference in my question, so that people can try out the code directly, right? I mark it, I will use it in my future questions. Thx. – Weixiang Guan Sep 27 '13 at 11:58
  • 1
    @WeixiangGuan No, you should read what an SSCEE is (not a platform): http://sscce.org/ – sdasdadas Sep 27 '13 at 16:51
  • @sdasdadas I finally got that! SSCCE means I should post a program that fulfills SSCC in my question! Because the requirements are specified in another website, I mistook the concept. – Weixiang Guan Sep 28 '13 at 11:05

2 Answers2

1

I finally found a working method that uses a layout manager (GridBagLayout):

GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1;
constraints.weighty = 1;
depictorPanel.add(currentViewer, constraints);
depictorPanel.revalidate();

This code makes the effect that I was expecting. I think my problem was not understanding each individual layout manager that well (since I am relatively new to Java).

Thank you all guys! I do learn something about Java through this question (and SSCCE) :-)

Weixiang Guan
  • 457
  • 2
  • 6
  • 16
0

I found a workaround to my problem, that is, manage the layout manually without using any layout manager (set the location and size manually and resize when the depictorPanel is resized).

Weixiang Guan
  • 457
  • 2
  • 6
  • 16
  • *"I found a workaround to my problem.."* The work-around will create 10 new problems. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Sep 27 '13 at 12:07
  • @AndrewThompson I understand what you mean, indeed I would like to use the layout managers if they work! But as I heard, Java is supposed to be compiled once and run anywhere, so I assume whatever I do, if the code works here, it should also work there. – Weixiang Guan Sep 28 '13 at 11:10
  • *"Java is supposed to be compiled once and run anywhere"* People have been heard to say, in criticism, that Java instead is 'compile once, *test everywhere*'. The point you seem not get, is that 'layout managers' ***is*** the Java way to provide that 'compile once, run anywhere' philosophy with laying out components, while a `null` layout manager is ..the opposite, that will break on a variety of implementations. You should try and figure out how to provide the GUI seen in [this answer](http://stackoverflow.com/a/5630271/418556) when using no layout. It shows the power of using them. – Andrew Thompson Sep 28 '13 at 12:30