表格进度条渲染器

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class ProgressBarRenderer extends DefaultTableCellRenderer {

	private static final long serialVersionUID = 1L;

	private final JPanel pane;
	private final JProgressBar progressBar;

	public ProgressBarRenderer() {
		super();
		pane = new JPanel(new BorderLayout());

		progressBar = new JProgressBar(0, 100);
		progressBar.setStringPainted(true);
		
		pane.add(progressBar, BorderLayout.CENTER);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		Component component = super.getTableCellRendererComponent(table, value,
				isSelected, hasFocus, row, column);

		pane.setBackground(component.getBackground());
		pane.setBorder(((JLabel)component).getBorder());
		
		int n = 0;
		if (value != null) {
			try {
				n = Integer.parseInt(value.toString());
			} catch (NumberFormatException ex) {
				ex.printStackTrace();
			}
		}
		progressBar.setValue(n);
		
		return pane;
	}
}

你可能感兴趣的:(java,swing)