Puoi fare un esempio per piacere?
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.Originariamente inviato da tcc
Puoi fare un esempio per piacere?
Poi, scusa, parlavi di scrivere un file .... ma dove fai la scrittura? La farai nel run() della classe UpdateBar?
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
Java Versions Cheat Sheet
Non ho capito.
La classe UpdateBar deve implementare Runnable,e nel costruttore gli metto
e nell'actio performed cosa gli faccio farecodice: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) { ; } }
Ok .... esempio completo (sicuramente non perfetto ... l'ho buttato già velocemente):
Tu dirai: ma ... ci sono un po' troppi Runnable. Eh beh ... questo è il bello di lavorare con Swing che non è thread-safe!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); } }); } }
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%)
Java Versions Cheat Sheet
L'esempio funziona alla perfezione
ma io non riesco
Ho fatto la classe UpdateBar:
ed ho nell'actionperformed :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) { } } }
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 entracodice: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(); }
,perchè????![]()
![]()
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) { } } } }