-1

I want to execute an ItemListener method if a JButton is pressed (as written in my ActionListener method). I feel like I'm missing something important, because as far as I know, you cannot have method within another method.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

    public class spelling2 extends JFrame {

        private static final long serialVersionUID = 1L;
        JFrame frame = new JFrame();
        JButton question1;
        JButton question2;
        private JCheckBox q1a1;
        private JCheckBox q1a2;
        private JCheckBox q1a3;


        public spelling2() {
        super("Question 1");
        setLayout(new FlowLayout());

        q1a1 = new JCheckBox("recipracate");
        q1a2 = new JCheckBox("reciprocate");
        q1a3 = new JCheckBox("reciprokate");
        add(q1a1);
        add(q1a2);
        add(q1a3);


        question1 = new JButton("Question 1");
        add(question1);

        question2 = new JButton("Question 2");
        add(question2);


        HandlerClass handler = new HandlerClass();
        q1a1.addItemListener(handler);
        q1a2.addItemListener(handler);
        q1a3.addItemListener(handler);
        question1.addActionListener(handler);
        question2.addActionListener(handler);
        }


        private class HandlerClass implements ActionListener, ItemListener {

        public void actionPerformed (ActionEvent event) {
        if (question1.isSelected()) {
            d
        }
        public void itemStateChanged (ItemEvent event) {
        if (q1a2.isSelected())
            JOptionPane.showMessageDialog(frame, "quintessence is the correct answer");
        else if (q1a1.isSelected())
            JOptionPane.showMessageDialog(frame, "That is the wrong answer");
        else if (q1a3.isSelected())
            JOptionPane.showMessageDialog(frame, "That is the wrong answer");

        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
traderjosh
  • 321
  • 5
  • 16
  • what is this? `if (question1.isSelected()) { d }` Close the method brackets properly. – Braj Aug 16 '14 at 18:56
  • Done. I want the 3 check boxes to appear when one button is clicked. – traderjosh Aug 16 '14 at 18:59
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). 3) Use a logical & consistent code formatting style! The code indentation is intended to help people follow the program flow. 4) A single blank line of white space is *always* enough. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Aug 17 '14 at 03:03

1 Answers1

1

I hope my interpretation of the question is correct and you are trying to find out which of the buttons was clicked. You can use ActionEvent.getSource() to get the object that originated the event. For example:

@Override
public void actionPerformed(ActionEvent event) {
    if (event.getSource() == question1) {
        JOptionPane.showMessageDialog(frame, "question1 is pressed");   
    } else if (event.getSource() == question2) {
        JOptionPane.showMessageDialog(frame, "question2 is pressed");
    }
}

Note that you can also use anonymous action listeners, for example:

question1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(frame, "question1 is pressed");               
    }
});

Also note that it is usually unnecessary to extend top level windows such as JFrame unless you add new functionality to it.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • I want a new window to appear with one question (3 check boxes), and after one is selected I want a new window to appear with a new question. – traderjosh Aug 16 '14 at 22:32
  • @Joshua I am not sure what you mean. There is nothing about it in your question. Do you want to pop up a window with a question string and three check boxes each representing a possible answer? – tenorsax Aug 16 '14 at 23:48