|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: May 2005
Città: Roma
Messaggi: 7938
|
[JAVA] JProgressBar maledetta
allora, partendo dal presupposto che sono fuso a livello celebrale...dopo 10 ore di programmazione sono compatibile
![]() allora, io in un metodo che genera un file faccio ESATTAMENTE queste cose: Codice:
compilaPrimaParte(); compilaSecondaParte(); compilaTerzaParte(); compilaQuartaParte(); compilaQuintaParte(); ho capito che devo usare i thread e ho trovato pgi da ogni parte e ogni ovunque.....ma non riesco a farlo -.-
__________________
My gaming placement |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Esempio pulito pulito e aggiornato.
Codice:
import java.awt.*; import java.awt.event.*; import java.beans.*; import javax.swing.*; public class Main { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new Main().start(); } }); } private volatile boolean interrupted; private void start() { final SwingWorker<Void, Void> executor = new SwingWorker<Void, Void>() { @Override public Void doInBackground() { primaInvocazione(); setProgress(20); secondaInvocazione(); setProgress(40); terzaInvocazione(); setProgress(60); quartaInvocazione(); setProgress(80); quintaInvocazione(); setProgress(100); return null; } }; final JProgressBar progressBar = new JProgressBar(0, 100); executor.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if("progress".equals(e.getPropertyName())) { progressBar.setValue((Integer)e.getNewValue()); } } }); JButton starter = new JButton(new AbstractAction("Start") { public void actionPerformed(ActionEvent e) { executor.execute(); } }); JToolBar toolbar = new JToolBar(); toolbar.add(starter); JFrame window = new JFrame("Progress Test"); window.add(toolbar, BorderLayout.NORTH); window.add(progressBar, BorderLayout.SOUTH); window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); window.setSize(400, 300); window.setVisible(true); } private void primaInvocazione() { sleep(1000); } private void secondaInvocazione() { sleep(1000); } private void terzaInvocazione() { sleep(1000); } private void quartaInvocazione() { sleep(1000); } private void quintaInvocazione() { sleep(1000); } private void sleep(long ms) { try { Thread.sleep(ms); } catch(InterruptedException ex) { interrupted = true; } } }
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: May 2005
Città: Roma
Messaggi: 7938
|
ho risolto convertendo la classe che doveva gestirmi le operazioni in thread....cmq terrò il tuo codice a mente per il futuro
![]() sempre grazzie ![]()
__________________
My gaming placement |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 19:44.