Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 16 su 16
  1. #11
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    313
    Puoi fare un esempio per piacere?

  2. #12
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da tcc
    Puoi fare un esempio per piacere?
    Ma l'avevi già scritto tu. Nel try avevi invocato invokeLater a cui passavi un Runnable che aveva il compito di aggiornare la progress bar.

    Poi, scusa, parlavi di scrivere un file .... ma dove fai la scrittura? La farai nel run() della classe UpdateBar?
    Andrea, Senior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  3. #13
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    313
    Non ho capito.
    La classe UpdateBar deve implementare Runnable,e nel costruttore gli metto

    codice:
    for (int i = 0; i <= 100; i++) {
          final int percent = i;
          try {
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                pb.updateBar(percent);
              }
            });
            java.lang.Thread.sleep(100);
          } catch (InterruptedException e) {
            ;
          }
        }
    e nell'actio performed cosa gli faccio fare

  4. #14
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Ok .... esempio completo (sicuramente non perfetto ... l'ho buttato già velocemente):

    codice:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    
    public class TestFrame extends JFrame
    {
        private JButton button;
        private JProgressBar progressBar;
    
        public TestFrame ()
        {
            super ("Test Frame");
            setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            setSize (300, 300);
    
            button = new JButton ("Scrivi file");
            add (button, BorderLayout.NORTH);
    
            progressBar = new JProgressBar (0, 100);
            add (progressBar, BorderLayout.SOUTH);
    
            button.addActionListener (new ActionListener ()
            {
                public void actionPerformed (ActionEvent e)
                {
                    button.setEnabled (false);
                    progressBar.setValue (0);
    
                    Thread t = new Thread (new ScrittoreFile ());
                    t.start ();
                }
            });
        }
    
    
        private class ScrittoreFile implements Runnable
        {
            public void run ()
            {
                try
                {
                    PrintStream ps = new PrintStream ("file.txt");
    
                    for (int i = 1; i <= 100; i++)
                    {
                        final int value = i;
    
                        ps.println ("blabla");
    
                        SwingUtilities.invokeLater (new Runnable () {
                            public void run () {
                                progressBar.setValue (value);
                            }
                        });
    
                        Thread.sleep (100);
                    }
    
                    ps.close ();
                }
                catch (Exception e)
                {
                    SwingUtilities.invokeLater (new Runnable () {
                        public void run () {
                            JOptionPane.showMessageDialog (TestFrame.this, "Errore nella scrittura", "ERRORE", JOptionPane.ERROR_MESSAGE);
                        }
                    });
                }
    
                SwingUtilities.invokeLater (new Runnable () {
                    public void run () {
                        button.setEnabled (true);
                    }
                });
            }
        }
    
    
        public static void main (String[] args)
        {
            SwingUtilities.invokeLater (new Runnable ()
            {
                public void run ()
                {
                    TestFrame f = new TestFrame ();
                    f.setVisible (true);
                }
            });
        }
    }
    Tu dirai: ma ... ci sono un po' troppi Runnable. Eh beh ... questo è il bello di lavorare con Swing che non è thread-safe!
    Andrea, Senior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  5. #15
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    313
    L'esempio funziona alla perfezione
    ma io non riesco

    Ho fatto la classe UpdateBar:
    codice:
    public class UpdateBar implements Runnable{
        
        private final JProgressBar pb; 
        public UpdateBar(JProgressBar progresBar){      
          this.pb = progresBar;
          System.out.println("llll : ");
            
       }
    
       public void run() {
        for (int i = 0; i <=100; i++) {
          final int percent = i;
          try {
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
               pb.setValue(percent);
              }
            });
            java.lang.Thread.sleep(100);
          } catch (InterruptedException e) {
            
          }
        }
        }
    ed ho nell'actionperformed :

    codice:
    if(e.getActionCommand().equals("Create corpus")){
              System.out.println("Create corpus"); 
              
              this.showWindow(iwin  = new ProgresBar(GraphicInterfaceAgSpit.getInstance(this)));               
              UpdateBar ping = new UpdateBar(((ProgresBar)iwin).getProgresBar());
             new Thread(ping).start();         
        }
    Dove showWindow mostra la finestra dove è present la JprogressBar e gliela passo ad UpdateBar,ma ho provato a fare stampare qulacosa nel costruttore di UpdateBar ma non entra
    ,perchè????

  6. #16
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    313
    Ho provato a mettere tutto nella classe ProgresBar ma ancora niente

    codice:
    public class ProgresBar extends JDialog implements IWindow,Runnable{
         
         private JProgressBar progresBar;
         private JPanel panelProgresBar;
           
       public ProgresBar(JFrame frame){
           super(frame,"Progres bar",true);
           this.setResizable(false);
           this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
           this.setBounds(450, 300, 400, 100);
           this.progresBar = new JProgressBar(0,100);
           this.panelProgresBar = new JPanel();
           this.panelProgresBar.add(this.progresBar);
           this.getContentPane().setLayout(new BorderLayout());
           this.getContentPane().add(BorderLayout.NORTH,new JLabel("             Wait please......"));
           this.getContentPane().add(BorderLayout.CENTER,this.panelProgresBar);
           }
    
        
       
       public JProgressBar getProgresBar(){
           return this.progresBar;
       }
       
      
        
        public void showWindow() {
            this.setVisible(true); 
        }
    
        public void updateBar(final int c) {
            SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                progresBar.setValue(c);
            }
        });
        
    }
    
        public void run() {
            for (int i = 1; i <= 100; i++) {
               updateBar(i);
             try {
                Thread.sleep(5);
                 } catch (InterruptedException ie) {
                     
                }
            }
        } 
       
        
        
        
        
    }

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 © 2026 vBulletin Solutions, Inc. All rights reserved.