/* * compteurjetons.java * * Created on February 18, 2008, 2:50 PM */ package compteurjetons; import java.io.File; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JFileChooser; /** * * @author maoue3 */ public class Main extends javax.swing.JFrame { /** Creates new form compteurjetons */ public Main() { initComponents(); fc = new JFileChooser(); worker_ = new Worker(champNbJetons, barreProgres); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // private void initComponents() { champFichier = new javax.swing.JTextField(); boutonFichier = new javax.swing.JButton(); jSeparator1 = new javax.swing.JSeparator(); jLabel1 = new javax.swing.JLabel(); champNbJetons = new javax.swing.JTextField(); boutonCompter = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); boutonAnnuler = new javax.swing.JButton(); barreProgres = new javax.swing.JProgressBar(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); champFichier.setEditable(false); boutonFichier.setText("Choisir fichier"); boutonFichier.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { boutonFichierActionPerformed(evt); } }); jLabel1.setText("Nombre de jetons :"); champNbJetons.setEditable(false); boutonCompter.setText("Compter"); boutonCompter.setEnabled(false); boutonCompter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { boutonCompterActionPerformed(evt); } }); jLabel2.setText("Progres :"); boutonAnnuler.setText("Annuler"); boutonAnnuler.setEnabled(false); boutonAnnuler.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { boutonAnnulerActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(champFichier, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32) .addComponent(boutonFichier)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(champNbJetons, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(30, 30, 30)) .addGroup(layout.createSequentialGroup() .addComponent(barreProgres, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE) .addGap(25, 25, 25))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(boutonAnnuler, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(boutonCompter, javax.swing.GroupLayout.Alignment.LEADING)) .addGap(33, 33, 33)) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(34, 34, 34) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(boutonFichier) .addComponent(champFichier, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(26, 26, 26) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(boutonCompter) .addComponent(champNbJetons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(16, 16, 16) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(boutonAnnuler) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(barreProgres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(30, Short.MAX_VALUE)) ); pack(); }// private void boutonCompterActionPerformed(java.awt.event.ActionEvent evt) { Long nbJetons = worker_.compter(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { System.out.print(worker_.getProgress()); barreProgres.setValue(worker_.getProgress()); } }); boutonCompter.setEnabled(false); boutonAnnuler.setEnabled(true); champNbJetons.setText(nbJetons.toString()); } private void boutonFichierActionPerformed(java.awt.event.ActionEvent evt) { int returnVal = fc.showOpenDialog(Main.this); if (returnVal == JFileChooser.APPROVE_OPTION) { // On détermine le fichier à ouvrir File fichier_ = fc.getSelectedFile(); champFichier.setText(fichier_.getPath()); // On envoie le fichier à ouvrir au worker qui aura à le traiter worker_.setFichier(fichier_); // Activer le bouton pour permettre le comptage boutonCompter.setEnabled(true); } else { // Do nothing } } private void boutonAnnulerActionPerformed(java.awt.event.ActionEvent evt) { worker_.cancel(true); champNbJetons.setText("Annulé"); boutonAnnuler.setEnabled(false); boutonCompter.setEnabled(true); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JProgressBar barreProgres; private javax.swing.JButton boutonAnnuler; private javax.swing.JButton boutonCompter; private javax.swing.JButton boutonFichier; private javax.swing.JTextField champFichier; private javax.swing.JTextField champNbJetons; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JSeparator jSeparator1; // End of variables declaration private final JFileChooser fc; private Worker worker_; } -------------------------- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package compteurjetons; import javax.swing.SwingWorker; import java.io.File; import java.io.FileNotFoundException; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.awt.List; import javax.swing.JTextField; import javax.swing.JProgressBar; /** * * @author Max */ public class Worker extends SwingWorker { public Worker(JTextField champNbJetons, JProgressBar barre) { compteur_ = new CompteurJetons(); } public void setFichier(File fichier) { fichier_ = fichier; } public Long compter(PropertyChangeListener caller) { observateur_ = caller; return (doInBackground()); } public Long doInBackground() { long nbJetons = 0; try { nbJetons = compteur_.Compte(fichier_, new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { setProgress(compteur_.getProgres()); observateur_.propertyChange(new PropertyChangeEvent(this,"worker",null,null)); } }); } catch (FileNotFoundException erreur) { javax.swing.JOptionPane.showMessageDialog(null, "Fichier non trouvé", "Erreur de lecture du fichier", javax.swing.JOptionPane.INFORMATION_MESSAGE); } return nbJetons; } private CompteurJetons compteur_; File fichier_; PropertyChangeListener observateur_; }