Do you have to change Java Swing code generated by Eclipse VE for a correct program?
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 = (Defaul开发者_运维百科tTableModel) 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;
}
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.
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.
精彩评论