Bonjour,

J'ai un petit soucis d'intégration de ma progressbar avec mon code. J'avoue aussi que c'est la première fois que je code une progressbar j'en ai jamais fait et bien sur avant de venir vers vous j'ai étudier la chose fait énormément de test et d'intégration du code mais la je bloque complètement et j'avance plus donc je vous remercié d'être indulgent et de me donnée des explications pour que je ne puisse plus y revenir dessus et que ce soit acquis.

Ma progressbar fonctionne parfaitement avec un bouton jusque la tout va bien. Je l'ai placé dans une Class que je fais appel quand j'en ai besoin.

Dans mon exemple je fais des updates sur des tables avec sqlite et je voudrais intégrer cette progressbar.

J'ai une JFrame avec des boutons et sur un bouton qui s'appel settings j'appel une JDialog ou a l'intérieur j'ai des options de couleur etc et lorsque je fini mes paramêtres j'appuie sur le bonton Save et la j'envoie mes requêtes Updates.

Après envoie je ferme ma JDialog et la ma ProgressBar en JDialog doit apparaître hors je n'y arrive pas et en plus comme ce sont des requêtes je suis en mode indéterminé car ça duré 2 seconde comme 30 secondes.

J'ai laisser en commentaire le test barbare que j'avais fais car impossible d'afficher la ProgressBar par contre avec un index en ++ dans le while elle s'affiche mais une fois que les requêtes sont terminé donc je sèche merci pour vôtre aide

Voici mon code ProgressBar

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import java.beans.PropertyChangeEvent;
import java.net.URL;
 
public class BackgroundWorker extends SwingWorker<Void, Void> {
 
        public static JProgressBar pb;
        public static int index = 0;
        private JDialog dialog;
 
public BackgroundWorker() {
 
    addPropertyChangeListener((PropertyChangeEvent evt) -> {
        if ("progress".equalsIgnoreCase(evt.getPropertyName())) {
 
            if (dialog == null) {
                dialog = new JDialog();
                URL url = ClassLoader.getSystemResource("images/icon.png");
                Image img = dialog.getToolkit().getImage(url);
                dialog.setIconImage(img);
                dialog.setLayout(new GridBagLayout());
                dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                //dialog.setAlwaysOnTop(true);
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(2, 2, 2, 2);
                gbc.weightx = 1;
                gbc.gridy = 0;
                dialog.add(new JLabel("Processing..."), gbc);
                pb = new JProgressBar();
                pb.setValue(0);
                pb.setStringPainted(true);
                gbc.gridy = 1;
                dialog.add(pb, gbc);
                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setModal(true);
                //JDialog.setDefaultLookAndFeelDecorated(true);
                dialog.setVisible(true);
            }
            pb.setValue(getProgress());
        }
    });
    }
 
    @Override
    protected void done() {
        if (dialog != null) {
            dialog.dispose();
        }
    }
 
    @Override
    protected Void doInBackground() throws Exception {       
        //for (index = 0; index < 100 ; index++) {
        while(index<=100){
            setProgress(index);
            try{Thread.sleep(50);} // make the process last a while
               catch (InterruptedException e){}
        //index ++;
        }
            //Thread.sleep(100);
       // }
    return null;
    }
 
}
Mon code Bouton Save

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
private void jb_SaveActionPerformed(java.awt.event.ActionEvent evt) throws IOException, SQLException {                                                           
                    int p = JOptionPane.showConfirmDialog(this, "Are Your Sure You Want To Save Your Configuration", "Confirmation",JOptionPane.OK_CANCEL_OPTION);
                    if(p == 0){
 
                    this.dispose();
                    new BackgroundWorker().execute(); 
                    SQLiteQueries.updateOptions();                   
           }                   
    }
et une parti du code SQL car il y a 100 requêtes

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static void updateOptions() throws SQLException{		
		try {        
                        //OptionsForm.index = 5;
                        PreparedStatement updateOptions;                    
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET Base=\"" +OptionsForm.CB_Base.getSelectedItem()+ "\"");
                            updateOptions.executeUpdate();
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET EditXboxData=\"" +OptionsForm.CB_EditXbox.getSelectedItem()+ "\"");
                            updateOptions.executeUpdate();
                            //OptionsForm.index = 10;
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET EditBodyData=\"" +OptionsForm.CB_EditBody.getSelectedItem()+ "\"");
                            updateOptions.executeUpdate();
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET AddCustomers=\"" +OptionsForm.CB_Customer.getSelectedItem()+ "\"");                            
                            updateOptions.executeUpdate();
                            //OptionsForm.index = 30;
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET Display=\"" +OptionsForm.CB_Display.getSelectedItem()+ "\"");
                            updateOptions.executeUpdate();
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET Mail=\"" +OptionsForm.CB_Mail.getSelectedItem()+ "\"");
                            updateOptions.executeUpdate();
                            //OptionsForm.index = 50;
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET TechMail=\"" +OptionsForm.t_EmailTechnician.getText()+ "\"");
                            updateOptions.executeUpdate();
                            String str = new String(OptionsForm.t_AppPassword.getPassword());
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET PassWordApp=\"" +str+ "\"");
                            updateOptions.executeUpdate();
                            //OptionsForm.index = 70;
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET SMTP=\"" +OptionsForm.t_SMTP_ADDRESS.getText()+ "\"");
                            updateOptions.executeUpdate();
                            updateOptions = connection.prepareStatement("UPDATE AppOptions SET PORT=\"" +OptionsForm.t_SMTP_PORT.getText()+ "\"");                            
                            updateOptions.executeUpdate();
                            //OptionsForm.index = 100;
                  } catch (SQLException e) {e.printStackTrace();}
    }