Question

i'm developing an app that has a jtable and user could start a download. i want to add in the table the name of download, the state and progress bar. I'm doing like this:

INIT JTABLE: ( CODE CREATED AUTOMATICALLY BY NETBEANS ( is there a way to edit it? o.O ))

jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Categoria", "Sottocategoria", "Progresso (%)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jScrollPane2.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

CREATE RENDERER:

class ProgRenderer implements TableCellRenderer {

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

ADD IT TO THE LAST COLOUM:

jTable1.getColumn("Progresso (%)").setCellRenderer(new ProgRenderer());

Now i've created an ArrayList<Thread> to save the active threads, and a ArrayList<JProgressBar> to save all the progressbar existing in jtable ( right way? ).

Now to add new row i make like this:

DefaultTableModel d = (DefaultTableModel) jTable1.getModel();
work++; // to have a number of active threads.
progress.add(getProgress(x, total));
d.addRow(new Object[]{Html.categoria, "Inizio scansione", progress.get(work)});
                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        int max = Html.totale;
                        for(int i=1;i<=max;i++){
                            try {
                                progress.get(work).setValue(i); // JUST FOR TRY
                                System.out.println(progress.get(work).getValue()); // TRY
                                Thread.sleep(1000);
                            } catch (InterruptedException ex) {
                        }
                    }
                });
                working.add(t);
                t.start();


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

My problem is that progress isn't updated, my approach is wrong? can you help me? thanksss

Was it helpful?

Solution

I recommend you:

  1. In table keep information about progress only.

    Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class, java.lang.Integer.class
    };
    
  2. Implement progress bar to extend JProgressBar

    class ProgRenderer extends JProgressBar implements 
    
    TableCellRenderer {
       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
          this.setValue((Integer)value)
          return this;
       }
    }
    
  3. Increment values

    @Override
    public void run() {
        int max = Html.totale;
        for(int i=1;i<=max;i++){
            try {
                SwingUtiliites.invokeLater(new Runnable(){
                   public void run(){
                       jTable1.setValueAt(Integer.valueOf(i),row,2);
                   }
                }
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
        }
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top