1

I want to add at runtime a JLabel under the purple pane which contains already some components, say under the progress bar: enter image description here

Here is the structure of the elements:

enter image description here

And this is my code which is issued when an event occurs (it's getting there i checked with debug) :

    jPanel1.add(new JLabel("Stack Overflow"));
    jPanel1.revalidate();
    jPanel3.revalidate();

I'm not seeing any changes whatsoever and have no clue where to go from here. When i put a textarea in the purple pane and then call it's setText() method at the same place i try to add the JLabel component it works.

Tom
  • 9,275
  • 25
  • 89
  • 147
  • 1
    Which layout manager are you using? It's possible that the new component ends up underneath the others. – Paul Tomblin Jun 27 '12 at 23:48
  • @PaulTomblin i changed the layout to border layout and now i see it behind the other label! – Tom Jun 27 '12 at 23:54
  • I'll wait for an answer that would suggest which layout to choose – Tom Jun 27 '12 at 23:59
  • If you just want the components stacked above each other, I'd use a BoxLayout, or just to make it easier, replace JPanel1 with a Box. – Paul Tomblin Jun 28 '12 at 00:01

2 Answers2

3

You need to learn more about layouts and how they work. I strongly suggest you read the entire layout manager tutorial, since understanding layouts are the solution here, and just using BorderLayout isn't the way to solve it. You'll likely want to nest layouts, perhaps using BorderLayout for the overall GUI, and having a central JPanel use BoxLayout to allow you to stack components on top of each other inside of it. Then perhaps add this JPanel to the main JPanel that uses BorderLayout in the BorderLayout.CENTER position.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Just a hunch, but maybe you need to call repaint() in addition to revalidate()

Java Swing revalidate() vs repaint()

Community
  • 1
  • 1
anttix
  • 7,709
  • 1
  • 24
  • 25