0

Just want to know can we retrieve cell of JTable as a JComponent and can we calculate the area of each cell of jtable so that i can bound to the user to click on that particular area.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Java_Alert
  • 1,159
  • 6
  • 24
  • 50

2 Answers2

3

Try a JTable tutorial first.

A JTable uses a table cell renderer, which delivers a JComponent for rendering a table cell. An efficiency optimization exists, in that in general (with the DefaultTableCellRenderer) the same JComponent is reused, and adapted to the content of the TableModel cell.

(Besides the renderer there is the table cell editor.)

In your case you simply can override a DefaultTableCellRenderer and do a jtable.setRenderer.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
3

Can we retrieve cell of jtable as a JComponent?

In short, no. To paint a JTable, Swing uses the concept of renderer. This means that instead of a having a live component for each cell, we re-use the same component to paint a whole column or several columns of a JTable. This means that the JTable will call the renderer with appropriate parameters, set its size and location to the corresponding cell, invoke the paint method, and then move the component further until the whole table is painted. This means that there is no child components for each cell inside the JTable. See more details on all this here.

Now, if you want to bind a mouse listener, you can do it directly on the JTable and you can use getCellRect to find out if the click is inside a particular cell, or use columnAtPoint and rowAtPoint to find out which cell was clicked.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117