-1

I'm doing my first steps with Java Swing and using Netbeans to draw the layout. Netbeans creates a static layout, but I need a bit of dynamic. I already managed to replace the content of JScrollPane by:

jspIngredients.remove(jpIngredients);
jspIngredients.setViewportView(new JPIngredients(brewRecipe));
pack();

That works fine and my own JPanel is visible inside the scroll pane after that.

Now I have the same requirement for a panel, but there it doesn't work:

jpPrevStep.removeAll();
jpPrevStep.add(new JPBrewStep(brewRecipe.getBrewStepbyPosition(1)));
jpPrevStep.revalidate();
jpPrevStep.repaint(); 

jpPrevStep is a panel which is part of the Netbeans created layout. Inside this panel I wand to show my own content, which is also a panel (named JPBrewStep).

But nothing is shown in the window, so the .removeAll() works, but adding the new content does not.

I also tried to add my new content via a layout instead direct:

jpCurrentStep.remove(jPanel1);
jPanel1 = new JPBrewStep(brewRecipe.getBrewStepbyPosition(1));
javax.swing.GroupLayout jpCurrentStepLayout = new javax.swing.GroupLayout(jpCurrentStep);
jpCurrentStepLayout.setHorizontalGroup(jpCurrentStepLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
jpCurrentStepLayout.setVerticalGroup(jpCurrentStepLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE));
jpCurrentStep.revalidate();
jpCurrentStep.repaint();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Probably the best way to get us to fully and quickly understand your problem would be if you were to to create and post a [minimal example program](http://stackoverflow.com/help/mcve), a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification. This code would be small enough to post in its entirety completely within your question as code-formatted text. – Hovercraft Full Of Eels May 27 '18 at 11:34
  • 1
    Also, you're not telling us the layout managers involved, and that is key information. One thing I can tell you is that you don't want to add/remove from containers that use GroupLayout if you can avoid it. Change the layout to something else, more programmer-friendly. – Hovercraft Full Of Eels May 27 '18 at 11:34
  • 1
    In addition to the sage advice already offered: use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556) when swapping out one component / panel for another. It is also possible to retain the current layouts when using a card layout. It simply requires an extra container for the card layout. – Andrew Thompson May 27 '18 at 12:24

1 Answers1

0

Thanks for your answers. I added

jpPrevStep.setLayout(new java.awt.CardLayout());

and now it works.

The full code pice is now:

jpPrevStep.removeAll();
jpPrevStep.setLayout(new java.awt.CardLayout());
jpPrevStep.add(new JPBrewStep(brewRecipe.getBrewStepbyPosition(1)), "card2");
jpPrevStep.revalidate();
jpPrevStep.repaint();

Sorry that I didn't paste more code, but I created the layout with netbeans design editor which generates a lot of code. netbeans seems to use the grouplayout as default.