Frage

I have a checkbox on a JFrame. When I check it, I want to display on the command window that it has been selected. Below is the code i am working with. It compiles and executes without errors, but I don't get the "one has been selected" on the window when I select the checkbox.

 public Checklist() {

    ...

    JCheckBox one = new JCheckBox("CT scan performed");
    one.addItemListener(new CheckBoxListener());

    }
        private class CheckBoxListener implements ItemListener{
        public void itemStateChanged(ItemEvent e)
        {
        if(e.getSource()==one){ if(one.isSelected()){
        System.out.println("one has been selected");
            }
            else{System.out.println("nothing");}
            }
     }}
War es hilfreich?

Lösung

I have tested this simple example and it works perfectly (it writes "one has been selected" when you select the checkbox, and "nothing" when you deselect it):

import javax.swing.*;
import java.awt.event.*;

public class Example extends JFrame{
    public JCheckBox one;

    public Example() {
        one = new JCheckBox("CT scan performed");
        one.addItemListener(new CheckBoxListener());
        setSize(300,300);
        getContentPane().add(one);
        setVisible(true);
    }

    private class CheckBoxListener implements ItemListener{
        public void itemStateChanged(ItemEvent e) {
            if(e.getSource()==one){
                if(one.isSelected()) {
                    System.out.println("one has been selected");
                } else {System.out.println("nothing");}
            }
        }
    }

    public static void main(String[] args) {
        new Example();
    }
}

In your example, it seems that one is declared in the constructor CheckList(). Are you sure that it can be accessed in the inner class CheckBoxListener?

Andere Tipps

You may not realize that you actually have two checkboxes going on in your program!

I assume your class looks something like this:

public class Checklist extends JFrame {
    private JCheckBox one;

    public Checklist() {
        JCheckBox one = new JCheckBox("CT scan performed");
        one.addItemListener(new CheckBoxListener());
        this.add(one);
    }
}

You have two copies of "one", the "private JCheckBox one" that belongs to Checklist, and the "JCheckBox one = ..." in your constructor. You'll then notice that when you call

one.addItemListener(new CheckBoxListener());
this.add(one);

you are actually using the temporary "one" in the constructor, which is NOT the same as the "one" at the top of your class!

Now, when you call

if(e.getSource() == one)

you are now using the "one" at the top of your class, which is NOT the checkbox you see in your window!

This is why removing that "JCheckBox" makes your program work--now the "one" in your constructor is the same as the "one" at the top of your class.

To make this more, clear, the following code is what you are REALLY doing in your broken example:

public class Checklist extends JFrame {
    private JCheckBox one;

    public Checklist() {
        JCheckBox anotherOne = new JCheckBox("CT scan performed");
        anotherOne.addItemListener(new CheckBoxListener());
        this.add(anotherOne);
    }
}

...

if(e.getSource() == one)  //not equal to anotherOne!

You need to add an action listener on every checkbox except the seventh one, which doesn't have any impact), and re-compute the enabled state of the button each time the listener is called. See addActionListener.

In order to achieve this, you will want one method that will check for all the cases you care about and take the appropriate action when a state is achieved. Then have an ActionListener for each checkbox that calls this one worker method.

Have you checked if this line

if(e.getSource()==one){

is ok? Try removing this and see if it helps. Events may be a pain with real sources.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top