0

I have a complex JPanel, that has various nested JPanels as components.

I want to replace one of these nested panels with a new one. But I can't get Top-Level Panel to redraw with new components unless I delete and add them all again:

JPanel topLevel = new JPanel();
JPanel upperPane = new JPanel();
JPanel lowerPane = new JPanel();

JPanel infoPane = new JPanel();

upperPanel.add(infoPane);
topLevel.add(UpperPane);
topLevel.add(lowerPane);

// The above (psuedocode) display nicely. //Now I want to:

infoPane = new JPanel(); //Changed this Panel somehow;

topLevel.revalidate();
topLevel.repaint();

//The above 2 lines do NOT display the new information.

How can I get it to update?

ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • _"How can I get it to update?"_ Use a `CardLayout` instead of trying to _replace_. See [**this example**](http://stackoverflow.com/a/21460065/2587435) – Paul Samsotha Feb 05 '14 at 09:13
  • @peeskillet That does not do what I need as all "cards" need to be created at start. You cannot change the underlying card AFTER you have displayed the first one. – ManInMoon Feb 05 '14 at 09:19
  • Consider posting a [MCTRE](http://stackoverflow.com/help/mcve) – Paul Samsotha Feb 05 '14 at 09:23

2 Answers2

0

If you're calling new JPanel() then you are referencing a different point in memory. Therefore, the reference to the old JPanel on upperPanel is lost, and changes made to infoPane after this will not be shown.

Haven't tried it myself, but change infoPane without calling new and that may well be your answer.

Ben
  • 346
  • 1
  • 8
  • Unfortunately I can't as I am using library that forces me to do a new to get a Chart. – ManInMoon Feb 05 '14 at 09:16
  • Then simply repeat `upperPanel.add(infoPane);`. When you call `new` then you are no longer referring to the `JPanel` that is shown, so any changes made will be invisible until you add it. – Ben Feb 05 '14 at 09:17
  • BUT. I would need to remove it from upperPanel first. The problem is that I get Screen Glitches sometimes between removing and adding again - even if they are consecutive lines – ManInMoon Feb 05 '14 at 09:37
0

FWIW, the design paradigm you describe seems awkward to me. Wouldn't it be more straightforward to continue with that original infoPane instance and simply redraw its contents? Then your topLevel panel doesn't have to be involved with the change.

So, have your infoPane display whatever it is that your library gives you when you call new(), and just use new() to put new WhateverItIs() inside your infoPane.

Would that work in your context?

Hope this helps.

Mark Phillips
  • 1,451
  • 13
  • 17
  • That would be wat I want - but unfortunately if use use "new"anywhere lower down in the nesting, the higher level panels do not recognise the change. – ManInMoon Feb 05 '14 at 10:09