-3

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?

Community
  • 1
  • 1
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • -1 why do you keep asking the same question over and over again, if you intend on sticking to your errors anyway? One last time: DO. NOT. ADD. COMPONENTS. TO. YOUR. MODEL! – kleopatra Mar 07 '12 at 10:29

2 Answers2

4
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;
        }
    }
Maarten Blokker
  • 604
  • 1
  • 5
  • 23
  • so you are telling me that i don't have to add manually a progress bar but setting new ProgressBarRenderer to my coloumn of jtable, progressbar is setting automatically on method getTableCellRendererComponent? and if i need to add other coloum of the same row? i add row in this way d.addRow(new Object[]{Html.categoria, "Inizio scansione", new JProgressBar()}); – Jayyrus Mar 06 '12 at 22:11
  • Yes i am, but how is: "d.addRow(new Object[]{Html.categoria, "Inizio scansione", new JProgressBar()});" not manually adding a progressbar? like i said in my post, you use an integer like: d.addRow(new Object[]{Html.categoria, "Inizio scansione", 0}); the renderer takes care of the rest, setting the renderers progress bars value to 0 or any other value for that matter. – Maarten Blokker Mar 07 '12 at 00:41
-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;
    }
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • -1 - a) that's not how renderers are meant to be implemented (accessing some outer list to return a new rendering component each time), instead use one renderer component and configure it with the data as given by the param b) the lookup will break if the table is sorted or filtered – kleopatra Mar 07 '12 at 10:35
  • this is the unique method that works.. nobody tell me why it takes 0 otherwise my progressbar – Jayyrus Mar 07 '12 at 22:43