JTable是Java Swing库中的一个组件,用于显示和编辑表格数据。在JTable的一行中单击按钮可以更改所有表记录中的标签,可以通过以下步骤实现:
以下是一个示例代码:
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JTableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTable Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建自定义的TableModel
MyTableModel model = new MyTableModel();
// 创建JTable并设置数据模型
JTable table = new JTable(model);
// 添加按钮列
TableColumn buttonColumn = new TableColumn(model.getColumnCount());
buttonColumn.setHeaderValue("Change Label");
buttonColumn.setCellRenderer(new ButtonRenderer());
buttonColumn.setCellEditor(new ButtonEditor(new JCheckBox()));
table.addColumn(buttonColumn);
// 添加JTable到窗口
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setVisible(true);
}
// 自定义TableModel
static class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Label"};
private Object[][] data = {
{"Label 1"},
{"Label 2"},
{"Label 3"}
};
@Override
public int getRowCount() {
return data.length;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
@Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
@Override
public boolean isCellEditable(int row, int col) {
return true;
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
}
// 自定义按钮渲染器
static class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText("Change Label");
return this;
}
}
// 自定义按钮编辑器
static class ButtonEditor extends DefaultCellEditor {
private JButton button;
private String label;
private boolean isPushed;
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
@Override
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;
}
@Override
public Object getCellEditorValue() {
if (isPushed) {
// 更改所有表记录中的标签
MyTableModel model = (MyTableModel) table.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
model.setValueAt("New Label", i, 0);
}
}
isPushed = false;
return label;
}
@Override
public boolean stopCellEditing() {
isPushed = false;
return super.stopCellEditing();
}
@Override
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
}
在这个示例中,我们创建了一个包含一个按钮列的JTable。当点击按钮时,会触发按钮的ActionListener,然后遍历TableModel中的所有行,将标签的值更改为"New Label"。你可以根据实际需求修改按钮的文本、按钮点击事件的逻辑等。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云