Reputation: 85
I currently have written some Java Swing using a JFrame
and it works fine.
But now I need to use multiple screens and they are done using cardlayouts
.
So i need to convert my JFrame
to JPanel
. Currently I have this line for my JFrame
mainFrame.getContentPane().add(c4Panel,BorderLayout.CENTER);
But if I convert mainFrame
to JPanel
I cant use getContentPane()
IDE tells me to use getRootPane()
but I get error on this line
mainPanel.getRootPane().add(c4Panel,BorderLayout.CENTER);
The error I get is
Exception in thread "main" java.lang.NullPointerException
Upvotes: 4
Views: 3492
Reputation: 168825
mainPanel.getRootPane().add(c4Panel,BorderLayout.CENTER);
Should just be:
mainPanel.add(c4Panel,BorderLayout.CENTER);
The code:
mainFrame.getContentPane()
..is simply returning a container which itself has an add()
method, and the getContentPane()
part has been unnecessary for some time.
Upvotes: 3