title: Java Error : javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel
body: <p>What I'm trying to do is I am creating a <code>JTable</code> inside a new instance of <code>JPanel</code> and <code>JFrame</code> and I am getting the error upon adding the rows in the table:</p>
  
  <pre><code>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)
      {
  
      }
  </code></pre>
  
  <p>The error occurs in the add rows in table</p>