5

What I'm trying to do is I am creating a JTable inside a new instance of JPanel and JFrame and I am getting the error upon adding the rows in the table:

Object[] column = {"id", "title"};
Object[][] data = {};
JTable toDoTable = new JTable(data, column) {
  public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
      int columnIndex) {
       if(columnIndex == 1) {
          setFont(new Font("Arial", Font.BOLD, 12));
       } else {
           setFont(new Font("Arial", Font.ITALIC, 12));
       }

         return super.prepareRenderer(renderer, rowIndex, columnIndex);
  }
};


JScrollPane jpane = new JScrollPane(toDoTable);
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setSize(new Dimension(1100, 408));
frame.setTitle("JTable Font Setting Example");
panel.add(jpane);
frame.add(new JScrollPane(panel));
frame.setVisible(true);

// Add rows in the Table
DefaultTableModel model = (DefaultTableModel)toDoTable.getModel();
ConnectMSSQLServer connServer = new ConnectMSSQLServer();
ResultSet rs = connServer.dbConnect();
  try
   {
      while (rs.next()) {
          String id = rs.getString("id");
          String title = rs.getString("title");
          model.addRow(new Object[]{id, title});
      }
    }
    catch(Exception e)
    {

    }

The error occurs in the add rows in table

5
  • 1
    This is because getModel() returns a TableModel, which is not necessarily an instance of DefaultTableModel. Presumably you'd need to call toDoTable.setModel, passing an instance of DefaultTableModel. Commented Dec 9, 2015 at 8:37
  • @AndyTurner Thanks but I don't fully understand it. I'm new to Java and learning things out. Any suggestion how to fix it? All I'm doing is adding rows in the table Commented Dec 9, 2015 at 8:38
  • Share the code where you create toDoTable - if you set a DefaultTableModel there, you can retrieve it later. OR if you want to initialize the table from the DB, just create a new DefaultTableModel and set that into your table Commented Dec 9, 2015 at 8:46
  • @Jan I have all the code above. I am creating the table first. Then query the rows in the database then add the rows to the table. You can re recheck my code above. thanks Commented Dec 9, 2015 at 8:48
  • in that case - check my answer :-) Commented Dec 9, 2015 at 8:49

2 Answers 2

7

Your problem here is that you are invoking the JTable(Object[][], Object[]) constructor. If you check out the source code in that link, you can see that it is invoking the JTable(TableModel) constructor internally, having constructed an anonymous instance of AbstractTableModel, which is what is returned by the getModel() method - this can't be cast to a DefaultTableModel.

However: what you are trying to do here won't work anyway. You are saying that the rows of the data are represented by a zero-element array:

Object[][] data = {};

You will not be able to add rows to this, because you can't resize an array once constructed.

Instead of this, you should construct an explicit DefaultTableModel:

TableModel tableModel = new DefaultTableModel(column, rowCount);

and then use this to construct the JTable:

JTable toDoTable = new JTable(tableModel) { ... }

I am not familiar with swing at all, but it looks like DefaultTableModel is backed by a Vector for the row data, so you don't need to know the exact value of rowCount up front.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It works now just what you suggsted. Thanks for the explanation too :)
0

Consider this slightly changed code:

    //Create the new model for the table
    DefaultTableModel model = new DefaultTableModel();

    ConnectMSSQLServer connServer = new ConnectMSSQLServer();
    //Try with catch for auto-closing result set
    try(ResultSet rs = connServer.dbConnect()) {
        while (rs.next()) {
            String id = rs.getString("id");
            String title = rs.getString("title");
            model.addRow(new Object[] { id, title });
        }
    } catch (Exception e) {
        //HANDLE THIS!
    }
    //Now populate the table with the new model
    toDoTable.setModel(model);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.