Java 是否可以在表格单元格中检测单击的按钮?
我有一个swing应用程序,其中有一个表,我放了一个面板,可以包含一个按钮Java 是否可以在表格单元格中检测单击的按钮?,java,swing,jtable,jbutton,tablecelleditor,Java,Swing,Jtable,Jbutton,Tablecelleditor,我有一个swing应用程序,其中有一个表,我放了一个面板,可以包含一个按钮 public class MyCellDataRenderer implements TableCellRenderer, TableCellEditor { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
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;
}
我的问题是,我是否可以检测到在面板内部包含的按钮上单击?
我在问技术上是否可行
谢谢是的,这是可能的,并且基于您的应用程序设计,有几种方法可以做到这一点。由于我不知道细节,我建议采用以下简单的解决方案:
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
}
}
});
在我的手机里,我有两个按钮和三个标签;它们都在一个面板中
你正确地使用了a。在本完整教程中,
StatusEditor
查询封闭的StatusPanel
,并在其getCellEditorValue()
的实现中返回一个合适的值。不,您不理解我。在我的单元格中,我有两个按钮和三个标签(它们都在一个面板中).我怎样才能从所有这些组件中检测出选择了哪个组件?似乎您正在寻找类似的东西。@alex2410:引用,我最初也想到了这一点。不客气;归功于@mKorbel的例子;请考虑更新您的问题,以反映专家组的要求。