jTable 中的 jButton 不可点击

3 java swing jtable jbutton

我遇到了问题,我无法单击按钮。它们的行为就像只是带有按钮设计的文本字段。

\n\n

我的Main

\n\n
    tableModStudents = (DefaultTableModel) studentsTable.getModel();\n    studentsTable.getColumn(studentsTable.getColumnName(8))\n                 .setCellRenderer(new JButtonRenderer());\n    studentsTable.getColumn(studentsTable.getColumnName(8))\n                 .setCellEditor(new JButtonEditor());\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的CellRenderer

\n\n
public class JButtonRenderer implements TableCellRenderer {    \n    private JButton button = new JButton();\n\n    public Component getTableCellRendererComponent(JTable table,\n            Object buttonText, boolean isSelected, boolean hasFocus, \n            int row, int column) {\n        table.setShowGrid(true);\n        button.setText("Details");\n        button.setToolTipText(buttonText.toString());\n        return button;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的CellEditor

\n\n
    public class JButtonEditor extends AbstractCellEditor implements TableCellEditor {\n\n    private JButton button;\n    private String txt;\n\n    public JButtonEditor() {\n        super();\n        button = new JButton();\n        button.setOpaque(true);\n        button.addActionListener(new ActionListener() {\n            public void actionPerformed(ActionEvent ae) {\n                System.out.println("Button gedr\xc3\xbcckt!");\n            }\n        });\n    }\n\n    public Object getCellEditorValue() {\n        return null;\n    }\n\n    public boolean isCellEditable(EventObject anEvent) {\n        return true;\n    }\n\n    public boolean shouldSelectCell(EventObject anEvent) {\n        return false;\n    }\n\n    public boolean stopCellEditing() {\n        return super.stopCellEditing();\n    }\n\n    public void cancelCellEditing() {\n    }\n\n    public void addCellEditorListener(CellEditorListener l) {\n    }\n\n    public void removeCellEditorListener(CellEditorListener l) {\n    }\n\n    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {\n        txt = (value == null) ? "" : value.toString();\n        button.setText(txt);\n        return button;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

你能找出其中的问题吗?它让我发疯...

\n\n

非常感谢 :)

\n

cam*_*ckr 5

查看表按钮列

它将按钮渲染器和编辑器组合在一个类中。

您所需要做的就是提供Action调用按钮时要调用的自定义(通过单击它或调用其助记符)。