【问题标题】:Is it possible to detect a button clicked inside a table cell?是否可以检测到在表格单元格内单击的按钮?
【发布时间】:2014-08-13 14:29:43
【问题描述】:

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

 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;

}

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

谢谢

【问题讨论】:

  • 看来你正在寻找类似next的东西。
  • @alex2410: 引用here,我本来也是这么想的。

标签: java swing jtable jbutton tablecelleditor


【解决方案1】:

在我的单元格中,我有两个按钮和三个标签;它们都在一个面板中。

您使用TableCellRenderer and TableCellEditor 是正确的。在这个完整的example 中,StatusEditor 查询封闭的StatusPanel 并在getCellEditorValue() 的实现中返回一个合适的值。

【讨论】:

  • 不客气;归功于@mKorbel 的例子;请考虑更新您的问题以反映小组要求。
【解决方案2】:

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

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

【讨论】:

  • 不,你不明白我的意思。在我的单元格中,我有两个按钮和 3 个标签(它们都在一个面板中)。我如何检测从所有这些组件中选择了哪个组件?跨度>
猜你喜欢
  • 2014-01-14
  • 2021-02-24
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2021-12-13
  • 2015-12-11
相关资源
最近更新 更多