Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

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!
0
dshrenik
Asked:
dshrenik
  • 3
3 Solutions
 
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
 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now