Paula Posio
Paula Posio

Reputation: 11

Java JCheckBox ItemListener makes program work incorrectly

I minimized my program to include only the problem and I tried to code exactly as I understood from many examples. When I used the ActionListener, I get problem solved. But I wonder why using ItemListener, checkbox does not operate correctly. If I run my program without ItemListener, it works correctly. With this ItemListener, checkBox doesn't change state.

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

public class omaJFrame extends JFrame{
    private JCheckBox checkBox1;

    public omaJFrame() {
        super("Window Title");

        TheHandler handler = new TheHandler();
        setLayout(new FlowLayout());

        checkBox1 = new JCheckBox("Checkbox 1");
        add(checkBox1);
        checkBox1.addItemListener(handler); 
    }

    private class TheHandler implements ItemListener {
        String output = "";

        public void itemStateChanged(ItemEvent event) {
            if (event.getItem()==checkBox1)
                output = String.format("%s", checkBox1.isSelected());
            JOptionPane.showMessageDialog(null, output); 
        }
    }
}

import javax.swing.JFrame;
public class EventHandlerMain {
    public static void main(String[] args) {
        omaJFrame window = new omaJFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(350,200);
        window.setVisible(true);
    }
}

Upvotes: 1

Views: 590

Answers (1)

trashgod
trashgod

Reputation: 205785

Works for me. Note also that Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class OmaJFrame extends JFrame {

    private JCheckBox checkBox1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                OmaJFrame f = new OmaJFrame();
            }
        });
    }

    public OmaJFrame() {
        super("Window Title");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        TheHandler handler = new TheHandler();
        setLayout(new FlowLayout());
        checkBox1 = new JCheckBox("Checkbox 1");
        add(checkBox1);
        checkBox1.addItemListener(handler);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    private class TheHandler implements ItemListener {

        String output = "";

        @Override
        public void itemStateChanged(ItemEvent event) {
            if (event.getItem() == checkBox1) {
                output = String.format("%s", checkBox1.isSelected());
            }
            JOptionPane.showMessageDialog(null, output);
        }
    }
}

Upvotes: 1

Related Questions