Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Adding buttons to a scroll pane

843806Nov 11 2007 — edited Nov 11 2007
I'm trying to add buttons to a scroll pane, I need the buttons to be of a fixed size, so that they won't change size if more buttons are added etc.

Here is my code so far
public class Menu extends JPanel 
{
	JButton a = new JButton("test");
	JButton b = new JButton("test1");
	JButton c = new JButton("test3");
	JButton d = new JButton("test4");
	JButton e = new JButton("test5");
	JButton f = new JButton("test6");
	JButton g = new JButton("test7");
	JButton h = new JButton("test8");
	
	Menu()
	{
		JPanel me = new JPanel();
		me.setLayout(new GridLayout(0,2));
		me.add(a);
		me.add(b);
		me.add(c);
		me.add(d);
		me.add(e);
		me.add(f);
		me.add(g);
		me.add(h);
		JFrame frame = new JFrame();
		JScrollPane spane = new JScrollPane(me);
		spane.setPreferredSize(new Dimension(180,500));
		add(spane);
		frame.add(this);
	}
}
This doesn't work because the buttons size will be adjusted to fit the size of the pane.

I need to have it to that if there are too many buttons to show, then a scroll bar will be shown.

Any ideas?
Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 9 2007
Added on Nov 11 2007
5 comments
839 views