Reputation: 1073
I'm new here, and this is my first question: I'm trying to make a question and answer (pop quiz) program, i've gone through alot of hurdles by checking out answers from previous questions, but i ran into a confusing one.
I place a radio button in a jPanel and made it final and static in order to retain its selected status, i'm trying to avoid making multiple jPanel classes (using NetBeans GUI builder) and decided to use only one Jpanel class. But my problem is, each time i press the next button, it still shows the same page(with the radio button still selected). How can i make an entirely new Jpanel, but still similar to the original one?
here's my code sample:
package pages;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
/**
*
* @author Hendy
*/
public class page1 extends javax.swing.JPanel {
/**
* Creates new form page1
*/
public page1() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jRadioButton1.setText("first");
jButton1.setText("next");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(176, 176, 176)
.addComponent(jRadioButton1)
.addContainerGap(179, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(30, 30, 30))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(131, 131, 131)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(27, 27, 27))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
page1 on = new page1();
add(on,BorderLayout.NORTH );
revalidate();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private static final javax.swing.JRadioButton jRadioButton1 = new javax.swing.JRadioButton();
// End of variables declaration
}
Upvotes: 1
Views: 142
Reputation: 803
If you have a JPanel, you must also have a container holding that JPanel. Most probably it is a JFrame; anyway - if you have a container, most probably you put your JPanel in it with function add(Component component)
. Try different approach:
In your container create a static field that will store your page1
. If you use stock JFrame, simply extend it.
From what I imagine, every single page1
represents a single question in quiz, so you want this field to be some kind of collection to store all your questions.
Collection type - this is up to you, but I think Map
, Set
, List
would be the most handy. What you want to do is put all your questions here and you can do it in for example in container constructor. I'll use Map
here, but feel free to use any other type of collection if you want.
Map
to store the question number and its JPanel, so let's haveMap<Integer, page1> map = new HashMap<Integer, page1>();
Map
.get
your page1
with the correct number of question that would be previous/next one and make it shown by putting it in your container with function setContentPane(Component chosenComponent)
.This way you have access to all your questions. I hope now it's easier to understand.
Speaking of chosen answers - wouldn't it be a better idea to create additional field in your page1
class that would simply store what the user chose and according to that field a proper answer would be checked on next/previous button clicked?
Example below:
Create a field in your JPanel to store your answer - whatever type you want, it may as well be an int. So, you have in your page1
field private int chosenAnswer;
. Now, when you create all your questions (probably when putting them in the Map
from the solution above), initialize your variable chosenAnswer = 0; (simply put it in page1
constructor). Now, when the previous/next button is clicked, check all your radio buttons if some of them is clicked. If first is, chosenAnswer = 1; etc. If none of them is leave 0. This is clear, I believe.
Now, you want to get this information back somehow, when you come back to the question that had previously checked answer. To do this, try this approach:
page1
function public void setAnswer()
. You want this function to check the value of that int chosenAnswer
- you can do it with switch
or simply multiple if
. This chosenAnswer
is gonna match the answer that was previously checked.JRadioButton
, that needs to be checked, call the function setSelected inherited from AbstractButton
, for example firstRadioButton.setSelected(true)
if the value of chosenAnswer == 1
.setContentPane(chosenQuestion)
and before that line put chosenQuestion.setAnswer();
.Upvotes: 2