在Java Table中显示进度列,可以通过以下步骤实现:
以下是一个示例代码,演示如何在Java Table中显示进度列:
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class ProgressTableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Progress Table Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create custom TableModel
ProgressTableModel model = new ProgressTableModel();
JTable table = new JTable(model);
// Add progress column to the table
TableColumn progressColumn = new TableColumn(model.getColumnCount());
progressColumn.setHeaderValue("Progress");
table.addColumn(progressColumn);
// Set custom renderer for progress column
progressColumn.setCellRenderer(new ProgressRenderer());
// Set custom editor for progress column
progressColumn.setCellEditor(new ProgressEditor());
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setVisible(true);
}
static class ProgressTableModel extends AbstractTableModel {
private List<String> tasks = new ArrayList<>();
private List<Integer> progress = new ArrayList<>();
public ProgressTableModel() {
tasks.add("Task 1");
tasks.add("Task 2");
tasks.add("Task 3");
progress.add(50);
progress.add(75);
progress.add(25);
}
@Override
public int getRowCount() {
return tasks.size();
}
@Override
public int getColumnCount() {
return 2; // Including progress column
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return tasks.get(rowIndex);
} else if (columnIndex == 1) {
return progress.get(rowIndex);
}
return null;
}
@Override
public String getColumnName(int column) {
if (column == 0) {
return "Task";
} else if (column == 1) {
return "Progress";
}
return super.getColumnName(column);
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 1) {
return Integer.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1; // Only progress column is editable
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 1) {
progress.set(rowIndex, (Integer) aValue);
fireTableCellUpdated(rowIndex, columnIndex);
}
}
}
static class ProgressRenderer extends DefaultTableCellRenderer {
private JProgressBar progressBar = new JProgressBar();
public ProgressRenderer() {
progressBar.setStringPainted(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Integer) {
int progressValue = (Integer) value;
progressBar.setValue(progressValue);
progressBar.setString(progressValue + "%");
}
return progressBar;
}
}
static class ProgressEditor extends DefaultCellEditor {
private JProgressBar progressBar = new JProgressBar();
public ProgressEditor() {
super(new JTextField());
progressBar.setStringPainted(true);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value instanceof Integer) {
int progressValue = (Integer) value;
progressBar.setValue(progressValue);
progressBar.setString(progressValue + "%");
}
return progressBar;
}
}
}
这个示例代码创建了一个包含进度列的表格,进度列显示了任务的进度百分比。你可以根据自己的需求修改TableModel中的数据和进度值,以及自定义渲染器和编辑器的样式。
领取专属 10元无门槛券
手把手带您无忧上云