JButton in JTable

One of the columns in my JTable has JButtons (1 JButton in a cell).

Depending on the cell in which the button is present, I want to execute a particular function.

Please let me know how I can do this. Thanks!
dshrenikAsked:
Who is Participating?
 
for_yanCommented:

this is conveninet method with MouseListener to determine which cell was clicked
You can probebly combimne this with action listener to act only on the click on the button inside the cell
public void mouseClicked(MouseEvent e) {
int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint());
System.out.println("Row = " + row + ", Column = " + col);

Open in new window

0
 
for_yanCommented:
When you add JButtons to your Componenets in the TableCellRenderer name your JButtons differently - adn you can pass
instances of JButton to the class which handles the ervent and then in the
even handler check what buttoin was the source of the event

actiopnPerformed(AcrtionEvent ae) {

if(ae.getSource().equals(button1)){
//do somthing}
else
{
//do spemthing else

}
0
 
for_yanCommented:
In general there is conevnient method of hadnling events from the JTable based on the location of mouseclick point
in which cell it was clicked  - but in this case it will also dal with events  if you click in the cvell but not on the button
0
 
CEHJCommented:
>>
Depending on the cell in which the button is present, I want to execute a particular function.

Please let me know how I can do this. Thanks!
>>

Buttons in a table behave no differently from elsewhere. Add an Action/ActionListener to the button to make it do what you want
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.