Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Advanced understanding of JTables  RSS feed

 
Matt Kurz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.) The following leads to index out of bounds. But not if I do also: tableModel.addColumn(column1); tableModel.addColumn(column2); Why?
Does that mean I have to register the TableColumns on the model, too? I thought initializing the JTable with the TableColumnModel would be enough.



2.) How can I change the column width? I tried:


and


But that does not schow a change. Why? And how have to do it?
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your TableModel has 0 columns and 0 rows, yet when you connect it all by creating the JTable your TableColumnModel expects two columns.
 
Matt Kurz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Your TableModel has 0 columns and 0 rows, yet when you connect it all by creating the JTable your TableColumnModel expects two columns.

Would you please explain what you mean? I have no idea.
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Kurz wrote:

Here you create a TableModel with 0 columns and 0 rows. In other words, it's completely empty.


Here you create two TableColumn objects, one for the first column and one for the second.


Your table now has two columns. Its model still has 0 columns. The table tries to get the model's values for columns 0 and 1 (because they are present in the TableColumnModel) but there are no such columns in the model. Hence the exception.
 
Matt Kurz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood. Thanks! I already read about deviding between data and it's presentation, but I need some practice. How can I change the column width? I tried:



I also tried table.repaint() table.revalidate, fire..., but nothing changed.

Edit: Here it looks easy: example But I do not see, why it does not work in my case.
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The auto resize mode is turned to off in that example.
 
Matt Kurz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:The auto resize mode is turned to off in that example.

Yes, I know. I also tried around that. For instance:

But both columns always have the same width. Would you please show a working example, and explain why all my tries did not work. And in which cases can I expect what from all these variants.

Edit: Ok now I found a working example.

But I have not understood much.

Edit2:
This works too, but the interpretation of the integers is different with setAutoResizeMode on:
 
Martijn van der Geest
Greenhorn
Posts: 5
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the following:

Create a new class and let the class extend DefaultTableModel.
Implement the method getColumnCount and return 2.
Implement the method getColumnName and return the correct name for the index. (ModuleName for index 0 etc.)
Implement other required methods.

Create your JTable based on your custom tablemodel class instance.
Use table.getColumnModel().getColumn(index).setWidth(width) or play around a bit with other width possibilities. (like setPrefferedWidth).

Not sure if this fixes your problem but it at least improves your code since jtable is based on the model/view principle.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!