0

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));
        }
    }
mKane848
  • 149
  • 1
  • 10
  • 2
    A bit unrelated, but: [Please don't use `Vector`](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) – Baz Oct 08 '14 at 06:42
  • So, what exactly isn't working? – Baz Oct 08 '14 at 09:42
  • Also, thanks for the heads up about Vectors. I'll definitely remember that for future projects! – mKane848 Oct 08 '14 at 17:20

1 Answers1

2

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();
}
Baz
  • 36,440
  • 11
  • 68
  • 94
  • Alright, awesome. I think I want to add another column (just to have a number next to each line) so that extra bit you added helps greatly! The answer alongside the info about Vectors is great, thanks for your time – mKane848 Oct 08 '14 at 19:53
  • 1
    Sorry! Thought I hit it before I closed the page – mKane848 Oct 08 '14 at 22:31