Forum Stats

  • 3,759,518 Users
  • 2,251,557 Discussions
  • 7,870,690 Comments

Discussions

[Help Needed] [Learning Java] [JProgressBar > JtableCell]

KatAlive
KatAlive Member Posts: 3 Green Ribbon

Hi all,

I need your help, I'm trying to put a JProgressBar into JTable cell, because one Column need this object.

I created class and Override it :

class ProgressCellRenderer extends JProgressBar implements TableCellRenderer{
   
  //Creates a JProgressBar with the range 0-100
  public ProgressCellRenderer(){
    super(0, 100);
    setValue(0);
    setStringPainted(true);
  }
@Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        
        //value is a percentage example 95%
        String sValue = null;
        int index = 0;
        if(value != null){
            sValue = value.toString();
            index = sValue.indexOf('%');
            int perc = 0;
            
            try{
                perc = Integer.parseInt(sValue.substring(0, index));
            
            }catch(NumberFormatException e){
                //Something
            }
            setValue(perc);
            setString(sValue);
        }
        return this;
    }
}

Then, in my Method I wrote this (in order to put the bar into correct column [3])

Main_Frame.max_table.getColumnModel().getColumn(3)
        .setCellRenderer(new ProgressCellRenderer());

the problem is that on this way all rows of the table in column 3 get the value of the last record found, in my case, i populated only "row 0" and the value at row 0, column 3 is 70%, so, all other rows become 70% with Progress Bar at 70% painted. [Screenshots attached]

Screenshot Without Progress Bar Method Active








Screenshot With Progress Bar Method Active








How can I correct this? I want that Bar in Column 3 get real value in the cell and paint proper Bar based on that value. if some cells are empty, no bar must be painted inside them.

Can someone help me to find my error?


Regards.