views:

607

answers:

4

Hello. 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. Thank you for your time.

+1  A: 

JTable has several constructors that take a TableModel as 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, JTable will internally create a DefaultTableModel instance 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:

MyTableModel model = new MyTableModel();
// ...initialise model if required
JTable table = new JTable(model);
Ash
I guess that is what I am confused on. The JTable info on the Sun web page states: There are two JTable constructors that directly accept data (SimpleTableDemo uses the first):JTable(Object[][] rowData, Object[] columnNames)JTable(Vector rowData, Vector columnNames)It then states:The advantage of these constructors is that they are easy to use. However, these constructors also have disadvantages:They treat all data types the same (as strings).
MarcZero
So reading on, I am under the impression that I have to create a custom class that extends AbstractTableModel and somehow a table I create will use that instead of the default constructor one. If I use the JTable(Object[][], Object[]) constructor, it does treat every cell as Strings, so I "assume" that calling the method without the constructor: JTable() will use the custom one I created, but my screen is now blank. I can go back to the default constructor and it shows up just fine, but I can't seem to get it to use my custom Table Model.
MarcZero
@MarcZero: See edits in my answer. (If it doesn't make sense, maybe post some more of your code. I get this feeling that I might be missing something in what you're trying to do...)
Ash
That was it. I can get the JTable to work show now, although getting it to render a checkbox is another mater entirely. I'll have to post another question. Thanks!
MarcZero
trashgod's answer (http://stackoverflow.com/questions/2259815/getting-a-jtable-with-a-custom-table-model-to-show-up-in-jscrollpane/2259916#2259916) has info on the check box.
Ash
A: 

OK. After reviewing my code I realized that if I am leaving out any constructors, it will not find the link to your custom Table Model. So, if you created the class:

class MyTableModel extends AbstractTableModel {
//code here
}

You need to instantiate it in the JTable constructor like this:

JTable table = new JTable(new MyTableModel());

So you cannot just leave it blank and expect it to "find" the new AbstractTableModel class.

MarcZero
+1  A: 

As you observed, Ash is right on about passing your data model in the JTable constructor. If your model's getColumnClass() returns Boolean.class, you'll get a check box renderer by default. You might like this example that illustrates using a custom renderer and editor.

trashgod
A: 

You need to extend AbstractTableModel, and pass this as a parameter for the constructor of your JTable. (As Marc does). In addition to the required method, you need to define this method to show the actual checkboxes:

 public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

This tells you JTable how to render each cell. If you dont override this, it will just be showed as a string.

Frederik Wordenskjold