Cows42
Cows42

Reputation: 307

JPanel and JDesktopPane

I'm trying to add a JPanel to my JFrame, thereafter set the JFrame's contentPane to a JDesktopPane so I can create new JInternalFrames and add them to the desktopPane, but the first JPanel is not showing when I do this:

JScrollPane newScroll = new JScrollPane(newButtonPanel); //Create new scroll and add the JPanel
newScroll.getVerticalScrollBar().setUnitIncrement(16);
add(newScroll, BorderLayout.CENTER); //Add the scroll containing the JPanel to center
newScroll.setVisible(true);
add(desktopPane);
setContentPane(desktopPane); 
desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

Upvotes: 1

Views: 316

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Here you add your JScrollPane to the JFrame's existing contentPane

JScrollPane newScroll = new JScrollPane(newButtonPanel); 
newScroll.getVerticalScrollBar().setUnitIncrement(16);
add(newScroll, BorderLayout.CENTER); 
newScroll.setVisible(true); 

And here you add the desktopPane to the existing contentPane and then replace that contentPane with the desktopPane.

add(desktopPane);  // why do both? both add to contentPane
setContentPane(desktopPane);   // and replace contentPane?
desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

The behavior you're getting shouldn't surprise you since you're replacing the contentPane and wondering why components added to it previously aren't seen. Solution: don't do this! If you want to show two components to the contentPane then don't replace it. Instead add each component to a different BorderLayout position since contentPanes use this layout by default. Where to place them will all depend on what GUI structure you're trying to build, something you've not yet told or shown us.

Upvotes: 1

Related Questions