How to add a listener to a checkbox in Java?

Hi,
I have the following code that creates three checkboxes.


What I want to do is, to trigger the showFailedResults method when I put a check on the failed checkbox.

Here is my code. At this point, I only need a trigger that will take the code to the beginning of showFailedResults method when I debug it.

How can I do that?

public class CSFilteringCheckBoxes extends Composite {
    
    private static Shell shell;
    private static Table table;
    private static String[] titles;
    private static Display display;
    
    private Button error = new Button(this, SWT.CHECK);
    private Button failed = new Button(this, SWT.CHECK);
    private Button passed = new Button(this, SWT.CHECK);
    private Label caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER);

    public CSFilteringCheckBoxes(Composite parent, int style) {
        super(parent, style);
        Font font = new Font(parent.getDisplay(),"Arial",9, SWT.NORMAL);
        final Color lightgray;
        lightgray = new Color (parent.getDisplay(), 140, 140, 140);
        
        GridLayout gl = new GridLayout(); gl.verticalSpacing = 4;
        setLayout(gl);
        error.setText("Error");
        failed.setText("Failed");
        passed.setText("Passed");
        caption.setText("Filtering");
        //caption.setForeground(lightgray);
        caption.setFont(font);
        
        
        error.setSelection(true);
        failed.setSelection(true);
        passed.setSelection(true);
        
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption
        setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        error.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        failed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        passed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        
    }
    
    
    public void showFailedResults(){
        
        List<CheckDetail> checkDetails = RunPerl.checkDetailsAll;
        
        //get the list of failed  checks
        
        failed.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (event.detail == SWT.CHECK) {
                    //show failed checks
                    
                } else if (event.type == 13) { //type 13 means mouse down

                }
            }
        });
        

        
        
        
        boolean newRun = true;
        boolean evenRow = true;
        //Gui.populateResultsTable(checkDetails, shell, table, titles, newRun, evenRow);        
    }
    
    
    public void setTable(Table table, String[] titles) {
        CSFilteringCheckBoxes.table = table;
        CSFilteringCheckBoxes.titles = titles;
    }  
    
    
    public void setShell(Shell shell, Display display) {
        CSFilteringCheckBoxes.shell = shell; 
        CSFilteringCheckBoxes.display = display;
    }

}

Open in new window

TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
I'm not sure if I have this 100% right because the code you have is a little bit strange, but don't you just need to change around what you have already. In the code above, you are adding the listener to the checkbox (line 49) from inside the method showFailedResults (line 43). Wouldn't you want to add the listener somewhere in your constructor (lines 13 - 40), and then the code inside the listener calls the showFailedResults() method?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ThommyCommented:
I also do not fully cope with your code. Therefore just a little example how to implement an itemlistener for a checkbox...

public class checkbox_listener extends JFrame implements ItemListener
{   JLabel  label1;
    JCheckBox checkbox1;
    
    checkbox_listener( String titel )
    {   setTitle( titel );
        setLayout( new GridLayout( 2, 1 ) );
        Panel panel1[] = new Panel[2];
        for(int i = 0; i < 2; i++)
        {   panel1[i] = new Panel();
            panel1[i].setSize( 500, 500 );
            add( panel1[i] );
        }
        label1    = new JLabel( "" );
        label1.setFont( new Font( "Arial" , Font.ITALIC , 15 ) );

        checkbox1    = new JCheckBox( "Selection 1" );
        checkbox1.addItemListener(this);
        
        panel1[0].add( label1 );
        panel1[1].add( checkbox1 );
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void itemStateChanged(ItemEvent ie)
    {  Object source = ie.getItemSelectable();
       label1.setText(source.toString());
       if (source == checkbox1)
           label1.setText("Selection 1 -> Checked!");
       if (ie.getStateChange() == ItemEvent.DESELECTED)
           label1.setText("Selection 1 -> NOT Checked!");
    }
     
    public static void main( String[] args )
    {   checkbox_listener  awindow = new checkbox_listener( "Checkbox Listener Example" );
        awindow.setSize( 250 , 120 );
        awindow.setLocation( 200 , 200 );
        awindow.setVisible( true );
    }
}

Open in new window


interface ItemListener catches the ItemEvent when the checkbox is clicked and then calls method itemStateChanged.
The state of the checkbox still has to be queried inside method itemStateChanged...
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
@Thommy,

Your example is for Swing not SWT as required by the question author
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.