Question

I have empty jpanel. After button click should add elements to it in loop. My code:

 radio = new JRadioButton[answerArrayList.size()];
 for (int i = 0; i < answerArrayList.size(); i++) {
    final Answer a = answerArrayList.get(i);
    radio[i] = new JRadioButton();
    radio[i].setText(a.getAnswer());
    group.add(radio[i]);
    aPanel.add(radio[i]);
 }
 aPanel.repaint();

But after this action panel is still empty. How I can add items to panel and clear all items from it?

Was it helpful?

Solution

You need to call

aPanel.revalidate();

before invoking repaint().

Side note: Take a look at CardLayout for changing the visibility of components at runtime

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top