how to select entire tableviewer row when checkbox clicked (first column of the table has checkboxes in each row)

StackOverflow https://stackoverflow.com/questions/22344534

  •  13-06-2023
  •  | 
  •  

Question

am beginner to SWT, anyone please help, my problem is: i have a table, which has 10 Columns, the first column has Check box and the rest of the column are filled from Database, when i check the checkbox i want that perticular row to be selected,

i am pasting my table code below,

    public static void main(String arg[])
    {
      // shell and display declared here

            mainTable = new TableViewer(content,  SWT.H_SCROLL| SWT.V_SCROLL | SWT.BORDER |SWT.DM_FILL_BACKGROUND|SWT.FULL_SELECTION);

            mainTable .getTable().setHeaderVisible(true);
            mainTable .getTable().setLinesVisible(true);
            mainTable .setContentProvider(new ArrayContentProvider());    

            // COLUM FOR PLACING THE CHECK BOXES
            TableColumn columncOL=new TableColumn(mainTable .getTable(),SWT.NONE);
            columncOL.setText("");
            columncOL.setWidth(40);

            TableViewerColumn columnaC=new TableViewerColumn(mainTable ,columncOL);
            columnaC.setLabelProvider(new ColumnLabelProvider(){

            Map<Object, Button> buttons = new HashMap<Object, Button>();
            @Override
                public void update(ViewerCell cell) {

                    TableItem item = (TableItem) cell.getItem();
                            Button button;
                            if(buttons.containsKey(cell.getElement()))
                            {
                                button = buttons.get(cell.getElement());
                            }
                            else
                            {
                                button = new Button((Composite) cell.getViewerRow().getControl(),SWT.CHECK);

                                String s = "Item 1";

                                button.setData(s, "Some other info or object here");
                                s = "Item 2";

                                button.setData(s, "This is item two");
                                //buttons.put(cell.getElement(), button);
                            }
                            TableEditor editor = new TableEditor(item.getParent());
                            editor.grabHorizontal  = true;
                            editor.grabVertical = true;
                            editor.setEditor(button , item, cell.getColumnIndex());
                            editor.layout();
                        }

                    });

                    // CREATING 1st COLUMNS
            TableColumn column1Head=new TableColumn(mainTable.getTable(),SWT.NONE);
                    column1Head.setText("col1");
                    column1Head.setWidth(200);
                    // setting column input
            TableViewerColumn column1=new TableViewerColumn(mainTable,column1Head);
                    column1.setLabelProvider(new ColumnLabelProvider()
                    {
                        public String getText(Object Element)
                        {
                            Student ap=(Student )Element;
                            return ap.col1();
                        }
                    });

    }

like this am creating remaing 8 columns i want to select the entire row when a perticluar check box checked . any one pleeeease help me As early as possible....

Était-ce utile?

La solution

Use CheckboxTableViewer to show a table with check boxes:

mainTable = CheckboxTableViewer.newCheckList(content, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);

To select the line when a check box is ticked you will need to use addCheckStateListener to select the checked row, something like:

mainTable.addCheckStateListener(new ICheckStateListener()
 {
   @Override
   public void checkStateChanged(CheckStateChangedEvent event)
   {
     if (event.getChecked())
       mainTable.setSelection(new StructuredSelection(event.getObject()));
   }
 });

Autres conseils

Easy way :first column of the table has checkboxes in each row can selected or not


CheckboxTableViewer tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.FULL_SELECTION);
Table table = tableViewer.getTable();
table.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event e) {
             Table table = (Table) e.widget;
                TableItem item = table.getItem(table.getSelectionIndex());
                for (int col = 0; col < table.getColumnCount(); col++)
                {
                    //Table_Column Checked or Not
                    if(item.getChecked())
                        //return true
                        //all ready checked(true) to change unchecked(false)
                        item.setChecked(false);
                    else
                        //unchecked(false) to checked(true) each row
                        item.setChecked(true);  
                }
        }           
    }               

this is useful for you....thanks

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top