How to include the vertical scrollbar in Jpanel with spring layout

How to include the scrollbar in jpanel with spring layout.I have included the jpanel.

Here is my code.

Jpanel panel = new Jpanel(new sprngLayout())
JScrollpane spane = new JScrollpane(panel);
spane.add(a);
spane.add(b);
continer.add(spane);

Please help me.
Rose_TaylorAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
your code works fine when I tested it
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

public class GUI {

	public static void main(String[] args) {

		JFrame frame = new JFrame();
		JPanel packingPanel = new JPanel(new FlowLayout());
		packingPanel.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.white), "pack"));
		packingPanel.setBackground(Color.red);
		packingPanel.setPreferredSize(new Dimension(400, 200));
		JScrollPane scrollPane = new JScrollPane(packingPanel);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		JRadioButton packingJRadioButton1 = new JRadioButton("TV)");
		packingJRadioButton1.setSelected(true);
		ButtonGroup packingbuttongroup = new ButtonGroup();
		packingbuttongroup.add(packingJRadioButton1);
		packingPanel.add(packingJRadioButton1);

		JRadioButton packingJRadioButton2 = new JRadioButton("Newspaper");
		packingbuttongroup.add(packingJRadioButton2);
		packingPanel.add(packingJRadioButton2);

		JRadioButton packingJRadioButton3 = new JRadioButton("Email");
		packingbuttongroup.add(packingJRadioButton3);
		packingPanel.add(packingJRadioButton3);

		JRadioButton packingJRadioButton4 = new JRadioButton("Mail");
		packingbuttongroup.add(packingJRadioButton4);
		packingPanel.add(packingJRadioButton4);

		JRadioButton packingJRadioButton5 = new JRadioButton("Open-Portrait");
		packingbuttongroup.add(packingJRadioButton5);
		packingPanel.add(packingJRadioButton5);

		JRadioButton packingJRadioButton6 = new JRadioButton("Closed-Portrait");
		packingbuttongroup.add(packingJRadioButton6);
		packingPanel.add(packingJRadioButton6);

		JRadioButton packingJRadioButton7 = new JRadioButton("Open-Sports");
		packingbuttongroup.add(packingJRadioButton7);

		packingPanel.setLayout(new BoxLayout(packingPanel, BoxLayout.Y_AXIS));

		frame.getContentPane().add(scrollPane);
		frame.setSize(100, 100);
		frame.setVisible(true);
	}

}

Open in new window

0
 
objectsCommented:
try:

Jpanel panel = new Jpanel(new sprngLayout())
JScrollpane spane = new JScrollpane(panel);
panel.add(a);
panel.add(b);
continer.add(spane);
0
 
Rose_TaylorAuthor Commented:
I have using four panels.like one outerpanel within outerpanel there two innerpanel,here i need only one panel need scrollbar.I am not sure why this is happing in my code.

Example:
outPaneel
...
...
new innerpanel
....
....
Jpanel panel = new Jpanel(new sprngLayout())
JScrollpane spane = new JScrollpane(panel);
panel.add(a);
panel.add(b);
outerpanel.add(innerpanel1);
outerpanel.add(spane);
continer.add(outerpanel);

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
> I am not sure why this is happing in my code.

not sure what is happening?
0
 
Rose_TaylorAuthor Commented:
:) I am not getting vertical scrollbar on my window.Please let me...
0
 
objectsCommented:
is the panel large enough to need one?

see here to force scrollbars
http://helpdesk.objects.com.au/java/how-do-i-get-a-jscrollpane-to-always-display-scroll-bars
0
 
Rose_TaylorAuthor Commented:
Excellent , I am getting scrollbar.But it is not enabled...looks like disabled.Rows are extend to the panel, but scrollbar not active.
0
 
objectsCommented:
sounds like a problem with your panel. check it is returning the correct preferred size
0
 
Rose_TaylorAuthor Commented:
Please give me any suggestions on this issue.
0
 
objectsCommented:
need to see the relevant parts of your code to be able to help further
0
 
Rose_TaylorAuthor Commented:
Please look at this......
JPanel packingPanel = new JPanel(new FlowLayout());
	        packingPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.white),"pack"));
	        packingPanel.setBackground(EZschool.getApplicationBGColor());
	        packingPanel.setPreferredSize(new Dimension(400, 200));
	        JScrollPane scrollPane = new JScrollPane(packingPanel);
	       // scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	        		scrollPane.setVerticalScrollBarPolicy(
	        		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
	         
			packingJRadioButton1 = new JRadioButton("TV)");
			packingJRadioButton1.setSelected(true);
			packingbuttongroup = new ButtonGroup();
			packingbuttongroup.add(packingJRadioButton1);
			packingPanel.add(packingJRadioButton1);
			
			
			packingJRadioButton2 = new JRadioButton("Newspaper");
			packingbuttongroup.add(packingJRadioButton2);
			packingPanel.add(packingJRadioButton2);
			
			
			packingJRadioButton3 = new JRadioButton("Email");
			packingbuttongroup.add(packingJRadioButton3);
			packingPanel.add(packingJRadioButton3);
			
			
			packingJRadioButton4 = new JRadioButton("Mail");
			packingbuttongroup.add(packingJRadioButton4);
			packingPanel.add(packingJRadioButton4);
			
			packingJRadioButton5 = new JRadioButton("Open-Portrait");
			packingbuttongroup.add(packingJRadioButton5);
			packingPanel.add(packingJRadioButton5);
			
			packingJRadioButton6 = new JRadioButton("Closed-Portrait");
			packingbuttongroup.add(packingJRadioButton6);
			packingPanel.add(packingJRadioButton6);
			
			packingJRadioButton7 = new JRadioButton("Open-Sports");
			packingbuttongroup.add(packingJRadioButton7);
			//packingPanel.add(packingJRadioButton7);
			
			


			
			  mountPanel.setLayout(new BoxLayout(mountPanel, BoxLayout.Y_AXIS));			  
			  packingPanel.setLayout(new BoxLayout(packingPanel, BoxLayout.Y_AXIS));
			  //jscrollPane.setLayout(new BoxLayout(jscrollPane, BoxLayout.Y_AXIS));
			  
	       
	        servicePanel.add(scrollPane);

Open in new window

0
 
objectsCommented:
>                 packingPanel.setPreferredSize(new Dimension(400, 200));

try getting rid of that

and why do you change the layout of packingPanel?
0
 
objectsCommented:
How did that go?
0
 
Rose_TaylorAuthor Commented:
No ...still i didn't get scrollbar...
0
 
Rose_TaylorAuthor Commented:
Excellent,Thank you .
0
 
Rose_TaylorAuthor Commented:
Excellent
0
 
objectsCommented:
no worries
0
All Courses

From novice to tech pro — start learning today.