• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1176
  • Last Modified:

How to add a listener to a checkbox in Java?

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;
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption
    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

  • 2
1 Solution
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?
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" );
        panel1[0].add( label1 );
        panel1[1].add( checkbox1 );

    public void itemStateChanged(ItemEvent ie)
    {  Object source = ie.getItemSelectable();
       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...
mccarlIT Business Systems Analyst / Software DeveloperCommented:

Your example is for Swing not SWT as required by the question author
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now