java在table中添加一个动态按钮
时间: 2024-03-11 21:49:08 浏览: 91
要在Java中添加一个动态按钮,首先你需要使用`JButton`类创建一个按钮对象,然后将其添加到`JTable`的每一行中。以下是一个简单的示例代码来实现这个功能:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DynamicButtonInTable extends JFrame {
private JTable table;
private DefaultTableModel model;
public DynamicButtonInTable() {
setTitle("Dynamic Button in Table");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 400);
// create table model with column names
model = new DefaultTableModel(new Object[]{"Name", "Age", "Button"}, 0);
// create table with the above model
table = new JTable(model);
// create a button column renderer and editor
TableColumn buttonColumn = table.getColumnModel().getColumn(2);
buttonColumn.setCellRenderer(new ButtonRenderer());
buttonColumn.setCellEditor(new ButtonEditor(new JCheckBox()));
// add some data to the model
model.addRow(new Object[]{"John", 25, "Click Me"});
model.addRow(new Object[]{"Mary", 30, "Click Me"});
model.addRow(new Object[]{"Bob", 35, "Click Me"});
// add the table to a scroll pane and to the frame
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
setVisible(true);
}
public static void main(String[] args) {
new DynamicButtonInTable();
}
// button column renderer
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ? "" : value.toString());
return this;
}
}
// button column editor
class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(UIManager.getColor("Button.background"));
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
return button;
}
public Object getCellEditorValue() {
if (isPushed) {
// perform button click action here
JOptionPane.showMessageDialog(button, label + " button clicked");
}
isPushed = false;
return label;
}
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
}
```
这个例子中,我们创建了一个`JTable`并添加了一个动态按钮列。我们使用`DefaultTableModel`来存储数据和按钮的文本。我们还创建了一个`ButtonRenderer`类来渲染按钮列,并且使用了`ButtonEditor`类来编辑按钮列。在`ButtonEditor`类中,我们添加了一个`ActionListener`来处理按钮点击事件。
当你运行这个程序时,你将看到一个表格,每一行都有一个按钮列,你可以点击按钮并处理相应的事件。
阅读全文