是否可以检测在表格单元格内单击的按钮?
我是一个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; }
我的问题是我能否检测到包含在面板内的按钮的点击? 我问的是技术上是否可行?
谢谢
在我的手机中,我有两个按钮和三个标签; 他们都在一个小组中。
使用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 } } });
- Java垃圾收集器是否会回收包含对其他对象的引用的非引用对象
- Java Calendar Object减去一个月
- NetBeans不再为以前的代码生成JAR文件
- 使用Java或Groovy为SQLite编写用户定义的SQL函数?
- ArrayList – 添加“相同”对象(same => equals,hashCode),Threads
- Maven / Jenkins java.lang.UnsupportedClassVersionError:不支持的major.minor版本51.0
- ColdFusion 10中的BouncyCastle库
- 即使测试失败,Gradle也会在测试阶段后执行任务
- 加密加密属性文件中的密码