views:

42

answers:

2

Using the Visual Editor in Eclipse I started a Swing UI containing a table (JTable) with 2 columns (JTableColumn). Adding data to the table this way:

final DefaultTableModel model = (DefaultTableModel) this.jTable.getModel();
model.addRow(new Object[] {"Column 1", "Column 2"});

generated an ArrayIndexOutOfBoundsException. I solved this by setting the number of columns of the model backing the table:

model.setColumnCount(this.jTable.getColumnCount());

But after this call, the column headers of the table I defined using the UI editor, are changed to "A" and "B". Now I'm wondering if I should go on and correct the generated code like I did, or is there a better way to build UI's with Visual Editor?

To be complete, this is the generated code to define the table and columns:

private JTable getJTable() {
    if (this.jTableSongs == null) {
        final TableColumn tableColumn1 = new TableColumn();
        tableColumn1.setHeaderValue("Header 1");
        final TableColumn tableColumn2 = new TableColumn();
        tableColumn2.setHeaderValue("Header 2");
        this.jTableSongs = new JTable();
        this.jTableSongs.addColumn(tableColumn1);
        this.jTableSongs.addColumn(tableColumn2);
    }
    return this.jTable;
}
+1  A: 

I don't know about this specific problem (table columns) but in the past i had similar problems with VE.

If you don't have the number of columns in the Property View, you have to specify it programmatically, editing the code, but VE will not remove hand added code.

onof
Yeah, but I don't want to hand code the GUI.
Kwebble
+1  A: 

or is there a better way to build UI's with Visual Editor?

The problem with using code generators is that you spend more time learning the IDE and not learning Java. The better approach is to use the IDE for debugging and so on and build the GUI's yourself so you are in full control and the code can be moved from one IDE to another.

I don't know what the generated code should look like but the following looks inconsistent:

this.jTable = new JTable(); 
this.jTableSongs.addColumn(tableColumn1); 
this.jTableSongs.addColumn(tableColumn2); 

A jTable variable is created (and returned from the method) but the columns are added to jTableSongs. So it looks to me like jTable has 0 columns which could cause an Exception.

camickr
I don't think of a GUI builder as a code generator. I'd like it to be a replacement for coding, so that I can focus on creating the layout and connecting it to the backend. The error in the code was a typo in the post and is corrected. In the class the names are all jTableSongs.
Kwebble