SWT-Table Viewer-隐藏列并从列中获取值 [英] SWT - Table Viewer - hiding columns and getting values from columns
问题描述
我正在尝试从表中的数据创建一个arraylist.我需要从可见列中获取值,但我还需要从表中不可见的列中获取值.将SWT与Table Viewer一起使用时,我不知道如何在表格中不显示列.我也不知道如何通过指定列名从表中提取数据.
I am trying to create an arraylist from the data in my table. I need to get the values from the visible columns, but I also need to get values from columns that are not visible in the table. Using SWT with a Table Viewer, I have no idea on how to not display columns in my table. I also have no idea on how to pull the data from the table by specifying column names.
我一直使用Swing,因此我一直使用表模型类.在摆动过程中,创建列,隐藏列并从中获取数据非常简单.
I have always used Swing, so I have always used a Table Model Class. In swing it is pretty simple to create the columns, hide them and get data from them.
这就是我在以前的Swing项目中所做的事情.
This is how I have done it in previous Swing projects.
在我的表中模型类:
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;
}
因此,当我需要任何其他类中的表中的数据时,我所要做的就是
so when I needed the data from my table in any other class, all I had to do was
Object item_id = SingletonSelectTable.getInstance().getValueAt(i, SingletonSelectTable.getInstance().ITEMID_COL);
我还可以通过设置MAX_COLUMNS
轻松隐藏列.
I could also easily hide columns by setting the MAX_COLUMNS
.
问题:
-
我需要学习如何使用表查看器向表中添加不会显示但仍包含值的列.
I need to learn how to add columns to the table that are not going to be displayed but still contain values using a table viewer.
我需要学习如何从表中访问值,以便可以从列中创建可见和不可见数据的数组.
I need to learn how to access the values from the table, so I can create a array of visible and non visible data from the columns.
使用Table Viewer甚至可以做到吗?
Is this even possible using a Table Viewer?
推荐答案
好的:
要隐藏TableColumn
,基本上可以将其宽度设置为0
并防止调整大小.通过将宽度设置为>= 0
取消隐藏它,并启用调整大小.
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.
由于您将TableViewer
与ModelProvider一起使用,因此当您要访问内容时,列被隐藏并不重要.只需从模型中获取对象并从中获取信息即可.
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.
这里是一个示例,它可以隐藏/取消隐藏列并仍然打印当前选定的Person:
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;
}
}
这篇关于SWT-Table Viewer-隐藏列并从列中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!