How listen for check box in JFace Table Viewer
Asked Answered
B

3

8

I am using a Table Viewer with check boxes as following:

final TableViewer legendViewer = new TableViewer(parent, SWT.CHECK);

What is THE solution to listen to check boxes selection/unselection in this viewer ?

Thanks in advance, Manu

Benedicite answered 1/7, 2011 at 6:57 Comment(0)
A
9

You listen for SWT.Selection events on the Table and check for event.detail == SWT.CHECK...

See this example for actual code....

Aundreaaunson answered 1/7, 2011 at 7:15 Comment(0)
E
13

Take a look at class CheckboxTableViewer derived from TableViewer which simplifies the use of a TableViewer with the SWT.CHECK style.

Add an implementaion of ICheckStateListener to your CheckboxTableViewer via addCheckStateListener(). The only method you have to implement is checkStateChanged(CheckStateChangedEvent event) where event contains all necessary information about the state change.

If you need only one column you can create a CheckboxTableViewer like this:

CheckboxTableViewer myTableViewer = CheckboxTableViewer.newCheckList(parent, style);
Expertise answered 1/7, 2011 at 7:20 Comment(0)
A
9

You listen for SWT.Selection events on the Table and check for event.detail == SWT.CHECK...

See this example for actual code....

Aundreaaunson answered 1/7, 2011 at 7:15 Comment(0)
A
0

Expanding @Tonny's answer, here's the listener you would use:

tableViewer.getTable().addListener(SWT.Selection, (e) -> {      
    if (e.detail == SWT.CHECK) {
        MyModel modelObj = (MyModel) e.item.getData();
        TableItem item = (TableItem) e.item;
        System.out.println("ITEM CHECKED -> " + item.getChecked());
    }
});

You see, you can cast the widget to TableItem to understand whether the user has either checked or unchecked the checkbox.


Still, as @Claimos' says, I would just use a JFace CheckboxTableViewer.

Aegean answered 5/6, 2019 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.