I am attempting to create my own custom TableModel for my JTable (because I would like to incorporate a row of JCheckBox’s into my table.) I have the JTable in a JScrollPane as well. Before I attempted to incorporate the JCheckBox and custom AbstractTableModel, the JTable would show up fine if I used the default (Object[][], Object[]) constructor. I read in the JTable tutorial on Sun that those constructors use a default of treating all data as Strings.
I then created my custom AbstractTableModel and went from this:
JTable table = new JTable(dataArray, col);
To This:
JTable table = new JTable();
I am assuming that this would call attempt to create the JTable with the custom-made class that extends AbstractTableModel, but now nothing shows up in the JScrollPane.
I am using this incorrectly? I virtually copied the code from the Sun tutorial and only changed the names of the datafiles involved. I also placed this method in the same class. Is there a different way to make sure that your table is created with your custom Table Model? Any insight would appreciated.
JTablehas several constructors that take aTableModelas a parameter. Is that what you’re looking for? From the code snippet you supplied, it seems like you’re calling the default constructor and expecting it to use your custom table model somehow. (Maybe there’s some code missing that does this?). If you use the default constructor,JTablewill internally create aDefaultTableModelinstance and use that.Edit:
Comments don’t take code very well, so adding here: To get the table to use your model, you would do something like this: