0

Since one week now, I am looking for how to make JProgressBar to a result set. Could you give me, please, an example a sample code of a progress bar applied on a result set?

This a sample code with a button and the action done by clicking on it.

JButton btnRechercher = new JButton("Rechercher");
    btnRechercher.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Connection conn = ConnectionManager.getInstance().getConnection();
            try {
                String sql = "SELECT id, nom, prenom, niveau, localisation, pays, secteur1 FROM infos WHERE (niveau LIKE ? AND  localisation LIKE ? AND pays LIKE ? AND secteur1 LIKE ?) OR (MATCH (titre_cv1, titre_cv2, titre_cv3) AGAINST (? IN BOOLEAN MODE))";

                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.setObject(1, ""+comboNiveau.getSelectedItem()+"");
                stmt.setObject(2, ""+comboLocal.getSelectedItem()+"");
                stmt.setObject(3, ""+comboPays.getSelectedItem()+"");
                stmt.setObject(4, ""+comboSecteur.getSelectedItem()+"");
                stmt.setString(5, ""+motCle.getText()+"");

                ResultSet rs = stmt.executeQuery();

                DefaultTableModel model = new DefaultTableModel();
                model.setColumnIdentifiers(new String []{"id","nom","prenom","niveau","localisation","pays","secteur1"}); 
                    while (rs.next()) {
                        model.addRow(new Object[]{rs.getObject("id"),rs.getObject("nom"),rs.getObject("prenom"),rs.getObject("niveau"),rs.getObject("localisation"),rs.getObject("pays"),rs.getObject("secteur1")});
                    }
                    table.setModel(model);
            } catch (Exception e1) {
                System.err.println(e1);
            }
            ConnectionManager.getInstance().close();
        }
    });

I add that :

class ThreadAvanceBarre extends Thread {

        public ThreadAvanceBarre(JProgressBar ProgressBar){     
        }

        public void run(){
        try {
            Connection conn = ConnectionManager.getInstance().getConnection();

            String sql = "SELECT id, nom, prenom, niveau, localisation, pays, secteur1 FROM infos WHERE (niveau LIKE ? AND  localisation LIKE ? AND pays LIKE ? AND secteur1 LIKE ?) OR (MATCH (titre_cv1, titre_cv2, titre_cv3) AGAINST (? IN BOOLEAN MODE))";

            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1, ""+comboNiveau.getSelectedItem()+"");
            stmt.setObject(2, ""+comboLocal.getSelectedItem()+"");
            stmt.setObject(3, ""+comboPays.getSelectedItem()+"");
            stmt.setObject(4, ""+comboSecteur.getSelectedItem()+"");
            stmt.setString(5, ""+motCle.getText()+"");
            ResultSet rs = stmt.executeQuery();
            DefaultTableModel model = new DefaultTableModel();
            model.setColumnIdentifiers(new String []{"id","nom","prenom","niveau","localisation","pays","secteur1"}); 
            while (rs.next()) {
                model.addRow(new Object[]{rs.getObject("id"),rs.getObject("nom"),rs.getObject("prenom"),rs.getObject("niveau"),rs.getObject("localisation"),rs.getObject("pays"),rs.getObject("secteur1")});
            }
            table.setModel(model);
        } catch (Exception e1) {
            System.err.println(e1);
        }
        ConnectionManager.getInstance().close();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lamine
  • 13
  • 4
  • "JprogressBar applied on a resulset": what does that mean? What have you tried? – JB Nizet Jul 21 '13 at 12:57
  • What i would like to do is when a i click on the button, a JProgressBar appears for showing me the progression of the actionPerformed. – Lamine Jul 21 '13 at 13:03
  • The code currently executed in actionPerformed() must be executed in a separate thread. Read the documentation of [SwingWorker](http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html), and try to adapt the examples to your code. – JB Nizet Jul 21 '13 at 13:05
  • I add the code to the main question !!! – Lamine Jul 21 '13 at 13:36
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 3) A single blank line of white space in source code is *always* enough. – Andrew Thompson Jul 21 '13 at 18:47
  • Hi Andrew, could you give me a sample code with my example please ??? – Lamine Jul 21 '13 at 19:01
  • How will you know how many rows will be returned in the resultset? If you don't know how many rows there are to process, you won't be able to meaningfully set a progress bar. You could run the query twice, first using a 'select count' to determine how many rows there are? – Graham Griffiths Jul 22 '13 at 08:11

0 Answers0