Question

In a Java project I am using swt widget button as a check box. I have read the java docs and the function i need to check whether the box is checked or not seems to be Button.getSelection(). In my implementation, whether i have selected the box or not, It always seems to return true.

setChecks is called at the start and getFilter is then called when a button is pressed. the, check boxes appear as expected and I can deselect some of them, but when i press my button the print line in getFilter returns true for each of the buttons.

void setChecks(String[] s){
    button=new Button[s.length];
    bool = new boolean[button.length];
    for(int i=0;i<s.length;i++){
        button[i] = new Button(shell,  SWT.CHECK);
        button[i].setSelection(true);
        button[i].setBounds(449, 209+(20*i), 174, 16);
        button[i].setText(s[i]);
        //System.out.println(s[i]);
    }
}
private boolean[] getFilter() {
    bool = new boolean[button.length];
    for (int i=0; i<bool.length;i++){
        bool[i]=button[i].getSelection();
        System.out.println(button[i].getSelection());
    }
    return bool;
}

Edit:

amended code:

button[i].addSelectionListener(new SelectionListener(){
            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
            }
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub
                Button b = (Button)arg0.getSource();
                b.setSelection(!b.getSelection());
            }
        });
Était-ce utile?

La solution

You are always setting it to true by

button[i].setSelection(true);

that's why you always get true. You need to set the button once you make a selection. Do you have a listener attached to it. You have to use

 void addSelectionListener(SelectionListener listener) 

Adds the listener to the collection of listeners who will be notified when the control is selected by the user, by sending it one of the messages defined in the SelectionListener interface.

Please read the documentation on SelectionListener. You have to use

void widgetSelected(SelectionEvent e)

Sent when selection occurs in the control.

void widgetDefaultSelected(SelectionEvent e)

Sent when default selection occurs in the control.

Autres conseils

This is the solution i have found, and then accessing bool, instead of calling getFilters.

void setChecks(String[] s){
    button=new Button[s.length];
    bool = new boolean[button.length];
    for(int i=0;i<s.length;i++){
        button[i] = new Button(shell,  SWT.CHECK);
        button[i].setSelection(true);
        bool[i]=true;
        button[i].addSelectionListener(new SelectionListener(){
            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
            }
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                Button b = (Button)arg0.getSource();
                for (int i=0;i<button.length;i++){
                    if (button[i].getText()==b.getText())bool[i]=!bool[i];
                }
            }
        });
        button[i].setBounds(449, 209+(20*i), 174, 16);
        button[i].setText(s[i]);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top