File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Getting component in a cell for a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting component in a cell for a JTable" Watch "Getting component in a cell for a JTable" New topic

Getting component in a cell for a JTable

thomas jacob
Ranch Hand

Joined: May 19, 2005
Posts: 91
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.

Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 878
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.

bitguru blog
thomas jacob
Ranch Hand

Joined: May 19, 2005
Posts: 91
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
I agree. Here's the link:
subject: Getting component in a cell for a JTable
It's not a secret anymore!