Ciao!![]()
Mi scuso fin da ora per la mancanza di commenti nel codice che ho dovuto rimuovere per restare nei 13k caratteri di limite
Ho un problema alquanto strano dal quale non riesco ad uscire.. Allora prima di tutto ho creato una classe "ProgressBar" che estende JFrame. La classe in questione tramite i metodi open(), close(), setPbValue(int i) controlla e modifica la finestra che contiene la progress bar.
I sorgenti per comodità li piazzo tutti nel fondo del messaggio
Ora, testando la classe così com'è questa funziona e la progressbar scorre senza problemi.
Nel momento in cui io vado ad istanziare un oggetto di ProgressBar in un'altra classe ho il problema che non avviene correttamente il refresh della barra di progressione e quindi rimane un "pop-up" bianco fino al termine dell'operazione. Perchè dico che non avviene il refresh? Perchè in un ciclo di prova
ogni volta che l'applicazione si ferma a causa del message dialog che aspetta l'input da parte dell'utente la finestra aperta da ProgressFrame è disegnata correttamente sullo schermo con il valore corretto.codice:ProgressFrame prog = new ProgressFrame(title); prog.open(); for(int i=0; i<=100; i++) { prog.setPbValue(i%1); JOptionPane.showMessageDialog(null,"pause until ok"); } prog.close();
Se invece io rimuovo il comando
la finestra contenente la progress bar non si visualizza correttamente. In particolare rimane bianca (senza nemmeno l'indicazione del titolo o scritta alcuna) durante tutta l'esecuzione del ciclo.codice:JOptionPane.showMessageDialog(null,"pause until ok");
Ho provato a modificare il ciclo in modo da inserire un ritardo causato da
ma non ottengo nessun miglioramento.codice:Thread.pause(100);
È interessante notare che il metodo
stampa correttamente sulla console il valore corretto di pb.codice:public void setPbValue(int i) { pb.setValue(i); System.err.println("\t PB: "+pb.getValue()+"/100"); }
Vi posto qui le tre classi del mio progetto:
ProgressBar.java
La classe Tools che interagisce direttamente con la ProgressBarcodice:package programmi; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.HeadlessException; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; public class ProgressFrame extends JFrame { private static final long serialVersionUID = 1L; private static Tools t = new Tools(); private JFrame win = new JFrame(); private static JLabel titleLabel; private static JLabel versionLabel; private static JPanel NPanel = new JPanel(); private static JPanel CPanel = new JPanel(); private JProgressBar pb; private Dimension screenSize; private Dimension frameSize; private static Color bg = Color.white; @SuppressWarnings("static-access") public ProgressFrame(String title) throws HeadlessException { screenSize = Toolkit.getDefaultToolkit ().getScreenSize (); frameSize = win.getSize (); pb = new JProgressBar(0,100); titleLabel = t.getTitle(); versionLabel = t.getLabelFromString(title, Color.black, Color.white, new Font("Serif", Font.BOLD, 14), JLabel.CENTER); win.setLayout(new BorderLayout()); NPanel.setLayout(new BorderLayout()); NPanel.add(BorderLayout.NORTH, titleLabel); NPanel.add(BorderLayout.SOUTH, versionLabel); CPanel.setLayout(new BorderLayout()); pb.setValue(0); pb.setStringPainted(true); pb.setSize(300, 30); CPanel.add(BorderLayout.EAST, new JLabel(" ")); CPanel.add(BorderLayout.WEST, new JLabel(" ")); CPanel.add(BorderLayout.NORTH, new JLabel(" ")); CPanel.add(BorderLayout.SOUTH, new JLabel(" ")); CPanel.add(BorderLayout.CENTER, pb); NPanel.setBackground(bg); CPanel.setBackground(bg); pb.setBackground(bg); win.add(BorderLayout.NORTH, NPanel); win.add(BorderLayout.CENTER, CPanel); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.pack(); win.setLocation ((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2); } public void open() { JOptionPane.showConfirmDialog(null, "Iniziare il Download?"); win.setVisible(true); } public void close() { win.setVisible(false); } public void setPbValue(int i) { pb.setValue(i); System.out.println("\t PB: "+pb.getValue()+"/100"); } }
Tools.java
ed infine la classe principale che utilizza il tuttocodice:package programmi; import java.awt.Color; import java.awt.Font; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Tools { public static String title = ""; public Tools() { } public static JLabel getVersion() { JLabel result = new JLabel("© 2011 - Zago Mattia - Version: 2.0", JLabel.CENTER); [omiss....] } public static JLabel getTitle() { [omiss....] } public static JLabel getLabelFromString(String text, Color fg, Color bg, Font font, int pos) { [omiss....] } public URL getUrlFromString(String url) { [omiss....] } public void chmod(String file) throws IOException, InterruptedException { [omiss....] } public void download(URL url, String FileName) throws IOException { title="Download di '"+FileName.substring(FileName.lastIndexOf("/")+1)+"' in corso"; URLConnection conn; conn = url.openConnection(); conn.connect(); InputStream streamIN = conn.getInputStream(); File f = new File(FileName); OutputStream streamOUT = new FileOutputStream(f); int dimensione = conn.getContentLength(); double progress; long bytesCopied = 0; byte[] buffer = new byte[4096]; int bytes; ProgressFrame prog = new ProgressFrame(title); System.err.println("Apro prog"); prog.open(); while ( (bytes = streamIN.read( buffer )) != -1 ){ streamOUT.write( buffer, 0, bytes ); bytesCopied += bytes; progress = ((100*bytesCopied)/dimensione); System.err.println("\tprogress: "+progress); prog.setPbValue((int) progress); } /* CICLO DI PROVA CON THREAD.SLEEP for(int i=0; i<=100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } prog.setPbValue(i); } */ streamOUT.close(); streamIN.close(); prog.close(); } public void unzip(String file) throws FileNotFoundException, IOException { [omiss....] } private static void copyInputStream(int dimensione, InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[4096]; int bytes; while ( (bytes = in.read( buffer )) != -1 ){out.write( buffer, 0, bytes );} in.close(); out.close(); } public boolean askQuestion(String question) { int q = JOptionPane.showConfirmDialog(null, question); if (q == 0) {return true;} if (q == 2) {return askQuestion(question);} return false; } public String askString(String question, String title, int messageType) { return JOptionPane.showInputDialog(null, question, title, messageType); } public Object optionBox(Object[] selectionValues, String initialSelection, String question, String title) { JDialog.setDefaultLookAndFeelDecorated(true); return JOptionPane.showInputDialog(null, question, title, JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection); } public void forceClose(String message, String title, int status) { JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE); System.exit(status); } }
Amunaptra.java
Edit: modifica titolocodice:package main; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.HeadlessException; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.MalformedURLException; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; import programmi.Eclipse; import programmi.Firefox; import programmi.ProgressFrame; import programmi.Tools; @SuppressWarnings({"static-access","unused"}) public class Amunaptra { private static Color bg = Color.white; private static JButton btnOK = new JButton("Download"); private static Tools t = new Tools(); /** * @param args */ public static void main(String[] args) { // Crea Frame Primario e Secondario JFrame primaryWin = new JFrame("Amunaptra"); JPanel secondaryWin = new JPanel(); // Crea componenti frame secondario JPanel NPanel = new JPanel(), CPanel = new JPanel(), SPanel = new JPanel(); // Crea etichette per titolo, versione e logo JLabel titleLabel = t.getTitle(); JLabel versionLabel = t.getVersion(); JLabel logo = getImage(); // JBUTTON btnOK.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if(chkFir.isSelected()) { Firefox fir = new Firefox(); fir.download(); fir.extract(); fir.chmod(); } if(chkEcl.isSelected()) { [omiss....] } if(chkUT4.isSelected()) { [omiss....] } // CHIUDE TUTTO System.exit(0); } } ); // Colori JLabel titleLabel.setFont(new Font("Serif", Font.BOLD, 16)); titleLabel.setForeground(Color.getHSBColor(12, 0.99f, 0.19f)); titleLabel.setBackground(bg); versionLabel.setFont(new Font("Serif", Font.PLAIN, 12)); versionLabel.setForeground(Color.gray); versionLabel.setBackground(bg); logo.setBackground(bg); // Manage secondary component // ------ NORTH NPanel.setLayout(new BorderLayout()); NPanel.add(BorderLayout.NORTH, titleLabel); NPanel.add(BorderLayout.SOUTH, versionLabel); // ------ CENTER CPanel.setLayout(new BorderLayout()); CPanel.add(optionPanel()); // ------ SOUTH SPanel.add(btnOK); // ------ Common NPanel.setBackground(bg); CPanel.setBackground(bg); SPanel.setBackground(bg); // Setup secondary JFrame secondaryWin.setLayout(new BorderLayout()); secondaryWin.add(BorderLayout.NORTH, NPanel); secondaryWin.add(BorderLayout.CENTER, CPanel); secondaryWin.add(BorderLayout.SOUTH, SPanel); // Setup primary JFrame primaryWin.setLayout(new BorderLayout()); primaryWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); primaryWin.getContentPane().add(logo, BorderLayout.WEST); primaryWin.getContentPane().add(secondaryWin, BorderLayout.EAST); primaryWin.pack(); // APRI FINESTRA Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize (); Dimension frameSize = primaryWin.getSize (); primaryWin.setLocation ((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2); primaryWin.setVisible(true); } private static JLabel getImage() { [omiss....] } private static JCheckBox chkUT4 = new JCheckBox("Urban Terror 4.01"); private static JCheckBox chkFir = new JCheckBox("Firefox 6.02"); private static JCheckBox chkEcl = new JCheckBox("Eclipse EE"); private static JPanel optionPanel() { [omiss....] } }


Rispondi quotando