Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Barra avanzamento caricamento in Java aiuto per favore

    Salve ragazzi, spero che riusciate a darmi una mano.

    Ho realizzato una applicazione. Al caricamento ho questa situazione ho una classe TestaFrame che estende JFrame, quesa classe la uso come contenitore per tutti i JPanel della mia applicazione.

    Poichè quando avvio la prima volta il software, vengono effettuate numerose operazioni, vorrei che all'avvio venga mostrata una immagine (logo dell'applicazione) e sotto una barra di scorrimento che indichi di volta in volta ciò che sta facendo il software.

    Per far questo ho trovato queste due classi.

    codice:
    package SplashScreen;
    import javax.swing.*;
    import java.awt.*;
    
    public class SplashScreen extends JFrame {
      BorderLayout borderLayout1 = new BorderLayout();
      JLabel imageLabel = new JLabel();
      JPanel southPanel = new JPanel();
      FlowLayout southPanelFlowLayout = new FlowLayout();
      JProgressBar progressBar = new JProgressBar();
      ImageIcon imageIcon;
    
      public SplashScreen(ImageIcon imageIcon) {
        this.imageIcon = imageIcon;
        try {
          jbInit();
        }
        catch(Exception ex) {
          ex.printStackTrace();
        }
      }
    
      // note - this class created with JBuilder
      void jbInit() throws Exception {
        imageLabel.setIcon(imageIcon);
        this.getContentPane().setLayout(borderLayout1);
        southPanel.setLayout(southPanelFlowLayout);
        southPanel.setBackground(Color.BLACK);
        this.getContentPane().add(imageLabel, BorderLayout.CENTER);
        this.getContentPane().add(southPanel, BorderLayout.SOUTH);
        southPanel.add(progressBar, null);
        this.pack();
      }
    
      public void setProgressMax(int maxProgress)
      {
        progressBar.setMaximum(maxProgress);
      }
    
      public void setProgress(int progress)
      {
        final int theProgress = progress;
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            progressBar.setValue(theProgress);
          }
        });
      }
    
      public void setProgress(String message, int progress)
      {
        final int theProgress = progress;
        final String theMessage = message;
        setProgress(progress);
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            progressBar.setValue(theProgress);
            setMessage(theMessage);
          }
        });
      }
      
      
    
      public void setScreenVisible(boolean b)
      {
        final boolean boo = b;
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            setVisible(boo);
          }
        });
      }
    
      private void setMessage(String message)
      {
        if (message==null)
        {
          message = "";
          progressBar.setStringPainted(false);
        }
        else
        {
          progressBar.setStringPainted(true);
        }
        progressBar.setString(message);
      }
    
    }
    main della classe splash screen

    codice:
    import javax.swing.UIManager;
    import javax.swing.ImageIcon;
    
    public class SplashScreenMain {
    
      SplashScreen screen;
    
      public SplashScreenMain() {
        // initialize the splash screen
        splashScreenInit();
        // do something here to simulate the program doing something that
        // is time consuming
        for (int i = 0; i <= 100; i++)
        {
          for (long j=0; j<50000; ++j)
          {
            String poop = " " + (j + i);
          }
          // run either of these two -- not both
          screen.setProgress("Yo " +i, i);  // progress bar with a message
         // screen.setProgress(i);           // progress bar with no message
        }
        splashScreenDestruct();
        //System.exit(0);
      }
    
      private void splashScreenDestruct() {
        screen.setScreenVisible(false);
      }
    
      public void splashScreenInit() {
        ImageIcon myImage = new ImageIcon("immagini/concepimento-dieta-della-fertilita-coppa-di-gelato2.jpg");
        screen = new SplashScreen(myImage);
        screen.setLocationRelativeTo(null);
        screen.setProgressMax(100);
        screen.setScreenVisible(true);
      }
    
      public static void main(String[] args)
      {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        SplashScreenMain mainflasj = new SplashScreenMain();
      }
    
    }
    Ora io dovrei integrare questa operazione all'interno del mio JPanel che viene caricato all'avvia nel JFrame.

    Posto la clase TestaFrame e JPanel1

    codice:
    public class TestaFrame extends JFrame{
    	PanelIngredienti ingredienti;
    	 public TestaFrame(){
    
    		    super("iGelato Versione 1.0");
    	        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	   //     getContentPane().setPreferredSize(new Dimension(1000, 600));
    	  /*      Toolkit t = Toolkit.getDefaultToolkit();
    	        Dimension screenSize = t.getScreenSize();
    
    	        double width = screenSize.getWidth();
    	        double height= screenSize.getHeight();
    	        double frameWidth = width;
    	        double frameHeight = height;
    	        Dimension d2 = new Dimension();
    	        d2.setSize(frameWidth, frameHeight);*/
    
    	        this.setPreferredSize(d2);
    	        
    	        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			this.pack();
    	        this.setVisible(true);
    	        this.setResizable(true);
    	        
    	        //adesso attacco al frame il pannello principale che sarà il cuore del programma
    	        MainPanel mainpanel = new MainPanel();  
    		    getContentPane().add(mainpanel, BorderLayout.CENTER);
    	        getContentPane().validate();
    	        //fine
    	 }//fine testaframe
    	 
    	 public static void main(String[] args) {
    	        SwingUtilities.invokeLater(new Runnable() {
    	            public void run() {
    	               TestaFrame f = new TestaFrame();
    	               
    	            }
    	        });}//fine metodo main
    
    
    }}}//fine classe generale

  2. #2
    JPanel che viene caricato all'avvio:

    codice:
    public class MainPanel extends JPanel implements PropertyChangeListener{
    	
    	static int i =0,volta =0;
    	static Componenti_Swing.TableExample table;
    	String salva;
    	Vector rows,columns;
    	static JTable ricetteTabella;
    	JScrollPane jScrollPane1,jScrollPane2;
    	static String zuccheriC,grassiC,tipo,snglC, altresoluzioniC,elemento;
    	static JFormattedTextField zuccheri,grassi,sngl,altriSolidi;
    	static Double amount = 0.00,Diff =0.00,DifferenzaRip=0.00,Quantita = 100.00, sommaSNGL=0.00,sommaAltrisolidi,DifferenzaSom=0.00,Sngl=0.00,Zuccheri=0.00,PercentualeDif=0.00;
    	MyTableModel tableModel;
    	static Double Grassi=0.00,sommaZuccheri=0.00,zuccheriint,sommaQuantita=0.00,AltriSolidi=0.00,sommaGrassi=0.00;
    	static Double ZuccheriSom=0.00,GrassiSom=0.00, SnglSom=0.00, AltriSolidiSom=0.00,QuantitaSom=0.00;
    	static Double ZuccheriDif =0.00,GrassiDif=0.00,SnglDif=0.00,AltriSolidiDif=0.00,QuantitaDif=0.00; 
    	Double ZuccheriRip=0.00, GrassiRip=0.00,SnglRip=0.00,AltriSolidiRip=0.00;
    	static JFormattedTextField quantitaRip,grassiRip,zuccheriRip,snglRip,altriSolidiRip,supportoRip,supporto,supportoDif,zuccheriDif, grassiDif,supportoSom,quantitaDif,quantitaSom,zuccheriSom,grassiSom,snglSom,altriSolidiSom,snglDif,altriSolidiDif;
    	Font fontTable;
    	static Double differenza;
    	static Double percZuccheri;
    	static Double somma =0.00;
    	boolean primoAccesso=false;
    	SplashScreen screen;
    	private DefaultListModel model = new DefaultListModel();
    	public MainPanel(){
    	   ImageIcon myImage = new ImageIcon("immagini/concepimento-dieta-della-fertilita-coppa-di-gelato2.jpg");
    		SplashScreen screen = new SplashScreen(myImage);
    		
    	    screen.setLocationRelativeTo(null);
    	    screen.setProgressMax(100);
    	    screen.setScreenVisible(true);
    	    for (int i = 0; i <= 100; i++)
    	    {
    	      for (long j=0; j<50000; ++j)
    	      {
    	        String poop = " " + (j + i);
    	      }
    	      screen.setProgress("Yo " +i, i);
    	    }
    
    	    Dimensione dimensioneComponenti = new Dimensione();
    	    Toolkit t = Toolkit.getDefaultToolkit();
    	    Dimension screenSize = t.getScreenSize();
    	    double width = screenSize.getWidth() ;
    	    double height= screenSize.getHeight() *0.9;
    	    Dimension d2 = new Dimension();
    	    d2.setSize(dimensioneComponenti.DimensionePanel());
    	    this.setPreferredSize(d2);//dimensione Frame principale
    	    
    	    
    	    Dimension dTabella = new Dimension();
    	    dTabella.setSize(dimensioneComponenti.jScrollPane2());
    
    	    Dimension dTable = new Dimension();
    	    dTable.setSize(dimensioneComponenti.jScrollPane1());
    	    
    	    //questa è la tabella dove verrà effettuato tutto il calcolo degli ingredienti
    	    rows=new Vector();    
    	    tableModel = new MyTableModel();
    	   // ricetteTabella = new JTable(new MyTableModel());
    	    ricetteTabella = new JTable(tableModel);
    	    ricetteTabella.setFont(fontTable);
    	    ricetteTabella.setRowHeight(25);
    	    ricetteTabella.setDropMode(DropMode.INSERT_ROWS);
    
    		    jScrollPane2 = new javax.swing.JScrollPane();
    		    jScrollPane2.setName("jScrollPane2");
    		    jScrollPane2.setViewportView(ricetteTabella);
    		    //ricetteTabella.setPreferredSize(dTabella);
    		    jScrollPane2.setPreferredSize(dTabella);
    		    jScrollPane2.getViewport().add(ricetteTabella);
    		    jScrollPane2.setVisible(true);
    
    		
    		    Vector vettore = new Vector();//do nome alle colonne
    		    vettore.add(new String("Ingrediente"));
    		    table = new TableExample(v, vettore);
    		    table.setRowHeight(25);
    		    table.setFillsViewportHeight(true);
    		
    		    jScrollPane1 = new javax.swing.JScrollPane();
    		    jScrollPane1.setName("jScrollPane1");
    		    jScrollPane1.setViewportView(table);
    		    jScrollPane1.setPreferredSize(dTable);
    		    jScrollPane1.getViewport().add(table);
    		    jScrollPane1.setVisible(true);
    		    
    		
    		    GridBagConstraints GBC = new GridBagConstraints();
    		      Container CR = new Container();
    		      GridBagLayout GBL = new GridBagLayout();
    		      CR.setComponentOrientation(ComponentOrientation.UNKNOWN);
    		      CR.setLayout(GBL);     
    		      this.add(CR);
    		  
    		      GBC = new GridBagConstraints();
    		      CR.add(jScrollPane2);
    		      GBC.gridx=0;
    		      GBC.gridy=0;
    		      GBC.gridwidth=8;
    		      GBC.gridheight=2;
    		      GBC.insets.top=5;
    		      GBC.insets.bottom=10;
    		      GBC.insets.left=2;
    		      GBC.insets.right=10;
    		      GBC.fill=GridBagConstraints.HORIZONTAL;
    		      GBL.setConstraints(jScrollPane2,GBC);
    		      
    		      GBC = new GridBagConstraints();
    		      CR.add(jScrollPane1);
    		      GBC.gridx=8;
    		      GBC.gridy=0;
    		      GBC.gridwidth=3;
    		      GBC.insets.top=5;
    		      GBC.insets.left=10;
    		      GBC.fill=GridBagConstraints.HORIZONTAL;
    		      GBL.setConstraints(jScrollPane1,GBC);
    		      
    		      LabelFormat lperc= new LabelFormat("in %");
    		      LabelFormat lqua= new LabelFormat("Quantità");
    		      LabelFormat lzuc= new LabelFormat("Zuccheri");
    		      LabelFormat lgra= new LabelFormat("Grassi");
    		      LabelFormat lsng= new LabelFormat("Sngl");
    		      LabelFormat laltr= new LabelFormat("AltriSolidi");
    		      
    		      GBC = new GridBagConstraints();
    			    CR.add(lperc);
    			    GBC.gridx=1;
    			    GBC.gridy=2;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(lperc,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(lqua);
    			    GBC.gridx=2;
    			    GBC.gridy=2;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(lqua,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(lzuc);
    			    GBC.gridx=3;
    			    GBC.gridy=2;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(lzuc,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(lgra);
    			    GBC.gridx=4;
    			    GBC.gridy=2;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(lgra,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(lsng);
    			    GBC.gridx=5;
    			    GBC.gridy=2;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(lsng,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(laltr);
    			    GBC.gridx=6;
    			    GBC.gridy=2;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(laltr,GBC);
    
    t
    		      Dimension dText = new Dimension();
    		      dText.setSize(dimensioneComponenti.textMainPanel());
    		       
    		        LabelFormat labelDifferenza = new LabelFormat("Quantità Mancanti: ");
    		        supportoDif = new JFormattedTextField();
    			    supportoDif.setValue(new Double(PercentualeDif));
    				supportoDif.setBackground(Color.red);
    				supportoDif.setPreferredSize(dText);
    				
    				quantitaDif = new JFormattedTextField();
    				quantitaDif.setValue(new Double(QuantitaDif));
    				quantitaDif.setBackground(Color.red);
    				quantitaDif.addPropertyChangeListener("value", this);
    				quantitaDif.setPreferredSize(dText);
    				
    				zuccheriDif = new JFormattedTextField();
    			    zuccheriDif.setValue(new Double(ZuccheriDif));
    			    zuccheriDif.setBackground(Color.red);
    			    zuccheriDif.addPropertyChangeListener("value", this);
    			    zuccheriDif.setPreferredSize(dText);
    				
    			    grassiDif = new JFormattedTextField();
    			    grassiDif.setValue(new Double(GrassiDif));
    			    grassiDif.setBackground(Color.red);
    			    grassiDif.addPropertyChangeListener("value", this);
    			    grassiDif.setPreferredSize(dText);
    			    
    			    snglDif = new JFormattedTextField();
    			    snglDif.setValue(new Double(SnglDif));
    			    snglDif.setBackground(Color.red);
    			    snglDif.addPropertyChangeListener("value", this);
    			    snglDif.setPreferredSize(dText);
    			    
    			    altriSolidiDif = new JFormattedTextField();
    			    altriSolidiDif.setValue(new Double(AltriSolidiDif));
    			    altriSolidiDif.setBackground(Color.red);
    			    altriSolidiDif.addPropertyChangeListener("value", this);
    			    altriSolidiDif.setPreferredSize(dText);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(labelDifferenza);
    			    GBC.gridx=0;
    			    GBC.gridy=3;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(labelDifferenza,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(supportoDif);
    			    GBC.gridx=1;
    			    GBC.gridy=3;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(supportoDif,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(quantitaDif);
    			    GBC.gridx=2;
    			    GBC.gridy=3;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(quantitaDif,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(zuccheriDif);
    			    GBC.gridx=3;
    			    GBC.gridy=3;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(zuccheriDif,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(grassiDif);
    			    GBC.gridx=4;
    			    GBC.gridy=3;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(grassiDif,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(snglDif);
    			    GBC.gridx=5;
    			    GBC.gridy=3;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(snglDif,GBC);
    			    
    			    GBC = new GridBagConstraints();
    			    CR.add(altriSolidiDif);
    			    GBC.gridx=6;
    			    GBC.gridy=3;
    			    GBC.insets.top=5;
    			    GBC.insets.left=10;
    			    GBC.fill=GridBagConstraints.HORIZONTAL;
    			    GBL.setConstraints(altriSolidiDif,GBC);	
    			    }
    	
    	}//fine MainPanel
    	
    	
    	 static class MyTableModel extends DefaultTableModel {
    		    private static final Object[] columnNames = { "INGREDIENTI", "in %","Quantità","Zuccheri","Grassi","S.N.G.L.","Altri Solidi","Solidi Totale" ,"Costo" };
    
    		    public MyTableModel() {
    		        super(columnNames, 1);    // 10 righe iniziali (vuote)
    		    }
    
    		    public boolean isCellEditable(int rowIndex, int columnIndex) {
    		          return true;  
    		    }
    
    		    public Class getColumnClass(Float columnIndex) {
    		        return Float.class;    // Le due colonne sono numeri interi
    		    }
    
    		    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    		        super.setValueAt(aValue, rowIndex, columnIndex);	
    		}
    	 
    	 private static String getNextString(int count) {
    	        StringBuffer buf = new StringBuffer();
    	        for (int i = 0; i < 5; i++) {
    	            buf.append(String.valueOf(count));
    	            buf.append(",");
    	        }
    
    	        // remove last newline
    	        buf.deleteCharAt(buf.length() - 1);
    	        return buf.toString();
    	    }	
    	}
    	 public void propertyChange(PropertyChangeEvent e) {
    	        Object source = e.getSource();
    	        if (source == zuccheri) {
    	            Zuccheri = ((Number)zuccheri.getValue()).doubleValue();
    	            zuccheriRip.setText(Double.toString(Zuccheri));
    	        } else if (source == grassi) {
    	            Grassi = ((Number)grassi.getValue()).doubleValue();
    	            grassiRip.setText(Double.toString(Grassi));
    	        } else if (source == sngl) {
    	        	Sngl = ((Number)sngl.getValue()).doubleValue();
    	            snglRip.setText(Double.toString(Sngl));
    	        }else if (source == altriSolidi) {
    	        	AltriSolidi = ((Number)altriSolidi.getValue()).doubleValue();
    	            //altriSolidiRip.setText(Double.toString(amount));
    	        	altriSolidiRip.setValue(AltriSolidi);
    	        }
    	    }//fine metodo change property
    	 
    	
    }//fine classe MainPanel

    L'errore che ottengo è questo che se avvio la classe splash screen da sola si vede l'immagine e la barra di scorrimento che avanza fino a 100.

    Preso lo stesso codice e portato in quesa classe vedo tutto insieme cioè quando carica tutta l'interfaccia mi fa vedere l'immagine con l'indicatore che segna il 100%

    Potete aiutrarmi????

  3. #3
    Scusate ragazzi, possibile che nessuno mi sa dare un aiuto??

    Possibile che nessuno abbia mai afforntato questo approccio nello sviluppo di una applicazione in java???

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da bircastri
    Scusate ragazzi, possibile che nessuno mi sa dare un aiuto??
    Prova magari a vedere tra i miei Java Examples (link sul mio sito) nella sezione AWT/Swing – Concurrency dove ci sono esempi che mostrano come eseguire una operazione "lunga" aggiornando progressivamente una JProgressBar.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    prima di tutto grazie per la risposta e il link del tuo sito

    allora ho visto che gestisci il tutto inizializzando un thread. in pratica tutto avviene qui
    correggimi se sbaglio

    codice:
     private class WorkRunnable implements Runnable {
            public void run() {
                try {
                    for (int i = 0; i <= 100; i++) {
                        updateProgress(i);
    
                        // A sleep .... just to do something.
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {}
                    }
                } finally {
                    // Always executes this.
                    workDone();
                }
            }
        }
    ora io ho questa situazione:

    All'avvio la mia applicazione parte con un JFrame vuoto sul quale io attacco un JPanel. Ho una classe PanelMIo che estende un Jpanel in cui ho tutto, componenti logica ecc ecc all'avvio questo JPAnel viene costruito e montto sul JFrame.

    Io vorrei che quando l'applicazione parte venga mostrata l'immagine del prog con le scritte ad es (Connessione col database, Poi inizializzazione dell'intergaccia ecc ecc)

    devo includere tutto il caricamento all'interno di questa tua classe???

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.