SWT - Table Viewer - hiding columns and getting values from columns

孤者浪人 提交于 2019-12-05 21:49:44

Alright:

To hide a TableColumn, you can basically set its width to 0 and prevent resizing. Unhide it by setting the width to something >= 0 and enable resizing.

Since you are using a TableViewer with a ModelProvider, it does not matter that the columns are hidden when you want to access the content. Just get the Object from the model and get your information from it.

Here is an example that can hide/unhide the columns and still print the currently selected Person:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));

    final TableViewer viewer = new TableViewer(shell, SWT.READ_ONLY);

    // First column is for the name
    TableViewerColumn col = createTableViewerColumn("Name", 100, 0, viewer);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if(element instanceof Person)
            {
                System.out.println("1");
                return ((Person)element).getName();
            }
            return "";
        }
    });

    // First column is for the location
    TableViewerColumn col2 = createTableViewerColumn("Location", 100, 1, viewer);
    col2.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if(element instanceof Person)
            {
                System.out.println("2");
                return ((Person)element).getLocation();
            }
            return "";
        }
    });

    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    data.horizontalSpan = 2;
    table.setLayoutData(data);

    /* This button will hide/unhide the columns */
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Hide / Unhide");

    button1.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            for(final TableColumn column : table.getColumns())
            {
                if(column.getWidth() == 0)
                {
                    column.setWidth(100);
                    column.setResizable(true);
                }
                else
                {
                    column.setWidth(0);
                    column.setResizable(false);
                }
            }
        }
    });

    /* This button will print the currently selected Person, even if columns are hidden */
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Print");

    button2.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            Person person = (Person) selection.getFirstElement();

            System.out.println(person);
        }
    });

    viewer.setContentProvider(ArrayContentProvider.getInstance());

    final Person[] persons = new Person[] { new Person("Baz", "Loc"),
            new Person("BazBaz", "LocLoc"), new Person("BazBazBaz", "LocLocLoc") };

    viewer.setInput(persons);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

private static TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber, TableViewer viewer) {
    final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
    final TableColumn column = viewerColumn.getColumn();
    column.setText(title);
    column.setWidth(bound);
    column.setResizable(true);
    column.setMoveable(false);

    return viewerColumn;
}

public static class Person {
    private String name;
    private String location;

    public Person(String name, String location) {
        this.name = name;
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String toString()
    {
        return name + " " + location;
    }
}

The only note is what setting column width to 0 does not prevent from calling of getText() method of label provider of that column. If this call must be avoided (it's time consuming, for example), the solution is to dispose() tree column. (To show column again, it must be again created.)

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