SWT – 表查看器 – 隐藏列并从列中获取值

我正在尝试从表格中的数据创建一个arraylist。 我需要从可见列中获取值,但我还需要从表中不可见的列中获取值。 将SWT与表查看器一起使用,我不知道如何不在表中显示列。 我也不知道如何通过指定列名从表中提取数据。

我一直使用Swing,所以我一直使用Table Model Class。 在swing中,创建列,隐藏它们并从中获取数据非常简单。

这就是我在以前的Swing项目中完成的方法。

在我的表模型类中

public String getColumnName(int column) { String s = null; switch (column) { case ITEMID_COL: { s = "ItemId"; break; } 

那么getValueAt()

  public Object getValueAt(int row, int column) { Object o = null; try { switch (column) { case ITEMID_COL: { o = rds.get(row).rev.getItem().getStringProperty("item_id"); break; } 

因此,当我需要其他课程中的表格中的数据时,我所要做的就是

 Object item_id = SingletonSelectTable.getInstance().getValueAt(i, SingletonSelectTable.getInstance().ITEMID_COL); 

我也可以通过设置MAX_COLUMNS轻松隐藏列。

问题

  1. 我需要学习如何向表中添加不会显示但仍使用表查看器包含值的列。

  2. 我需要学习如何从表中访问值,因此我可以从列中创建可见和不可见数据的数组。

  3. 这甚至可以使用表格查看器吗?

好的:

要隐藏TableColumn ,您基本上可以将其宽度设置为0并防止resize。 通过将宽度设置为>= 0来取消隐藏它并启用resize。

由于您将TableViewer与ModelProvider一起使用,因此在您要访问内容时隐藏列并不重要。 只需从模型中获取对象并从中获取信息。

这是一个可以隐藏/取消隐藏列并仍然打印当前所选人员的示例:

 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; } } 

唯一的注意事项是将列宽设置为0不会阻止调用该列的标签提供程序的getText()方法。 如果必须避免此调用(例如,这是耗时的),解决方案是dispose()树列。 (要再次显示列,必须再次创建。)