This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Adding Scroll Pane to a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding Scroll Pane to a JPanel" Watch "Adding Scroll Pane to a JPanel" New topic
Author

Adding Scroll Pane to a JPanel

craig peddie
Greenhorn

Joined: May 15, 2013
Posts: 12
Hi,

I have spent the last couple of days searching the web trying to find a solution to what no doubt is a simple problem, and have finally given up. What I am trying to do is lay out a touch panel with a JPanel across the top with several buttons on it, then a navigation bar down the side with different buttons, and then a content area making up the rest of the frame. What I am trying to do is have the navigation bar at the side show only 5 buttons at a time, and I want to be able to scroll down to see additional ones. It seems no matter what I do I can't get the scroll bar to show up. Here is the code of the class that builds the bar and adds it to the application JFrame:


When I uncomment the scrollPane.add the entire NavBar disappears. Any help would be greatly appreciated!!

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You created a JScrollPane, but you didn't put anything inside it. Or rather, you put an empty JPanel inside it.

Whatever things you want to be controlled by the JScrollPane, they should be inside it. Based on your problem description, which perhaps I didn't read too carefully, it's "NavBar" you want to put inside it. Like this:



By the way I find the name "NavBar" rather confusing, because it starts with a capital letter. That's not illegal, but the standards for Java programming state that class names should start with a capital letter and variable names should start with a lower-case letter. So I find it hard to avoid thinking of "NavBar" as the name of a class.
craig peddie
Greenhorn

Joined: May 15, 2013
Posts: 12
My bad on the naming conventions, the unruly NavBar variable is now navBar. But adding the navBar to the scroll pane doesn't work. I've read all over that the preferred size is an important variable to set, so I've set that as well. Here are the last two lines in the method now:


When I comment these lines out, I at least get the navBar displayed (showing all 7 buttons instead of the 5 I want), but when I include these lines, the entire navBar disappears.



[Download navBarOn.tiff] Download

[Download navBarOff.tiff] Download

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You have to create the JScrollPane the way I described, and then add that to the panel. Don't add navBar to the panel, it belongs in the scroll pane.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7


You are adding the "navBar" to the frame. You need to add the scrollPane to the frame:





Get rid of the above line. It is the job of the layout manager to determine the size of the navBar based on the components added to it.



Get rid of all the setPreferred/Minimum size methods. Each component has its own preferred size. The GridLayout will make sure all buttons are the same size.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding Scroll Pane to a JPanel
 
Similar Threads
JScrollPane inside JTabbedPane
Swing Graphics and JPanel setPrefrerredSize
New Image in JScrollPane
Problem displaying JTable inside a JScrollPane
Positioning and size of GUI elements