-1

the value of the jprogressbar must be updated once i change the value of progress in the database but i didnot understand the class ProgressRenderer very much and i couldn't update the jprogressbar

class ProgressRenderer extends JProgressBar implements TableCellRenderer { 

     public ProgressRenderer(){
    super(0, 100);
    setValue(0);
    setString("0%");
    setStringPainted(true);
  }

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

    //value is a percentage e.g. 95%
    final String sValue = value.toString();
    int index = sValue.indexOf('%');
    if (index != -1) {
      int p = 0;
      try{
        p = Integer.parseInt(sValue.substring(0, index));
      }
      catch(NumberFormatException e){
      }
      setValue(p);
      setString(sValue);
    }
    return this;
  }

} 
table.getColumnModel().getColumn(4).setCellRenderer(new ProgressRenderer());
  • You're re-asking the exact same question (not allowed) but *without* needed improvements, without a valid [mre]. Please delete the duplicate and improve the original, including giving code that we can compile, run and test for ourselves. Again, please read the link to see why this is a critical step when asking debugging questions. – Hovercraft Full Of Eels May 25 '19 at 21:26

1 Answers1

-1

Check out the following code and integrate it to your getTableCellRendererComponent() function.

final DefaultBoundedRangeModel model = new DefaultBoundedRangeModel();
frame.add(new JProgressBar(model));
Thread t = new Thread(new Runnable() {
    public void run() {
        int i = 1;
        model.setMinimum(0);
        model.setMaximum(100);
        try {
            while (i <= 100 || true) {
                model.setValue(i);
                i++;
                Thread.sleep(50);
            }
        } catch (InterruptedException ex) {
            model.setValue(model.getMaximum());
        }
    }
});

To learn more, check the link: Update JProgressBar

Shariq Ali
  • 31
  • 1
  • 9