0

Right now I have this code

jTable1.getColumn("Progress").setCellRenderer(new TableCellRenderer() {

    @Override
    public Component getTableCellRendererComponent(JTable arg0, Object arg1,
    boolean arg2, boolean arg3, int arg4, int arg5) {
        // TODO Auto-generated method stub
        return new JProgressBar();
    }
});

and I want to update it's progress as it keeps going, but of course this code doesn't work.

So I change it to this.

jTable1.getColumn("Progress").setCellRenderer(new TableCellRenderer() {

    @Override
    public Component getTableCellRendererComponent(JTable arg0, Object arg1,
    boolean arg2, boolean arg3, int arg4, int arg5) {
        // TODO Auto-generated method stub
        JProgressBar bar2 = bar; //where bar is another JProgressBar being updated outside.
        return bar2;
    }
});

This code works, but the problem is, when there is multiple rows, their columns all update to the same value, which is not what I want.

I want to be able to update the indiviual row's JProgressBar, but how do I do that?

4

1 回答 1

1

我做了类似的事情(JSpinner 而不是 JProgressBar),我已经这样做了:

public class SpinnerRenderer implements TableCellRenderer {
    private JSpinner spinner = new JSpinner();

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        checkNotNull(model, "The SpinnerModel from the table model is null");

        if (!(value instanceof SpinnerModel)) {
            System.err.println("Error: The value" + " in cell: row=" + row
                    + " col=" + column + " must be instance"
                    + " of SpinnerModel");
        }else{
            SpinnerModel model = (SpinnerModel)value;
            spinner.setModel(model);
        }

        return spinner;
    }

}

您应该注意,我从表模型中获取数据,其中每个微调器都有单独的 SpinnerModel。

于 2013-08-20T16:26:02.580 回答