Click here to Skip to main content
15,617,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am writing a backup program and l want the progress to display in the cell. l have put the progress bar but it doesn't animate the new value when l use table object to set new value. Please l need an urgent help.

What I have tried:

The initializing code below. Am using DefaultTableModel
TableColumn tableCol;
<pre lang="java">tableCol = jTable1.getColumnModel().getColumn(4);
        tableCol.setCellRenderer(new renderProgress());


below is a class that extends JProgressBar and implements Tablecellrenderer
Java
import java.awt.Component;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author slab1
 */
public class renderProgress extends JProgressBar implements TableCellRenderer{

    public renderProgress(){
		super(0,100);
		setValue(0);
		setString("0%");
		setStringPainted(true);
                
	 }
    @Override
    public boolean isDisplayable(){
        return true;
    }
    
    @Override
    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;
    }
    
}


then the snipet that sets new value below (which is the bytes written)
Java
<pre>int count=0;
		byte[] bytes = new byte[1024];
		int length;
                int progress=0;
		while ((length = fis.read(bytes)) >= 0) {
//                        jTable1.getModel().setValueAt(length +"%".toString(), WIDTH, 4);
			zos.write(bytes, 0, length);
                        count+=length;
//                        for(int i =0; i<100; i++){
//                            jTable1.setValueAt(i, 0, 4);//Object of the table model to set new value to the table column
//                        }
                        if(fileSize>0L){
                            progress = (int) ((count * 100) / fileSize);
                            defTable.setValueAt(progress, 0, 4);
                        }
		}
Thanks
Posted
Updated 21-Jul-17 3:58am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900