Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use a custom Object for a header  RSS feed

 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm trying to create a table where the Header is a custom object.

The custom object would be something along the lines of



This is simpler then what I want to do, but it's the basic concept. I want the column header to render the myLabel for the visual (at least at first). I want this render to be applied to however many columns I have, which will differ from table to table, but I always want the column header of each column to be of type myColumnObject.

I thought somethinglike this would work, but I'm getting java.lang.ClassCastException: java.lang.String cannot be cast to myColumnObject which makes sense, but I thought that I would be getting back my object.

p.s. I re-labeled all my code from my actual project, so if something looks off, it was probably just the re labeling.










 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I wrong in thinking you can override the JLabel of a Column header as a custom object?
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show some more of the stack trace you're getting?
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Can you show some more of the stack trace you're getting?


Sure. I actually took all my code out of my main project, and rebuilt it in a sandbox for easier debugging. If i need to repost the code let me know.

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to com.jason.test.tableheaders.MyColumnObject
at com.jason.test.tableheaders.DefaultTestCellRenderer.getTableCellRendererComponent(DefaultTestCellRenderer.java:27)
at javax.swing.plaf.basic.BasicTableHeaderUI.getHeaderRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableHeaderUI.getHeaderHeight(Unknown Source)
at javax.swing.plaf.basic.BasicTableHeaderUI.createHeaderSize(Unknown Source)
at javax.swing.plaf.basic.BasicTableHeaderUI.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at javax.swing.ViewportLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at javax.swing.ScrollPaneLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at java.awt.BorderLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at java.awt.BorderLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at java.awt.BorderLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at java.awt.Window.pack(Unknown Source)
at com.jason.test.tableheaders.HeaderTester.main(HeaderTester.java:31)
 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parameter called 'value' that is being passed to your getTableCellRendererComponent(..) method is of type String. You are trying to cast it to type MyColumnObject which isn't of type String and hence the ClassCastException.
What is MyColumnObject and does it have any methods that take the value to display as a String?
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my issue is here:



because when i get into



the value of my object is:


com.jason.test.tableheaders.MyColumnObject@7e4736ab

and that becomes the label of the headers if i take out the attempt to cast it .
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The parameter called 'value' that is being passed to your getTableCellRendererComponent(..) method is of type String. You are trying to cast it to type MyColumnObject which isn't of type String and hence the ClassCastException.
What is MyColumnObject and does it have any methods that take the value to display as a String?


YEs,

The point of this whole exercise is to use



as the Object header, and at least to start , use myLabel as the Column render.

The end goal is this object is going to be a bit more complex, with different data for what the header could be, data for what drop downs in the cells of that column might be, default values for new columns, and all kinds of things.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, i want the header of each column to have this storage object that's filled with data/metadata that i use for itself (header label) and it's following column functionality. Does that make sense?
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too often these things go unanswered by the person that eventually figured out their own issue.

I have an approach that works: I Switched to the AbstractTableModel for my model.

I then Provided an ArrayList to the constructor




then I override the getColumnName method

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!