Issue
I am an IT student and I've been trying to figure out why it doesn't work and I don't know what to do anymore so I decided to ask for help. The main problem is that my dialog box won't work on condition of the button here is the code for the action:
class Main extends JFrame implements ActionListener {
JButton submit;
submit = new JButton("Submit");
submit.setBounds(480, 400, 90, 25);
submit.addActionListener(this);
public void actionPerformed(ActionEvent e){
if (submit.isSelected()){
JOptionPane.showMessageDialog(this, "Done!");
}
}
and here's the whole code of the program for more details --
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Main extends JFrame implements ActionListener {
JButton submit;
Main(){
JFrame t = new JFrame("Parin_DCIT50Finals");
t.setBackground(Color.black);
JPanel mult = new JPanel();
mult.setBackground(Color.cyan);
mult.setBounds(5, 5, 250, 130);
JLabel mul = new JLabel("MULTIPLE CHOICE:");
JLabel qu = new JLabel("What is your favotite food?");
JRadioButton a = new JRadioButton("Burger");
JRadioButton b = new JRadioButton("Pizza");
JRadioButton c = new JRadioButton("Adobo");
JRadioButton d = new JRadioButton("Me");
mult.add(mul);
mult.add(qu);
mult.add(a);
mult.add(b);
mult.add(c);
mult.add(d);
JPanel mult1 = new JPanel();
mult1.setBackground(Color.yellow);
mult1.setBounds(5, 130, 250, 130);
JLabel qu1 = new JLabel("Best programming laguage?");
JCheckBox a1 = new JCheckBox("Java");
JCheckBox b1 = new JCheckBox("Python");
JCheckBox c1 = new JCheckBox("C++");
JCheckBox d1 = new JCheckBox("English");
mult1.add(qu1);
mult1.add(a1);
mult1.add(b1);
mult1.add(c1);
mult1.add(d1);
JPanel tf = new JPanel();
tf.setBackground(Color.cyan);
tf.setBounds(5, 260, 250, 130);
JLabel tof = new JLabel("TRUE OR FALSE:");
JLabel qu2 = new JLabel("Edward is very handsome & cute");
JCheckBox t1 = new JCheckBox("True");
JCheckBox f1 = new JCheckBox("False");
tf.add(tof);
tf.add(qu2);
tf.add(t1);
tf.add(f1);
JPanel tf1 = new JPanel();
tf1.setBackground(Color.yellow);
tf1.setBounds(5, 390, 250, 130);
JLabel qu3 = new JLabel("Math is a difficult subject");
JCheckBox t2 = new JCheckBox("True");
JCheckBox f2 = new JCheckBox("False");
tf1.add(qu3);
tf1.add(t2);
tf1.add(f2);
JPanel id = new JPanel();
id.setBackground(Color.cyan);
id.setBounds(260, 5, 500, 130);
JLabel identif = new JLabel("IDENTIFICATION:");
JLabel qu4 = new JLabel("What is my full name?");
JTextField ans = new JTextField(20);
id.add(identif);
id.add(qu4);
id.add(ans);
JPanel id1 = new JPanel();
id1.setBackground(Color.cyan);
id1.setBounds(260, 130, 500, 130);
JLabel qu5 = new JLabel("'OOP' stands for?");
JTextField ans1 = new JTextField(20);
id1.add(qu5);
id1.add(ans1);
JPanel essay = new JPanel();
essay.setBackground(Color.yellow);
essay.setBounds(260, 260, 500, 130);
JLabel tit = new JLabel("ESSAY:");
JLabel qu6 = new JLabel("What are your thoughts about COVID-19 during the whole school year?");
JTextArea ans2 = new JTextArea();
ans2.setBounds(20,75,500,200);
essay.add(tit);
essay.add(qu6);
essay.add(ans2);
submit = new JButton("Submit");
submit.setBounds(480, 400, 90, 25);
submit.addActionListener(this);
t.add(id);
t.add(id1);
t.add(essay);
t.add(mult);
t.add(mult1);
t.add(tf);
t.add(tf1);
t.add(submit);
t.setLayout(null);
t.setSize(500,500);
t.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (submit.isSelected()){
JOptionPane.showMessageDialog(this, "Done!");
}
}
public static void main(String[] args) {
new Main();
}
}
Thank you so much in advance and I'm looking forward to learn from you guys more.
Solution
if (submit.isSelected()){
https://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#isSelected()
The isSelected check is for toggle buttons. Those are buttons that behave like a checkbox and you can click them on or off. Your button is not one of those.
Try removing the condition. Currently the only thing registered for that actionPerformed is that one button so the condition is not needed (what was it supposed to do anyway?).
If for example you want to use the actionPerformed method for multiple things and you want to check whether the incoming event is from your button then you could use something like:
public void actionPerformed(ActionEvent e){
if (e.getSource() == submit){
...
Answered By - dratenik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.