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

前端 未结 2 628
太阳男子
太阳男子 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:27

    In my cell, I have two buttons and three labels; they are all in one panel.

    You are correct to use a TableCellRenderer and TableCellEditor. In this complete example, the StatusEditor queries the enclosing StatusPanel and returns a suitable value in its implementation of getCellEditorValue().

    0 讨论(0)
  • 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
           }
        }
    });
    
    0 讨论(0)
提交回复
热议问题