Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting component in a cell for a JTable

 
thomas jacob
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Jtable contains several Jcomponents like the JLabel, JTextButton. Is there a possible way to get the components instance in say eg: row 1 and column 1.

Regards
Thomas
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your table model actually contain JComponents, so that calling yourTable.getModel().getValueAt(1, 1) would return something like a JLabel?

If so, then just call yourTable.getModel().getValueAt(row, col). But this is a very weird way to do things. Unless you are writing something like a GUI builder, then I would say it's a mistake to do it this way.

Usually your table model will hold values of type String, Integer, Boolean, and so on. In this case there isn't really a JComponent for any particular row/col. The table will use one or more components to render the value, but it is associated with a particular row/col for only a fleeting moment, then is reused elsewhere. This is how TableCellRenderers work.

What is odd is that you are the third person over the last few days to ask a question about accessing the "component" for a table cell. Either this is a weird coincidence, or the situation is something like you are all in the same class and your instructor hasn't explained things very well.

In any case, there just isn't any kind of "component instance" that you can "get" for a particular row/col, unless you want to get it briefly while rendering or editing is in progress.
 
thomas jacob
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am involved in a forte conversion project. I am trying to implement an ArrayField feature of Forte using the best match JTable in java. The ArrayField has a feature to return a component back
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic