i'm currently working on a quiz program in which if all questions aren't answered, a JOptionpane confirm dialog appears asking the user if he/she wants to proceed. At first, everything was working fine, then i decided to use a paintComponent() method throughout the program. After this implementation, if this JOptionpane pops up, the containing Jpanel disappears(taking all the questions displayed away with it) then only the background remains visible. Been at it all day. here's the code where the issue occurs:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package operator;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.JOptionPane;
import static operator.Rules.examTimer;
import static operator.Welcome.image;
/**
*
* @author Hendy
*/
public class QuestionExams extends javax.swing.JPanel {
QuestionCall on = new QuestionCall();
int sBtn,nums = on.number();
/**
* Creates new form QuestionExams
*/
public QuestionExams() {
randomQuestion();
initComponents();
if (control == 0)
previousButton.setEnabled(false);
group.add(a);
group.add(b);
group.add(c);
group.add(d);
setSelected();
if (counter>=(maximum()-1) && control >= (maximum()-1))
{nextButton.setText("Finish");
finish.setVisible(false);}
if (counter== maximum() && control == maximum())
{
determinant();
}
}
public void determinant(){
if (checkAdd()<maximum())
{
{
sBtn = JOptionPane.showConfirmDialog(null, " Do you want to finish the exam?\n "
+ "Some questions are still unanswered",
"Proceed?",
JOptionPane.YES_NO_OPTION);
if (sBtn == JOptionPane.YES_OPTION)
{ wrongsAdd();
nextPage();
System.out.println("this shows the question key:"+wrongs.get(0));
System.out.println("this shows the question number:"+wrongNos.get(0));}
else
{
on.numberBack();
selection();
--control;
--key;
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
QuestionExams oan = new QuestionExams();
add(oan,BorderLayout.NORTH );
revalidate();
System.out.println("counter:"+counter +" and "+"control:" +control);
}
}
}
else
{
wrongsAdd();
nextPage();
}
}
public void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, null);
repaint();
}
@SuppressWarnings("unchecked")
private void initComponents() {
group = new javax.swing.ButtonGroup();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
examTiming = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
nameQ = new javax.swing.JTextArea();
jPanel4 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
a = new javax.swing.JRadioButton();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
b = new javax.swing.JRadioButton();
c = new javax.swing.JRadioButton();
d = new javax.swing.JRadioButton();
jScrollPane1 = new javax.swing.JScrollPane();
optA = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
optB = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
optC = new javax.swing.JTextArea();
jScrollPane5 = new javax.swing.JScrollPane();
optD = new javax.swing.JTextArea();
quitButton = new javax.swing.JButton();
previousButton = new javax.swing.JButton();
nextButton = new javax.swing.JButton();
finish = new javax.swing.JButton();
private void finishActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
AnswerCheck an = new AnswerCheck();
add(an, BorderLayout.NORTH );
revalidate();
wrongsAdd();
}
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
{
selection();
if(control==counter){
{newAnswer();
{if ((QuestionCall.display6E(balance.get(key+1))+ "").equals(objective.get(control)))
scores=1;
else
scores =0;}
newScore();
if ("nothing".equals(value)){
check=0;
newCheck();
}
}
++counter;}
if (control<counter){
replaceAnswer();
{if ((QuestionCall.display6E(balance.get(key+1))+ "").equals(objective.get(control)))
scores=1;
else
scores =0;}
changeScore();
if ("nothing".equals(value)){
check=0;
validateCheck();
}
}
++control;
System.out.println(QuestionCall.display6E(balance.get(key+1)));
++key;
removeAll();
setLayout( new BorderLayout() );
invalidate();
QuestionExams oan = new QuestionExams();
add(oan,BorderLayout.NORTH );
revalidate();
System.out.println(key);
System.out.println("counter:"+counter +" and "+"control:" +control+" position: "+position);
}
}
private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
on.numberBack();
selection();
--control;
--key;
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
QuestionExams oan = new QuestionExams();
add(oan,BorderLayout.NORTH );
revalidate();
System.out.println(QuestionCall.display6E(balance.get(key+1)));
System.out.println(key);
System.out.println("counter:"+counter +" and "+"control:" +control);
}
private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int select = JOptionPane.showConfirmDialog(null, "Do you really want to quit the test?",
"Quit?",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION)
// to
{
examTimer.stop();
on.clearFramework();
removeAll();
setLayout(new FlowLayout());
setLayout( new BorderLayout() );
invalidate();
oktest an = new oktest();
add(an, BorderLayout.NORTH );
revalidate();}
}
}
private javax.swing.JRadioButton a;
private javax.swing.JRadioButton b;
private javax.swing.JRadioButton c;
private javax.swing.JRadioButton d;
public static javax.swing.JLabel examTiming;
private javax.swing.JButton finish;
private javax.swing.ButtonGroup group;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
public javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JTextArea nameQ;
private javax.swing.JButton nextButton;
private javax.swing.JTextArea optA;
private javax.swing.JTextArea optB;
private javax.swing.JTextArea optC;
private javax.swing.JTextArea optD;
private javax.swing.JButton previousButton;
private javax.swing.JButton quitButton;
}
Most of the layout code is left out. I also included some images for clarity.
First is the last question page
Next up is what happens when i click the next button(which is now labelled finish)
and lastly, this was the original work, before i did the paintComponent() method, unluckily, i don't have the code for this anymore(I just have the .exe file)
I dunno if its a bug, because the Quit button also shows an optionpane of its own, and it displays perfectly.
Solved It. I wasn't too sure were the error was coming from, so i just decided to create my own yes-no dialog. It worked normally at first, but when i made it modal, it had the same issue as the JOptionPane, which made me conclude that it was the modal property of the JOptionPane that caused it. But by moving the JOptionPane action to next button, instead of the constructor, the event happened, just as normal again.