Is it possible to detect a button clicked inside a table cell?

前端 未结 2 646
太阳男子
太阳男子 2020-12-21 18:56

I a swing application in which i have a table inside it i\'m putting a panel that can contain a button.The code is follow

 public class MyCellDataRenderer im         


        
2条回答
  •  太阳男子
    2020-12-21 19:40

    Yes it is possible and there are few ways how to do it based on your application design. As I do not know the details I would suggest this simple solution:

    table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
           final int row = table.rowAtPoint(e.getPoint());
           final int column = table.columnAtPoint(e.getPoint());
    
           if(isButtonCell(row, column)) { //check if the cell is your button
               handleButtonClicked(); //invoke the click button handle
           }
        }
    });
    

提交回复
热议问题