Question

Possible Duplicate:
Adding multiple JProgressBar to TableColumn of JTable

i have a jTable with a DefaultTableModel with this coloumn:

String String JProgressBar

and every row is created like this:

progress.add(getProgress(x, total));
d.addRow(new Object[]{category, "Initializing..", progress.get(work)});

Where getProgress is:

private JProgressBar getProgress(int x, int total) {
        JProgressBar progressCsv = new JProgressBar();
        progressCsv.setMaximum(totale);
        progressCsv.setStringPainted(true);
        progressCsv.setString("0%");
        return progressCsv;
}

And progress:

progress = new ArrayList<JProgressBar>();

My class implements TableCellRenderer so

@Override
    public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
        int v = Integer.parseInt(o.toString());
        JProgressBar b = (JProgressBar) jtable.getModel().getValueAt(i, i1);
        b.setValue(v);
        return null;
    }

Where i and i1 are 0 - 2. so the first row and third coloum ( JProgressBar ).

On: JProgressBar b = (JProgressBar) jtable.getModel().getValueAt(i, i1); i get

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to javax.swing.JProgressBar

Where is the problem? can you help me?

Was it helpful?

Solution 2

if you use ArrayList you could :

public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
        int v = Integer.parseInt(o.toString());
        JProgressBar b = progress.get(i);
        b.setValue(v);
        return b;
    }

OTHER TIPS

JProgressBar b = (JProgressBar) jtable.getModel().getValueAt(i, i1);

This is the line causing this error. You want to cast an integer to a progress bar, wich is of course impossible. This code makes no sence in a lot of ways. First off, lose the getProgress(int x, int total) method. If you want to have a progress bar your jtable, you did right by using a custom cell renderer, but you dont need to actually add a progressbar to the table. Instead, you use an integer. Then, in your cell renderer, you use that integer to display a progressbar. Your renderer would look more like this:

class ProgressBarRenderer extends JProgressBar implements TableCellRenderer {

        public ProgressBarRenderer() {
            setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            setOpaque(true);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            int progress = (Integer) value;
            setValue(progress);
            return this;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top