CheckboxTableViewer first column select all

你离开我真会死。 提交于 2020-01-16 00:58:19

问题


I am currently developing a Wizard that will take me through a couple of steps. On one of the WizardPages I want to have a dynamically filled table where the user selects some of the rows. I am using a CheckboxTableViewer for this. Currently it works well (including dynamic number of columns, etc.) and the result looks like in the screenshot.

My problem with this is that the first column spans over the column with the checkboxes. I would like to have the following behaviour:

  • The first column header ("nodeAlias" in the screenshot) should start on top of the column, not above the checkboxes (so it needs to shift a bit to the right)
  • I want to have a checkbox in the header above the checkboxes that performs a "select/deselect all"

My code that I am using in my wizard page (extends WizardPage) looks like this:

public class ShowResults extends WizardPage {
    private Table table;

    public ShowResults() {
        super("wizardPage");
        setTitle("Title");
        setDescription("Description");
    }

    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NULL);

        setControl(container);
        container.setLayout(new GridLayout(1, false));

        CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER | SWT.FULL_SELECTION);
        createColumns(checkboxTableViewer);
        table = checkboxTableViewer.getTable();
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        table.setHeaderVisible(true);
        table.setLinesVisible(true); 

        checkboxTableViewer.setContentProvider(ArrayContentProvider.getInstance());

        checkboxTableViewer.setInput(getData()); // returns a list of Record-Objects
    }


    private void createColumns(CheckboxTableViewer checkboxTableViewer) {
        // get the required columns as list and act for each
        for(String columnName : getColumnNames()){
            // create a column for the first name
            TableViewerColumn colFirstName = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
            colFirstName.getColumn().setWidth(150);
            colFirstName.getColumn().setText(columnName);
            colFirstName.setLabelProvider(new ColumnLabelProvider() {
              @Override
              public String getText(Object element) {
                Record r = (Record) element;
                return r.getValue(columnName);
              }
            });
        }

    }

I've not included the class definition for the Record as the displaying of information works anyway. (I guess it is not of importance for this problem).


回答1:


Why not take a look at Nebula grid widget? you can write your own CellRenderers for the column headers. See here




回答2:


An alternative solution could be not to use a header. Only act like you had one:

Keep a row as a fixed first element in your table, and add a unique setter to the element behind that special model. It should not only set it's own value but also other elements values in the list. It's other members will act as a static header.

Otherwise, if you manage to get the checkbox in the header, and it works on your platform (MacOS as it looks), it might still not work on an another.



来源:https://stackoverflow.com/questions/31944765/checkboxtableviewer-first-column-select-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!