This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Understanding JTable.addColumn() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Understanding JTable.addColumn()" Watch "Understanding JTable.addColumn()" New topic
Author

Understanding JTable.addColumn()

Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Hello folks,

this is a hacky approach at understanding swing's JTable, based on Sun's Demo Application. After clicking into the table, a new column is added and displayed (my custom datamodel registers as listener to the columnAdded-Event). I'm expecting to have cell-value for this new column = "argh" but the value is the same as it is for the first column (Mary). I don't get this. What am I missing here? The TableDemo.debugColumnAdd output at least assumes that argh is returned for the new column. So why/how are the renderers calling tableModel.getValueAt(...)? Instead of using columnIndex 5 they wrap back to 0.

The main class:


My custom tableModel:


1. Make it run - 2. Make it run correctly - 3. Make it pretty OR fast/small - 4. ??? - 5. Profit
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Solution:
TableColumn foo = new TableColumn(); creates a new TableColumn with a model index of 0! That's why the code always returns Mary (getValueAt(0,0)) instead of the expected argh (getValueAt(0,5))
TableColumn foo = new TableColumn(5); is the solution for adding one column, otherwise TableColumn foo = new TableColumn(tableModel.getColumnCount); would be for adding arbitrary numbers of columns.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding JTable.addColumn()
 
Similar Threads
JTable
Checkbox editor for JTable column
jtable, table model, column headers
JTable and its size
Problem with JTable