Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di xrwnis
    Registrato dal
    Apr 2008
    Messaggi
    42

    [JAV] JTextField setText

    Ciao a tutti. Nella mia applicazione java devo inviare un file ad un server ftp. Durante il trasferimento vorrei mostrare in una JTextField le operazioni effettuate, ad esempio connessione al server, trasferimento file, chiusura della connessione ecc ecc. Ho una classe in cui è presente un Jpanel con la JTexField tLoading;da questa classe chiamo il metodo statico upload di un'altra classe chiamata FileUpload passando come parametri oltre a quelli necessari per trasferire il file anche la textField tLoading. Questo è il metodo upload
    codice:
    public static void upload( String ftpServer, String user, String password,String fileName, File source,JTextField tLoading ) throws MalformedURLException,IOException{
    
            if (ftpServer != null && fileName != null && source != null){
                tLoading.setText("Connessione al server");
                StringBuffer sb = new StringBuffer( "ftp://" );
                // check for authentication else assume its anonymous access.
                if (user != null && password != null){
                    sb.append( user );
                    sb.append( ':' );
                    sb.append( password );
                    sb.append( '@' );
                }
                sb.append( ftpServer );
                sb.append( '/' );
                sb.append( fileName );
                /*
                * type ==> a=ASCII mode, i=image (binary) mode, d= file directory
                * listing
                */
                sb.append( ";type=i" );
    
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try{
                    tLoading.setText("Trasferimento file "+source.getName());
                    System.out.println("Trasferimento file "+source.getName());
                    URL url = new URL( sb.toString() );
                    URLConnection urlc = url.openConnection();
    
                    bos = new BufferedOutputStream( urlc.getOutputStream() );
                    bis = new BufferedInputStream( new FileInputStream( source ) );
    
                    int i;
                    // read byte by byte until end of stream
                    while ((i = bis.read()) != -1){
                        bos.write( i );
                    }
                }
                finally{
                    tLoading.setText("Chiusura connessione al server");
                    System.out.println("Chiusura connessione al server");
                    if (bis != null)
                        try{
                            bis.close();
                        }
                        catch (IOException ioe){
                            ioe.printStackTrace();
                        }
                    if (bos != null)
                        try{
                            bos.close();
                        }
                        catch (IOException ioe){
                            ioe.printStackTrace();
                        }
                }
                tLoading.setText("");
            }
            else{
                System.out.println( "Input not available." );
            }
       }
    Non capisco come mai la textfield resta invariata ho anche provato a chiamare repaint dopo setText, ma nulla. Mi sembra una cosa molto semplice ma davvero non so più cosa provare... Qualche idea? Grazie a tutti in anticipo.

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802

    Re: [JAV] JTextField setText

    Originariamente inviato da xrwnis
    Non capisco come mai la textfield resta invariata ho anche provato a chiamare repaint dopo setText, ma nulla. Mi sembra una cosa molto semplice ma davvero non so più cosa provare... Qualche idea? Grazie a tutti in anticipo.
    Dovresti fare l'operazione in un thread a parte.
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  3. #3
    Utente di HTML.it L'avatar di xrwnis
    Registrato dal
    Apr 2008
    Messaggi
    42
    Non so se ho interpretato bene le tue parole... Ho provato a fare così:
    codice:
    public class MyThread extends Thread{
            String msg;
            JTextField tf;
    
            public void MyThread(){
                start();
            }
    
            public MyThread(String s,JTextField t){
                msg=s;
                tf=t;
                start();
            }
    
    
            public void run(){
                System.out.println("prova");
                tf.setText(msg);
            }
        }
    
    metodo upload della classe FileUpload:
    
    public void upload( String ftpServer, String user, String password,String fileName, File source,JTextField tLoading ) throws MalformedURLException,IOException,InterruptedException{
    
            if (ftpServer != null && fileName != null && source != null){
                System.out.println("Connessione al server");
                MyThread t=new MyThread("Connessione al server", tLoading);
                t.join();
                StringBuffer sb = new StringBuffer( "ftp://" );
                // check for authentication else assume its anonymous access.
                if (user != null && password != null){
                    sb.append( user );
                    sb.append( ':' );
                    sb.append( password );
                    sb.append( '@' );
                }
                sb.append( ftpServer );
                sb.append( '/' );
                sb.append( fileName );
                /*
                * type ==> a=ASCII mode, i=image (binary) mode, d= file directory
                * listing
                */
                sb.append( ";type=i" );
    
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try{
    //                new MyThread("Trasferimento file "+source.getName(), tLoading);
                    System.out.println("Trasferimento file "+source.getName());
                    URL url = new URL( sb.toString() );
                    URLConnection urlc = url.openConnection();
    
                    bos = new BufferedOutputStream( urlc.getOutputStream() );
                    bis = new BufferedInputStream( new FileInputStream( source ) );
    
                    int i;
                    // read byte by byte until end of stream
                    while ((i = bis.read()) != -1){
                        bos.write( i );
                    }
                }
                finally{
    //                new MyThread("Chiusura connessione al server", tLoading);
                    System.out.println("Chiusura connessione al server");
                    if (bis != null)
                        try{
                            bis.close();
                        }
                        catch (IOException ioe){
                            ioe.printStackTrace();
                        }
                    if (bos != null)
                        try{
                            bos.close();
                        }
                        catch (IOException ioe){
                            ioe.printStackTrace();
                        }
                }
            }
            else{
                System.out.println( "Input not available." );
            }
       }
    La scritta "prova" esce al momento giusto, mentre il valore nella text viene cambiato alla fine dell'esecuzione del metodo...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.