繁体   English   中英

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM