Pessoal,
Preciso da ajuda de vocês para fazer funcionar esta classe que usarei quando precisar de um monitor de progresso para outras classes.
O problema é que a barra de progressão só surge no final.
Caso tenham um exemplo melhor de classe genérica com ProgressMonitor para sugerir…
[code]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package com.tfv.principal;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class MonitorProgressao extends JFrame implements ActionListener {
static ProgressMonitor pbar;
static int counter = 0;
public static int getCounter() {
return counter;
}
public static void setCounter(int counter) {
MonitorProgressao.counter = counter;
}
public MonitorProgressao(String titulo,int minimo, int maximo) {
super(titulo);
setSize(250, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pbar = new ProgressMonitor(null, titulo, "Inicializando", minimo, maximo);
Timer timer = new Timer(100, this);
timer.start();
}
public static void main(String args[]) {
}
@Override
public void actionPerformed(ActionEvent e) {
// Invoked by the timer every half second. Simply place
// the progress monitor update on the event queue.
SwingUtilities.invokeLater(new Update());
}
class Update implements Runnable {
@Override
public void run() {
if (pbar.isCanceled()) {
pbar.close();
System.exit(1);
}
pbar.setProgress(counter);
pbar.setNote("Operation is " + counter + "% complete");
}
}
}
[/code]
Um exemplo de método de uma classe que instanciará esta genérica de ProgressMonitor:
public class SincronizarDados extends javax.swing.JDialog {
private String mensagem;
private Session sessao;
private int qtdRegistros;
public SincronizarDados(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
this.atualizaTabelaClientes();
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SincronizarDados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SincronizarDados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SincronizarDados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SincronizarDados.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SincronizarDados dialog = new SincronizarDados(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
....
public void atualizaTabelaClientes() {
try {
ARRAYOFSTRUCTCLI result = listaclientes();
sessao = HibernateUtil.getSessionFactory().openSession();
sessao.beginTransaction().begin();
this.qtdRegistros = result.getSTRUCTCLI().size();
System.out.println(this.qtdRegistros);
MonitorProgressao mp = new MonitorProgressao("Sincronizando...", 0, this.qtdRegistros);
for (int i = 0; i < this.qtdRegistros; i++) {
System.out.println(i);
MonitorProgressao.setCounter(i);
Cliente cliente = new Cliente();
cliente.setCodigo(result.getSTRUCTCLI().get(i).getCCOD());
sessao.saveOrUpdate(cliente);
}
sessao.getTransaction().commit();
sessao.close();
} catch (Exception ex) {
System.out.println(Acesso.getInstance().getCodigo() + " - " + new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date()) + " - erro ao tentar listar clientes na pesquisa. Info:" + ex.toString());
}
System.out.println("Finalizada sincronização!");
}
}