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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| public class DemoCompteur extends JFrame {
private JPanel contentPane;
private static JLabel label1;
private static JProgressBar progressBar;
private JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DemoCompteur frame = new DemoCompteur();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DemoCompteur() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 602, 529);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 0;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
label1 = new JLabel("Compteur");
GridBagConstraints gbc_label1 = new GridBagConstraints();
gbc_label1.fill = GridBagConstraints.HORIZONTAL;
gbc_label1.insets = new Insets(0, 0, 5, 0);
gbc_label1.gridx = 0;
gbc_label1.gridy = 0;
panel.add(label1, gbc_label1);
label1.setHorizontalAlignment(SwingConstants.CENTER);
progressBar = new JProgressBar();
//progressBar.setMaximum(1000000);
GridBagConstraints gbc_progressBar = new GridBagConstraints();
gbc_progressBar.fill = GridBagConstraints.HORIZONTAL;
gbc_progressBar.insets = new Insets(0, 0, 5, 0);
gbc_progressBar.gridx = 0;
gbc_progressBar.gridy = 1;
panel.add(progressBar, gbc_progressBar);
JButton btn1 = new JButton("On compte");
GridBagConstraints gbc_btn1 = new GridBagConstraints();
gbc_btn1.fill = GridBagConstraints.HORIZONTAL;
gbc_btn1.gridx = 0;
gbc_btn1.gridy = 2;
panel.add(btn1, gbc_btn1);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
SwingWorker<Object, String> worker = new SwingWorker<Object, String>() {
@Override
protected Object doInBackground() throws Exception {
calcul(n->publish(n), p->setProgress(p));
return null;
}
@Override
protected void process(List<String> chunks) {
if ( chunks.size()>0 ) {
label1.setText(chunks.get(chunks.size()-1)+"/"+N);
}
}
};
// permet à la progressbar de progresser
worker.addPropertyChangeListener(e-> {
if ("progress".equals(e.getPropertyName())) {
progressBar.setValue((int)e.getNewValue());
}
});
worker.execute();
}
});
}
private static int N = 10000;
void calcul(Consumer<String> publisherCallback, Consumer<Integer> progressCallBack) {
for (int n=1; n<=N; n++){
publisherCallback.accept(String.valueOf(n));
progressCallBack.accept((int)(100*((double)n)/N));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
} |
Partager