Frage

I made this sample below to simulate multiple JCheckBox creation and its Action Listener.

int global=0;

//some code

JCheckBox[] checkBox = new JCheckBox[2];

        for(int i = 0; i <=1; i++){

        checkBox[i] = new JCheckBox(strings[i]);
        panel.add(checkBox[i]);

          checkBox[i].addItemListener(new ItemListener() {

                @Override
                public void itemStateChanged(ItemEvent evt) {
                    if (evt.getStateChange() == ItemEvent.SELECTED){
                        JOptionPane.showConfirmDialog(null, "Message"+global);
                    }
                }
            });                                                             
          global++;
        }

What I'm not getting is that my output for the Dialog is always "Message 2". In my logic if I'm declaring one AddItemListener for each checkBox, I should recieve two different dialogs for each checked box, such as "Message 1" and "Message 2". What am I doing wrong here? How to handle this please?

Thanks in advance

War es hilfreich?

Lösung

When showConfirmDialog() is first called global has already value 2. If you want different message for each check-box try putting global++ (will increment at each call) right before JOptionPane.showConfirmDialog(null, "Message"+global); and this will make it more clear to you.

if I'm declaring one AddItemListener for each checkBox, I should recieve two different dialogs for each checked box, such as "Message 1" and "Message 2"

Why do you think you should get two (different) invocations of listener method per checkbox if you know that you have only one listener per checkbox?

One of the more possible solutions could be to implement your own ItemListener which has stored the message (or just number) to be shown in its instance variable.

Andere Tipps

global is nowhere related to the JChekcbox objects that you are creating in your code. So , Whenever itemStateChanged is called by the application, It is reading the latest value of global which is 2. To achieve whatever you are looking for you should change your code in this way:

for(int i = 0; i <=1; i++){

        checkBox[i] = new JCheckBox(strings[i]);
        panel.add(checkBox[i]);
          checkBox[i].addActionCommand(String.valueOf(i+1));
          checkBox[i].addItemListener(new ItemListener() {

                @Override
                public void itemStateChanged(ItemEvent evt) {
                    if (evt.getStateChange() == ItemEvent.SELECTED){
                        JOptionPane.showConfirmDialog(null, "Message"+((JCheckBox)evt.getSource()).getActionCommand());
                    }
                }
            });   
         global++;
     }   

In your code global is increased by 1 at each iteration of the loop. Value of global is 2 after loop exits that is why you have "Message 2". If these numbers represent the location in the array then I would try:

    @Override
    public void itemStateChanged(ItemEvent evt) {
        int loc = indexInArray(evt.getItem(),checkBox);
        if (evt.getStateChange() == ItemEvent.SELECTED){
            JOptionPane.showConfirmDialog(null, "Message"+global);
        }
    }
});

You can implement a simple search in the method indexInArray:

public int indexInArray(Object []objects, Object obj){
    for(int i = 0 ; i < objects.length; i++){
        if(objects[i] == obj){
            return i;
        }
    }
    return -1;
}              
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top