Reputation: 159
I'm self-teaching Java GUIs and am trying to implement an older text-based program I made into something with an actual menu. In using WindowBuilder on Eclipse for formatting and internet resources to help, but I've hit a bit of a roadblock.
I've got a one-dimensional vector that can hold any number of strings. I'd like to display it in a table, but I'm not quite sure if I'm doing it right. All of my back-end stuff works fine, I'm just really struggling with the GUI. Any tips/corrections/resources you're willing to offer are greatly appreciated! I've been bumbling around for hours and I fear I've hit a wall. To be honest I'm having trouble testing the GUI within WindowBuilder as well, but that's another story. The relevant code in my GUI class is as follows:
Vector<String> demo = new Vector<String>();
//nonsense elements just for the sake of debugging
demo.addElement("Line1");
demo.addElement("Line2");
demo.addElement("Line3");
demo.addElement("Line4");
demo.addElement("Line5");
demo.addElement("Line6");
table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableItem item;
for(int i = 0; i < demo.size(); i++)
{
// Create a new TableItem for each line in the vector (each row)
item = new TableItem(table, SWT.NONE);
for (int j = 1; j <= demo.size(); j++) {
// Populate the item
item.setText(j - 1, demo.get(j));
}
}
Upvotes: 0
Views: 292
Reputation: 36894
The problem is this line:
item.setText(j - 1, demo.get(j));
You only have one column (since you didn't create any yourself, the table assumes there is only one column), but using TableItem#setText(int, String)
sets the text in column i
(which is only equal to 0
for one of your items).
So, if you've just got one column, use this:
item.setText(demo.get(j));
or
item.setText(0, demo.get(j));
If you've got more colums, create them before adding items (new TableColumn(table, SWT.NONE)
), then add your items using:
for(int i = 0; i < items.size(); i++)
{
TableItem item = new TableItem(table, SWT.NONE);
for(int j = 0; j < table.getColumnCount(); j++)
{
item.setText(j, "something here");
}
}
Then afterwards you have to pack()
the columns:
for(TableColumn col : table.getColumns())
{
col.pack();
}
Upvotes: 2