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.
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.
This is how I have done it in previous Swing projects.
In my table model class:
public String getColumnName(int column) {
String s = null;
switch (column) {
case ITEMID_COL: {
s = "ItemId";
break;
}
Then the 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);
I could also easily hide columns by setting the MAX_COLUMNS
.
Questions:
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.
Is this even possible using a Table Viewer?
Alright:
To hide a
TableColumn
, you can basically set its width to0
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:
The only note is what setting column width to
0
does not prevent from calling ofgetText()
method of label provider of that column. If this call must be avoided (it's time consuming, for example), the solution is todispose()
tree column. (To show column again, it must be again created.)