是否可以检测在表格单元格内单击的按钮?

我是一个swing应用程序,其中我有一个表格,我正在放置一个可以包含按钮的面板。代码如下

public class MyCellDataRenderer implements TableCellRenderer, TableCellEditor { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { MyCellData myCellData = (MyCellData) table.getValueAt(row, column); JPanel panel = GridBagHelper.createPanel(); if (myCellData.isATableHeader()) { panel.setBackground(myCellData.getCellBackgroundColor()); panel.add(myCellData.getContenant(), GridBagHelper.createGridBagConstraints(0, 0, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH)); return panel; } boolean condition=true; if (condition==true) { panel.setBackground(myCellData.getCellBackgroundColor()); panel.add(myCellData.getContenant(), GridBagHelper.createGridBagConstraints(0, 0, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH)); return panel; } panel.setBackground(myCellData.getCellBackgroundColor()); panel.add(myCellData.getContenant(), GridBagHelper.createGridBagConstraints(0, 0, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH)); return panel; } 

我的问题是我能否检测到包含在面板内的按钮的点击? 我问的是技术上是否可行?

谢谢

在我的手机中,我有两个按钮和三个标签; 他们都在一个小组中。

使用TableCellRendererTableCellEditor是正确的。 在这个完整的示例中 , StatusEditor查询封闭的StatusPanel并在其getCellEditorValue()实现中返回一个合适的值。

是的,这是可能的,根据您的应用程序设计,如何做到这一点的方法很少。 由于我不知道细节,我建议这个简单的解决方案:

 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 } } });