western AJMM | D'accord, ce qu'ils disent est beau ... si j'ai bien compris, tu utilise cette méthode! Dans ce cas peux tu me dire pourquoi le code suivant ne marche pas (la barre de défilement ainsi que le contenu du label ne sont mis à jour)?
Code :
- /*!
- Cette classe doit permettre de ...
- */
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class VerificationValidate
- {
- private final static JFrame fenetre = new JFrame();
- private final JLabel label = new JLabel("" );
- static private String contenu_label = new String();
- private JProgressBar pb = new JProgressBar(0, 1000);
- static private int valeur_pb = 0;
- private JButton annuler = new JButton("Cancel" );
- static private boolean valeur_annuler = false;
-
- private void construitFenetre()
- {
- Container cp = fenetre.getContentPane();
- cp.removeAll();
- cp.setLayout(new GridBagLayout());
- label.setText(contenu_label);
- constrain(cp,label,0,0,1,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1.0,1.0,0,0,0,0);
- constrain(cp,pb,0,1,1,1,GridBagConstraints.BOTH,GridBagConstraints.CENTER,1.0,1.0,0,0,0,0);
- constrain(cp,annuler,0,2,1,1,GridBagConstraints.NONE,GridBagConstraints.CENTER,1.0,1.0,0,0,0,0);
- fenetre.setSize(400, 100);
- fenetre.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- valeur_annuler = true;
- }
- });
- annuler.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- valeur_annuler = true;
- }
- });
- }
- private void constrain(Container container,Component component,int grid_x,int grid_y,int grid_width,int grid_height,int fill,int anchor,double weight_x,double weight_y,int top,int left,int bottom,int right)
- {
- GridBagConstraints c = new GridBagConstraints();
- c.gridx = grid_x;
- c.gridy = grid_y;
- c.gridwidth = grid_width;
- c.gridheight = grid_height;
- c.fill = fill;
- c.anchor = anchor;
- c.weightx = weight_x;
- c.weighty = weight_y;
- if (top+bottom+left+right > 0) c.insets = new Insets(top, left, bottom, right);
- ((GridBagLayout)container.getLayout()).setConstraints(component, c);
- container.add(component);
- }
-
- public VerificationValidate()
- {
- contenu_label = "Preparing...";
- valeur_pb = 0;
- construitFenetre();
- fenetre.show();
- }
- public boolean copier(File file_in, File file_out)
- {
- fenetre.setTitle("Copy "+file_in.getName()+" sur "+file_out.getName());
- boolean res = true;
- long taille_in = file_in.length();
- int i = 0;
- int j;
- try
- {
- BufferedInputStream in = new BufferedInputStream(new FileInputStream (file_in));
- DataOutputStream out = new DataOutputStream(new FileOutputStream(file_out));
- while(
- ((j = in.read()) >= 0)
- &&
- (!valeur_annuler)
- )
- {
- valeur_pb = (int) (1000 * ((double)i++) / ((double)taille_in));
- out.write(j);
- fenetre.validate();
- }
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(
- fenetre,
- "Error occurs during copy.",
- "Error:",
- JOptionPane.ERROR_MESSAGE);
- res = false;
- }
- return res;
- }
- public static void main(String[]argv)
- {
- if(argv.length != 2)
- {
- JOptionPane.showMessageDialog(
- null,
- "Il faut preciser le nom de fichier source et le nom de fichier arriv? ...",
- "Error:",
- JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- VerificationValidate vv = new VerificationValidate();
- File file_in = new File(argv[0]);
- if(! file_in.exists())
- {
- JOptionPane.showMessageDialog(
- null,
- "Le fichier source n'existe pas",
- "Error:",
- JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- File file_out = new File(argv[1]);
- if(vv.copier(file_in, file_out))
- {
- JOptionPane.showMessageDialog(
- null,
- "La copie: tout va bien",
- "Error:",
- JOptionPane.INFORMATION_MESSAGE);
- System.exit(0);
- }
- else
- {
- JOptionPane.showMessageDialog(
- null,
- "La copie: tout va mal ;-)",
- "Error:",
- JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- }
- }
|
|