• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Advanced understanding of JTables

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 5
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic