Made it work! Thank you guys!
The code follows. I used BoxLayout
since I thought it'd be ideal for stacking questions one on top of the other, but now I got issues with the layout... When I stack several questions the question panels start overlapping. Any thoughts?
panels1 = new MultipleChoice[5];
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
for(int i=0; i<4; i++){
panels1[i]= new MultipleChoice();
panels1[i].setAlignmentX(CENTER_ALIGNMENT);
add(panels1[i]);
}
setVisible(true);
I'm working on designing an Online Test applet.
A test has multiple choice and true/false questions. To set up a test I created two JPanel
classes, one for the multiple choice question and one for the true/false questions. So when a test is created I'll just dynamically add these panels to a JPanel
container according to the non-fixed number of questions.
First, how can I dynamically add new panels to a panel? I thought about declaring an array of the JPanel
type. I created and then add objects of this panel class using a for
loop:
MultitipleChoicePanel[] PanelArray;
for (...){
PanelArray[i] = new MultipleChoicePanel();
containerpanel.add(PanelArray[i]);
}
I don't know if this is technically possible.
This is my first time using Swing, and I tried doing this but obviously it didn't work.
Does anyone have an idea how correctly dynamically add these panels?
Second, which of the layout managers is best suited for the container panel in order to fit every new panel added right under the previous one?
I thought about dynamically setting up a GridLayout
of one column and add rows as I add panels. But I've been really struggling modifying swings dynamically.
Any suggestions?
Thank you so much for your help!