tags:

views:

50

answers:

1

Hi,

I got a JScrollPane in which I want to place a list of radio buttons and labels. My problem is the panel doesn't scroll, I suppose it's because i didn't set a viewport, but how can I set it when I have to many components? My code looks something like this:

JScrollPane panel = new JScrollPane();
JRadioButton myRadio;
JLabel myLabel;
for(int i = 0; i<100; i++){
    myRadio = new JRadioButton();
    myLabel = new JLabel("text");
    panel.add(myRadio);
    panel.add(myLabel);
 }

Thanks.

+3  A: 

It is better to put you buttons and labels in a wrapper JPanel and then drop that into a JScrollPane.

try this:

    JPanel panel = new JPanel(new GridLayout(0,1));
    JRadioButton myRadio;
    for(int i = 0; i<100; i++){
        myRadio = new JRadioButton("text" + i);
        panel.add(myRadio);
     }
    JScrollPane scrollPane = new JScrollPane(panel);

be sure to look into ButtonGroup as well. ButtonGroups allow you to enforce the single selection constraint common to radio buttons.

akf
I tried setting a JPanel as viewport but it doesn't work because it is not a component.
Lucia
JPanel is most definitely a Component. Try the code above or "JScrollPane.setViewportView(myJPanel)".
DJClayworth