Reputation: 117
String[] titles = {"System Code","Domain Name","Organizational Unit","Organization Name"};
for(int i=0; i<titles.length; i++)
{
TableColumn column = new TableColumn(table, SWT.LEFT, i);
column.setText(titles[i]);
column.setWidth(150);
column.setMoveable(true);
column.setResizable(true);
}
this code works for me but i want to have an array of TableColum, like this one
Table table;
TableColumn[] columns;
table = new Table(composite, SWT.NONE);
columns = new TableColumn[noOfColumns];
table.setHeaderVisible(true);
but now you see they are not associated with table. How can i associate all these to columns to table ??
Upvotes: 0
Views: 1256
Reputation: 7326
As far as making the columns into an array,
String[] titles = {"System Code","Domain Name","Organizational Unit","Organization Name"};
TableColumn[] columns = new TableColumn[titles.length];
for(int i=0; i<titles.length; i++)
{
TableColumn column = new TableColumn(table, SWT.LEFT, i);
column.setText(titles[i]);
column.setWidth(150);
column.setMoveable(true);
column.setResizable(true);
columns[i] = column;
}
For the second part though, you're trying to get that array into your table?
Are you using javax.swing.table.TableColumn? As it doesnt appear to have setText and setMoveable methods on it. If you are using it, and fixed that in your code, simply add the following code into the for loop (at the end):
tableInstance.addColumn(column);
Or do another iteration afterwards/later on:
for( TableColumn column : columns ) {
tableInstance.addColumn(column);
}
tableInstance if your instance of the JTable class
Here's a full class with all the issues I found fixed up (you wont need all of it, such as the frame declaration, but just to let you see it all):
public class TableTest {
public static void main(String[] args) {
JFrame f = new JFrame();
JTable table = new JTable();
JScrollPane scroll = new JScrollPane(table);
String[] titles = {"System Code","Domain Name","Organizational Unit","Organization Name"};
TableColumn[] columns = new TableColumn[titles.length];
for(int i=0; i<titles.length; i++)
{
TableColumn column = new TableColumn(i);
column.setHeaderValue(titles[i]);
column.setWidth(150);
column.setResizable(true);
columns[i] = column;
table.addColumn(column);//since we add this here, no real point in keeping
//the columns in an array tbh anymore
}
f.add(scroll);
f.setSize(500, 500);
f.setVisible(true);
}
}
Upvotes: 2