Buscar
Social
Ofertas laborales ES

Foro sobre Java SE > ProgressBar no avanza

Buenas, tengo una barra de proceso que quiero que avance según voy leyendo archivos de un ftp pero no soy capaz de hacerlo ya que leo los archivos y hasta que no termina no se actualiza la progressbar. El código es el siguiente:

public class jdFTP extends javax.swing.JDialog {
private boolean sendMode;
private String[][] file;

private FTPClient client;
private final ConfigProperties prop;

/**
* Creates new form jdFTP
* @param parent
* @param modal
* @param prop
*/
public jdFTP(java.awt.Frame parent, boolean modal, ConfigProperties prop) {
super(parent, modal);
initComponents();
this.prop=prop;
setLocationRelativeTo(null);
System.setProperty("java.net.preferIPv4Stack" , "true");
}

/**
* Establece el modo de descarga de ficheros del ftp.
*/
public void download(){ sendMode=false; }

/**
* Establece el modo de envío de ficheros al ftp.
* @param file
*/
public void send(String[][] file){ sendMode=true; this.file=file; }


private boolean enviar(String fileName, String path, boolean b){
boolean envio=true;
try {
conectar();
if(b) changePath(prop.getFTP_path()+path);
else changePath(prop.getFTP_path()+"new");

//Subiendo el fichero al FTP en la carpeta asignada.
FileInputStream fis = new FileInputStream(prop.getXML_pathSend()+fileName);
client.storeFile(fileName, fis);

//Desconexión del FTP.
fis.close();
desconectar();

if(b){
jProgressBar.setValue(1);
enviar(fileName, path, false);
}else{
jProgressBar.setValue(2);
File fichero=new File(prop.getXML_pathSend()+fileName);
fichero.delete();
fichero.deleteOnExit();
}//Una vez subido al FTP borramos el fichero del pc.
} catch (IOException ex) {
envio=false;
Object[] options = {"Aceptar"};
JOptionPane.showOptionDialog(null,"Envio fallido: \n"+ex,"Estado de envío",
JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE,
null, options, options[0]);
}
return envio;
}

private void descargar(){
try {
conectar();
changePath(prop.getFTP_path()+"new");

ArrayList lista=listar();
jProgressBar.setMaximum(lista.size());
for (int i=0;i<lista.size();i++) {
FileOutputStream fis = new FileOutputStream(prop.getXML_pathDownload()+lista.get(i));
client.retrieveFile(lista.get(i).toString(),fis);
fis.close();
borrar(lista.get(i).toString());
jProgressBar.setValue(i+1);
}
desconectar();

String cad;
if(jProgressBar.getMaximum()>0) cad="Ficheros recogidos correctamente."; else cad="Nada que descargar.";

Object[] options = {"Aceptar"};
JOptionPane.showOptionDialog(null,cad,"Descarga de ficheros",
JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE,null, options, options[0]);
dispose();
} catch (IOException ex) { }
}



private void conectar(){
try {
client = new FTPClient();

//Conexión al FTP.
client.connect(prop.getFTP_url());
client.login(prop.getFTP_user(),prop.getFTP_password());
client.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (IOException ex) { }
}
private ArrayList listar(){
ArrayList lista= new ArrayList();
try { for (FTPFile ftpFile : client.listFiles()) { lista.add(ftpFile.getName()); }
} catch (IOException ex) { }
return lista;
}
private void changePath(String path){try { client.changeWorkingDirectory(path); } catch (IOException ex) { } }
private void borrar(String fileName){ try { client.deleteFile(fileName); } catch (IOException ex) { } }
private void desconectar(){ try { client.logout(); client.disconnect(); } catch (IOException ex) { } }

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jProgressBar = new javax.swing.JProgressBar();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
formFocusGained(evt);
}
});
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jProgressBar.setForeground(new java.awt.Color(51, 255, 0));
jProgressBar.setMaximum(2);
jProgressBar.setStringPainted(true);
getContentPane().add(jProgressBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 320, 40));

pack();
}// </editor-fold>

private void formFocusGained(java.awt.event.FocusEvent evt) {
if(sendMode){
boolean envio=true;
for (String[] fileData : file) {
if(envio)envio=enviar(fileData[0], fileData[1], true);
else enviar(fileData[0], fileData[1], true);
}
if(envio){
Object[] options = {"Aceptar"};
JOptionPane.showOptionDialog(null,"Ficheros enviados correctamente.","Estado de envío",
JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE,null, options, options[0]);
}
}else descargar();
dispose();
}

noviembre 26, 2014 | Registered Commentercarlota89

La zona en la que intento hacer que el progresssbar aumente es en la función enviar y en la función descargar

noviembre 26, 2014 | Registered Commentercarlota89

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

noviembre 26, 2014 | Registered Commenterchoces

Los dialogos "modales", una vez que se hacen visibles, no pueden actualizar su interfaz grafica desde el mismo hilo que son lanzados. Usa un thread aparte, o como te sugiere choces, un worker, para manejar la barra de progreso.

noviembre 26, 2014 | Registered Commenteralaguslaz