[英]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;
}
我的問題是我是否可以檢測到面板內包含的按鈕是否被點擊? 我在問技術上是否可行?
謝謝
在我的單元格中,我有兩個按鈕和三個標簽。 它們全部集中在一個面板中。
您正確使用TableCellRenderer
和TableCellEditor
。 在這個完整的示例中 , 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.