0

I want to replace JPanel1 in a JFrame by JPanel2 which contains a JScrollPane on which JPanel1 should be displayed.

Making the JFrame displaying JPanel2 works, but when I use

JPanel2.JScrollPane1.setViewportView(JPanel1)

i end up having nothing displayed on my JScrollPane1. However, adding other components work, e.g.:

JPanel2.JScrollPane1.setViewportView(new JTextField("I need help!"))

Can anyone tell me what I am doing wrong?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 2
    Please post [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) for faster help. – Paul Samsotha Apr 19 '14 at 11:20
  • Also follow Java naming convention, and use lower case for variable names and follow encapsulation rules by using private fields and getters, unless you have good reason for not doing so. – Paul Samsotha Apr 19 '14 at 11:22
  • Possible duplicate of [Java Swing: How to change GUI dynamically](http://stackoverflow.com/questions/5750068/java-swing-how-to-change-gui-dynamically); also consider `CardLayout`. – trashgod Apr 19 '14 at 11:36
  • Yea, sorry for poor coding style - I didn't program for many years. – user3551485 Apr 22 '14 at 07:47
  • @trashgod I don't understand why I should be using CardLayout, as the pointer should be working... and CardLayout cannot be used here, as the parent jPanel1 should become the child of a content of jPanel2. However, I figured out that if I reinitiate the variable (by using jPanel1 = new JPanelWithContent();) everything works fine. Do you have any idea why I have to reinitiate it instead of just passing the pointer on? – user3551485 Apr 22 '14 at 07:59
  • If you really can't use `CardLayout`, try the other approach cited in your [mcve](http://stackoverflow.com/help/mcve). – trashgod Apr 22 '14 at 11:03

0 Answers0