View Javadoc
1   package edu.jiangxin.apktoolbox.swing.extend.plugin;
2   
3   import edu.jiangxin.apktoolbox.utils.Constants;
4   
5   import javax.swing.*;
6   import java.awt.*;
7   import java.io.Serial;
8   
9   public class ProgressBarDialog extends JDialog {
10      @Serial
11      private static final long serialVersionUID = 1L;
12      final JProgressBar progressBar = new JProgressBar();
13      final JLabel progressLabel = new JLabel("");
14  
15      public ProgressBarDialog(String title) {
16          setTitle(title);
17          setSize(400, 100);
18          setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
19          //Swing Worker can't update GUI components in modal jdialog
20          //https://stackoverflow.com/questions/54496606/swing-worker-cant-update-gui-components-in-modal-jdialog
21          setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
22          setLocationRelativeTo(null);
23  
24          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
25  
26          JPanel contentPanel = new JPanel();
27          contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));
28          add(contentPanel);
29  
30          contentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
31  
32          progressBar.setMaximum(100);
33          progressBar.setValue(0);
34          progressBar.setPreferredSize(new Dimension(300, 20));
35          contentPanel.add(progressBar);
36  
37          contentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
38  
39          contentPanel.add(progressLabel);
40  
41          contentPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
42  
43          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
44      }
45  
46      public void setValue(int value) {
47          progressBar.setValue(value);
48          progressLabel.setText(value + "%");
49      }
50  }