1

I'm trying to dynamically add some components to a JPanel, but unfortunatelly they don't appear. I only see the ones added in the constuctor.

Updated version (adding a new JPanel, where all the components will be):

public class View extends JPanel {

JPanel panel = new JPanel();
JLabel label;
JLabel labels[];
JButton b1 = new JButton("OK");

public View() {
   this.setLayout(new FlowLayout());
   this.add(panel); // adding a new JPanel
   label = new JLabel("My label");
   panel.add(label);  // adding label to the new panel (this one works)
}


public void showLabels() {
  System.out.println("function showLabels called");

  labels = new JLabel[5];

  for (int i = 0; i < 5; i++) {
      labels[i] = new JLabel("Label: " + i);
      panel.add(labels[i]); // this one doesn't work
  }
  panel.add(b1); // this one doesn't work, too
    this.validate(); // validating this class (parent container)
    panel.validate(); // validating the panel, where all the components are
  }
}

Unfortunatelly nothing changed.

Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181

1 Answers1

6

Call validate() on the parent container, as shown in the Nested Layout Example. Those labels on the lower left are added dynamically. Note that calling pack() might cause the size of the GUI to change, whereas calling validate() won't. If you need to get the GUI to resize - call pack(), else call validate().

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Even when I put all my components at first on a seperat JPanel and then put the new JPanel on the first (parent) JPanel `View` and revalidate and repaint both, nothing changes... – Evgenij Reznik Apr 10 '12 at 18:18
  • *"revalidate and repaint"* ..Note that I mentioned ***neither*** of those methods (for good reason). Perhaps you are thinking of mre's answer (which has now been deleted). – Andrew Thompson Apr 10 '12 at 18:31
  • Ok, I've updated my code so far. Now I'm using `validate()` only, but nothing changed. – Evgenij Reznik Apr 10 '12 at 18:43
  • 1
    *"my code so far"* (sigh) Please understand several things. 1) The term 'SSCCE' above is a link. 2) When I post a link, I expect you to follow it, read it, and ask if you have any questions. 3) Posting that link is (amongst other things) a hint that I won't bother looking at vague descriptions of code, or code snippets. 4) The update does **not** contain an SSCCE. -- Back to you. – Andrew Thompson Apr 10 '12 at 18:51
  • 1
    *"My code is short,"* Yes. Good so far. "..self contained,"* Direct quote **the code .. can be 'copied, pasted, compiled, run'** The first two are fine. The code will not **compile** until imports are added (to make it shorter, I often compromise and do 'package' imports rather than individual class imports). The code will not **run** because it has no `main()` that creates the GUI, adds it to a top-level container and sets the container visible. *"but I think, I'm following the rules"* An SSCCE is a recommendation, not a rule. Thereafter something is either 'an SSCCE' or 'not an SSCCE' – Andrew Thompson Apr 10 '12 at 20:16