0
    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.

MooHa
  • 819
  • 3
  • 11
  • 23

1 Answers1

1

If the JTable or TableModel are empty and have no column information, then you need to supply that information yourself.

You could either create a new TableColumnModel, add the required TableColumns to it, providing the width information you want/need and apply that model to the JTable.

You would then need to fill in the TableModel and supply that to the JTable as well.

I would, personally, extract the meta data from the ResultSet BEFORE I tried retrieving the data from it.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • you see the method above is returning the model, which includes the columns there already which is used by the table in view. So how can I associate the column size with it on its return? – MooHa Jan 23 '13 at 23:31
  • I'm assuming you're return a `TableModel`, which only carries name and number of column information. The `JTable` uses that information to auto generate the columns (by default, you can change this). You need to, some how, supply the `TableColumnModel` as well. I would change the method to return you own model, containing the `TableModel` and `TableColumnModel` – MadProgrammer Jan 23 '13 at 23:32