This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Adding ScrollPane to JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding ScrollPane to JPanel" Watch "Adding ScrollPane to JPanel" New topic
Author

Adding ScrollPane to JPanel

Nageswar Kakolla
Ranch Hand

Joined: Jan 16, 2006
Posts: 71
Hi there,

Does JPanel need to implement Scrollable interface in order to add JScrollPane ?

I wanted to add JScrollPane to JPanel containing array of Radio Buttons.

I tried very hard but in vain

let me post the code what I am trying

JPanel m_itemPanel = new JPanel();
JScrollPane m_scrollPane = new JScrollPane(m_itemPanel);
m_scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_ALWAYS);

since I need to scrollpane to this JPanel and not to JFrame and I cant get getContentPane() of Frame

My JPanel consists of series of JRadioButtons and I want JScrollpane to be visible so that I can scroll down and Up

Appreciate Quick response and Thanx in advance for your help
Matti Poro
Ranch Hand

Joined: Dec 03, 2005
Posts: 41
You add a component to a JScrollPane, not vice versa. Make sure it is m_scrollpane you added to the container (JFrame) and not m_itempanel.
Nageswar Kakolla
Ranch Hand

Joined: Jan 16, 2006
Posts: 71
initially i did add JPanel to JScrollpane but that time

all radiobuttons in JPanel are gone and i havent seen scrollbar either
Matti Poro
Ranch Hand

Joined: Dec 03, 2005
Posts: 41
Here's an example:


[ March 02, 2006: Message edited by: Matti Poro ]
Nageswar Kakolla
Ranch Hand

Joined: Jan 16, 2006
Posts: 71
But I want to add Scrollpane to JPanel which inturn called by JFrame.

So you are telling me I should add scrollpane to JFrame
Matti Poro
Ranch Hand

Joined: Dec 03, 2005
Posts: 41
The JPanel will be added with the JScrollPane.
Nageswar Kakolla
Ranch Hand

Joined: Jan 16, 2006
Posts: 71
Thanx Matti for your response.

I executed your sample code and put similar logic for my problem

JFrame frame = getRapidFrame(this);
frame.add(new JScrollPane(m_itemPanel));
//frame.setSize(200,200);
frame.setVisible(true);

where getRapidFrame will give JFrame of whole application where My Panel is hosted but behaviour is wiered i got.

Is it possible to add ScrollPane to JPanel instead of Frame ?

I will explain myproject breifly :
I have Frame where it is divided into regions left & right areas where each region is JPanel.
My right area will host JPanels depending on the selection of nodes in left area.

one of My JPanel will come into picture in right region and I have array of radio buttons to be displayed with Scrollpane.

If I tried to get Parent Frame and add Scrollbar,I got wierd display like my radio buttons are scattered all over and there is no scroll bar displayed

I am not able to post GUI screen here to explain ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding ScrollPane to JPanel
 
Similar Threads
Scroll Pane not visible on Frame window
how to add a scrollpane...
JTable
JTable-JScrollPane
Scrollbar for jtable