如何在Jtable中加入按钮
时间: 2024-02-24 18:56:34 浏览: 86
在JTable中添加按钮,需要使用自定义的单元格渲染器和单元格编辑器。具体步骤如下:
1. 创建一个自定义的单元格渲染器
在此单元格渲染器中,我们可以将JButton组件添加到表格中的单元格中。
```java
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public 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) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText((value == null) ? "" : value.toString());
return this;
}
}
```
2. 创建一个自定义的单元格编辑器
在此单元格编辑器中,我们可以定义按钮的行为。
```java
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public 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(table.getBackground());
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = true;
return button;
}
public Object getCellEditorValue() {
if (isPushed) {
JOptionPane.showMessageDialog(button, label + ": Ouch!");
}
isPushed = false;
return new String(label);
}
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
```
3. 将自定义的单元格渲染器和单元格编辑器应用到表格中
```java
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JButtonInJTableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JButton in JTable Example");
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Name");
model.addColumn("Button");
JTable table = new JTable(model);
table.getColumn("Button").setCellRenderer(new ButtonRenderer());
table.getColumn("Button").setCellEditor(new ButtonEditor(new JCheckBox()));
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(scrollPane);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
在这个例子中,我们首先创建了一个DefaultTableModel作为数据模型,添加了两列“Name”和“Button”,并填充了一行数据。然后使用JTable的getColumn方法获取“Button”列,分别将自定义的单元格渲染器和单元格编辑器应用到该列中。最后将表格添加到JScrollPane中,并将ScrollPane添加到JPanel中,再将JPanel添加到JFrame中显示出来。
运行程序,你会看到一个JTable中带有按钮的例子。
阅读全文