public TableModel getTableData() {
TableModel model=null;
try {
String sql = "SELECT eventID as 'Event ID', date as 'Date',eventName as 'Name', time as 'Start Time' FROM Event";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
model = (DbUtils.resultSetToTableModel(rs));
//CODE TO SET WIDTH OF JTABLE IN VIEWS
}
catch(SQLException e){
e.printStackTrace();
}
finally {
try {rs.close(); pst.close();}
catch(SQLException e){} }
return model;
}
Above is a method of the Model class, responsible for setting up the table model for the table I have in Views called tableEvent
.
The Controller class uses the above method to set change the state of the tableEvent
of Views
Class. Please note tableEvent
in Views is empty/nothing there. Model passes values of rs
to Controller
which will then change the display of the table and populate values.
Heres what I mean:
view.tableEvent.setModel(model.getTableData());
However, I am trying to set the width of the columns in Model
rather than inViews
through tableEvent.getColumnModel().getColumn(1).setPreferredWidth(60);
. if I pass the table itself to the method getTableData()
in order to set the width of the column, it doesnt really work. Is there a way I can specify my desired width of the first 4 column of the table in Model class in geTableData() method somehow so that it is consistent at all times. Right now I have this at part of the constructor of Views class which obviously means its only to my desired width only at initialisation.