How can I put a "(de)select all" check box in an SWT Table header?
Asked Answered
B

3

8

I have an SWT Table that I'm instantiating with the SWT.CHECK style in order to display a check box next to every row. My users have requested another check box in the header row of the table in order to allow them to select/deselect all the rows with one click.

I can't see any obvious way to do it, and I've only found Swing/JTable examples through Google. Does anyone know how to do this? I'm hoping it's possible without re-implementing Table or falling back on a header context menu.

Brahmaputra answered 19/8, 2010 at 10:44 Comment(0)
T
7

Just create two images of check box. First one without a tick and second one having a tick.Now add the first image to the tableColumn header. After that add listener to tableColumn in such a way that when you click button for the first time, table.selectALL() method should be fired along with changing the tableColumn header image to second one. When you click button again call table.deSelectAll() method and replace the tableColumn header with the first image.

You can use this condition:

When the checkbox(image) is clicked, use a for loop to check whether, any of the checkboxes in the table is checked. if anyone is found checked then fire table.deSelectAll() method , else fire table.selectAll() method.

There will not be any problem for the "checkbox" during table/widow resizing.

tableColumn0.addListener(SWT.Selection, new Listener() {
    @Override
    public void handleEvent(Event event) {
        // TODO Auto-generated method stub
        boolean checkBoxFlag = false;
        for (int i = 0; i < table.getItemCount(); i++) {
            if (table.getItems()[i].getChecked()) {
                checkBoxFlag = true;
            }
        }

        if (checkBoxFlag) {
            for (int m = 0; m < table.getItemCount(); m++) {
                table.getItems()[m].setChecked(false);
                tableColumn0.setImage(new Image(Display.getCurrent(),
                        "images/chkBox.PNG"));

                table.deselectAll();

            }
        } else {
            for (int m = 0; m < table.getItemCount(); m++) {
                table.getItems()[m].setChecked(true);
                tableColumn0.setImage(new Image(Display.getCurrent(),
                        "images/chkBox2.PNG"));

                table.selectAll();
            }
        }

    }
});
Turenne answered 28/6, 2011 at 6:11 Comment(0)
G
4

You could use a FormLayout to allow stacking objects, then add a checkbox on top of the table as follows:

FormData fd = new FormData();
fd.left = new FormAttachment(table, 5, SWT.LEFT);
fd.top = new FormAttachment(table, 5, SWT.TOP);
checkbox.setLayoutData(fd);
checkbox.moveAbove(table);

You might find it useful for correctly aligning the checkbox to obtain the height of the table header row with table.getHeaderHeight().

Gershon answered 29/8, 2010 at 12:3 Comment(2)
Sorry for the slow response. I've been meaning to try this out for ages. Will give it a go and accept if it does what I think it will :) Thanks!Brahmaputra
No problem. Drop a comment if it doesn't work out as expected.Gershon
E
0

Fully describe this code :: de)select all” check box in an SWT Table header


public class TaskView extends ViewPart {

    public static TableItem std_item;
    public static List<Student> std=new ArrayList<Student>();
    public static Table table;
    private TableColumn col_name_add;
    private TableColumn col_image_add;
    static int countcheck;
    static int  staticno=1;
    static int check=0,uncheck=0;

    public TaskView() {
        setTitleImage(ResourceManager.getPluginImage("RCP_Demo", "icons/Tasksview.png"));
    }

    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(null);

        ////////// Table Create
        table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK|SWT.CENTER);
        ////SWT.CHECK: Display first column check box

        table.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {

                TableItem item = table.getItem(table.getSelectionIndex());
                for(int col=1;col<table.getColumnCount();col++)
                {
                    //Table_Column Checked or Not
                    if(item.getChecked())
                        item.setChecked(false);
                    else
                        item.setChecked(true);

                    /////////First column value get
                    if(col==1)
                    {
                        System.out.println(item.getText(col));
                    }
                    TableItem[] itemCheck = table.getItems();
                    for(int i=0;i<table.getItemCount();i++)
                    {
                        if(itemCheck[i].getChecked())
                            ++check;
                        else
                            ++uncheck;
                    }
                    if(check==table.getItemCount())
                        //Change column image:Checkbox checked
                        col_image_add.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/check.png"));
                    else
                        //Change column image:Checkbox Unchecked
                        col_image_add.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/uncheck.png"));

                    //System.out.println("Check:"+check+"uncheck"+uncheck);
                    check=0;
                    uncheck=0;
                }
            }
        });
        table.setBounds(10, 10, 343, 297);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        ////// SWT Table header Column
        col_image_add = new TableColumn(table, SWT.LEFT);
        col_image_add.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                //All Row selected or Not
                //column Icon change checked(selected) or not

                System.out.println("Total Row Count:"+table.getItemCount());
                TableItem item[] = table.getItems();
                if(staticno==1)
                {
                    for(int i=0;i<table.getItemCount();i++)
                    {
                        item[i].setChecked(true);
                        col_image_add.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/check.png"));
                    }
                    staticno=0;
                }else
                {
                    for(int i=0;i<table.getItemCount();i++)
                    {
                        item[i].setChecked(false);
                        col_image_add.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/uncheck.png"));
                    }
                    staticno=1;
                }
            }
        }
        });
        col_image_add.setMoveable(true);
        col_image_add.setToolTipText("Click");
        col_image_add.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/uncheck.png"));
        col_image_add.setWidth(36);

        //Dynamic column Name add

        String[] Col_names={"Stud_id","Stud_Name","Stud_Gender"};
        for(int i=0;i<Col_names.length;i++)
        {
            col_name_add = new TableColumn(table,SWT.CENTER);
            col_name_add.setWidth(100);
            col_name_add.setText(Col_names[i]);
        }
    }
    public TableViewer getViewer() {
          return null;
    }
}

thanks....

Eyas answered 19/1, 2017 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.