在 Java 中,如何向 JTable 单元格添加/插入 JButton?
JTable 是 JComponent 类的子类,它可用于创建表格,其中信息显示在多个行和列中。从 JTable 中选择值时,将生成一个 TableModelEvent,可以通过实现 TableModelListener 接口来处理该事件。我们可以通过定制 **DefaultTableModel** 或 **AbstractTableModel** 中的代码向 JTable 单元格添加或插入 JButton,还可以通过实现 TableCellRenderer 接口并覆盖 getTableCellRendererComponent() 方法来定制代码。
示例
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableButtonTest extends JFrame { private JTable table; private JScrollPane scrollPane; public JTableButtonTest() { setTitle("JTableButton Test"); TableCellRenderer tableRenderer; table = new JTable(new JTableButtonModel()); tableRenderer = table.getDefaultRenderer(JButton.class); table.setDefaultRenderer(JButton.class, new JTableButtonRenderer(tableRenderer)); scrollPane = new JScrollPane(table); add(scrollPane, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setSize(400, 300); setVisible(true); } public static void main(String[] args) { new JTableButtonTest(); } } class JTableButtonRenderer implements TableCellRenderer { private TableCellRenderer defaultRenderer; public JTableButtonRenderer(TableCellRenderer renderer) { defaultRenderer = renderer; } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if(value instanceof Component) return (Component)value; return defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } class JTableButtonModel extends AbstractTableModel { private Object[][] rows = {{"Button1", new JButton("Button1")},{"Button2", new JButton("Button2")},{"Button3", new JButton("Button3")}, {"Button4", new JButton("Button4")}}; private String[] columns = {"Count", "Buttons"}; public String getColumnName(int column) { return columns[column]; } public int getRowCount() { return rows.length; } public int getColumnCount() { return columns.length; } public Object getValueAt(int row, int column) { return rows[row][column]; } public boolean isCellEditable(int row, int column) { return false; } public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }
输出
广告