1

I'm trying to have painted into a JPanel (which is inside a ScrollPane), a bunch of labels and RadioButtons, dynamically. I receive an ArrayList with "Advice" objects, and I want to iterate over them to represent them in a way I have a label that describes them, and then, two radio buttons (to choose "Yes" or "No").

But at the moment, with this code at the JFrame's constructor, it's not properly working:

// My constructor
public CoachingFrame(AdvicesManager am) {
    initComponents();
    this.am = am;

    // I set the layout for the inner panel (since ScrollPane doesn't allow BoxLayout)
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    // Iterate over the arraylist
    for(int i=0;i<am.advices.size();i++){

       //Add elements to the panel
       panel.add(new JLabel( am.advices.get(i).getQuestion()));
       ButtonGroup group = new ButtonGroup();

       // Group the RadioButtons inside another panel, so I can use FlowLayout
       JPanel buttonsPanel = new JPanel();
       buttonsPanel.setLayout(new FlowLayout());
       JRadioButton rad1 = new JRadioButton();
       JRadioButton rad2 = new JRadioButton();
       group.add(rad1);
       group.add(rad2);
       buttonsPanel.add(rad1);
       buttonsPanel.add(rad2);

       // Add the radiobuttons' panel to the main one, and revalidate
       panel.add(buttonsPanel);
       panel.revalidate();
    }
     // Finally, add the panel to the ScrollPane.
    questions.add(panel);
}

I receive the arraylist correctly; I already checked that. The problem seems to be when painting the components.

Since I always use the NetBeans GUI creator, I'm not very used to add components via code. Can someone help me? I guess I'm missing something here.

edit: Note that "questions" is the ScrollPane object!

edit 2: This "questions" panel should have all those components painted: http://i.imgur.com/tXxROfn.png

4
  • try pack() method on your panel Commented Jan 3, 2016 at 18:25
  • Try to show us your best attempt at a minimal reproducible example (please check the link). Also post images of what you're seeing and what you're trying to achieve. This will help us better understand your problem. Commented Jan 3, 2016 at 18:26
  • I think it's more visible and easier to debug now Commented Jan 3, 2016 at 18:34
  • 3
    "Note that "questions" is the ScrollPane object!". That's possibly the problem. Contents are not added to a JScrollPane like in other components. Use JscrollPane.setViewportView(Component) to set the view instead of .add(). Commented Jan 3, 2016 at 18:36

1 Answer 1

2

As Kiheru said, ScrollPane doesn't allow views (like my JPanel) to be added with .add(), instead, I had to use .setViewportView(Component). Now it's working perfectly, thank you!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.