How to Use JScrollPane with Multiple Components in Java?

JScrollPane is one of the most Classy Features of Swings. I Had a really tough time figuring out how to use it with Many JRadioButtons. All I wanted to do was, to create a list of Many Radio Buttons and place them inside a ScrollPane. But, somehow the Scroll Pane wasn’t working. So, after some research on the net, I was able to figure out how to do that.

The Following Code gives detailed Explnation on what to do.
{code type=php}
//First Create an Outer Panel with Box Layout Settings.
outer_p = new JPanel();
outer_p.setLayout(new BoxLayout(cat_p, BoxLayout.Y_AXIS));
JScrollPane cat_scroll = new JScrollPane(outer_p);
outer_p.setSize(150, 800); //Important
cat_scroll.setBounds(150,315,195,130);
get_radios(); //This is a function, which would add all the JRadioButtons to the outer Panel(outer_p).
cat_scroll.setBorder(null); //Just to make it look good.
{/code}

I think that is all. Here, my get_radios() function, gets all the my Radiobuttons from the Database and adds it to outer_p. You needn’t worry about that. You can simple use a for loop and add dummy buttons to test it out.

We have been talking about JRadioButtons here, but you can add nearly everything to the Panel, and it would scroll.

Leave a Reply

Your email address will not be published. Required fields are marked *