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?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
user2519193
  • 211
  • 2
  • 14

1 Answers1

1

I've done something similar(JSpinner instead of JProgressBar) and I've done it like this:

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;
    }

}

You should note that I fetch the data from the table model, where I have separate SpinnerModel for each spinner.

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
  • +1, it's the trick ! @user2519193 For information you should use a [`BoundedRangeModel`](http://docs.oracle.com/javase/7/docs/api/javax/swing/BoundedRangeModel.html) instead of `SpinnerModel` with `JProgressBar` – NiziL Aug 20 '13 at 16:29
  • But where do I put this class in? Just use this class in setCellRenderer? – user2519193 Aug 20 '13 at 17:34
  • You use it like that: table.setCellRenderer(new MyCellRenderer()); – Svetlin Zarev Aug 20 '13 at 18:34