Problems with extending JTables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JHuman
    New Member
    • Feb 2009
    • 5

    #1

    Problems with extending JTables

    Hey guys, I'm having a few problems with a JTable subclass I'm making. The constructor:

    Code:
    public CustomTable()
      {
        super();
        TableColumn col = new TableColumn();
        col.setCellRenderer(new CustomCellRenderer());
        col.setHeaderValue("Blah");
        DefaultTableModel model = ((DefaultTableModel)getModel());
        [B]model.addColumn(col);[/B]
        getColumnModel().addColumn(col); 
      }
    When I call addRow(), it gives me an array index out of bounds exception if I do not call the bolded line. However, when I do call that line, I get duplicate columns appearing (one in the table model, one in the column model). I tried not adding the column to the column model, and that works, but then when the column is displayed only via the table model, any custom cell renderers I use don't work, and the column doesn't display the header value I set it to either (just displays the toString() of the TableColumn object).

    Any help? What is the proper way to add columns?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Only add your new column to your DefaultTableMod el given its name. The column will be intially empty but you can fill it yourself later. See the API documentation for the DefaultTableMod el class.

    kind regards,

    Jos

    Comment

    Working...