简体   繁体   中英

Want display the search results in the jtextarea

//RESEARCH jframe code




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package InterfaceDico;

import GestionDictionnaire.GestionDico;
import javax.swing.JOptionPane;

/**
 *
 * @author Franck
 */
public class RechercherMot extends javax.swing.JFrame {

    /**
     * Creates new form RechercherMot
     */
    public RechercherMot() {
        initComponents();
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        valider = new javax.swing.JButton();
        entrer_mot = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        entrer_definition = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("                                                       RECHERCHER MOT ");

        jLabel2.setText("MOT A RECHERCHER :");

        valider.setText("RECHERCHER");
        valider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validerActionPerformed(evt);
            }
        });

        entrer_mot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entrer_motActionPerformed(evt);
            }
        });

        entrer_definition.setColumns(20);
        entrer_definition.setRows(5);
        jScrollPane1.setViewportView(entrer_definition);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(valider))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(entrer_mot, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(entrer_mot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(valider)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void validerActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
         String mot= entrer_mot.getText();
        entrer_mot.setText("");
        String definition = entrer_definition.getText();
        entrer_definition.setText("");


            if(mot.equalsIgnoreCase("")){
                JOptionPane.showMessageDialog(null, "données incorrectes", "Erreur", JOptionPane.ERROR_MESSAGE);
            }
        else{
                GestionDico dico =new GestionDico();
                dico.RechercherMot(mot);

Here when I do enter_definition=setText(dico.RechercherMot(mot)) an error occurred.

                //JOptionPane.showMessageDialog(null,"votre mot a été trouvé avec succès ","reussite",JOptionPane.INFORMATION_MESSAGE);


            }
    }                                       

    private void entrer_motActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:

    }                                          

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RechercherMot.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RechercherMot.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RechercherMot.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RechercherMot.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RechercherMot().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextArea entrer_definition;
    private javax.swing.JTextField entrer_mot;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton valider;
    // End of variables declaration                   
}





//management function code




 public void RechercherMot(String mot){
        String ret="select * from dictionnaire where mot=?";
           try {
            PreparedStatement ps = (PreparedStatement) ConnectionDico.getCon().prepareStatement(ret);

            ps.setString(1,mot);
            //execution
           ResultSet resultat= ps.executeQuery();
           while(resultat.next()){
               System.out.println("mot = " +resultat.getString("mot")+" "+"definition = "+resultat.getString("definition")+" ");
               JOptionPane.showMessageDialog(null,"votre mot a été trouvé avec succès et sa définition est la suivante:\n"+" mot = " +resultat.getString("mot")+" "+"/ definition = "+resultat.getString("definition")+" ","reussite",JOptionPane.INFORMATION_MESSAGE);
           }
        } catch (SQLException ex) {
            Logger.getLogger(GestionDico.class.getName()).log(Level.SEVERE, null, ex);
             JOptionPane.showMessageDialog(null, "le mot n'a pas été retrouvé dans le dictionnaire", "Erreur", JOptionPane.ERROR_MESSAGE);
        }

    }
}

The return type of the method RechercherMot(String mot) is void. what that means is the it is not returning anything. Now you have used enter_definition.setText(dico.RechercherMot(mot)) where enter_definition is an instance of JTextArea and the function setText requires a String as a parameter. But your dico.RechercherMot(mot) returns void or nothing. So you get the compile error that void cannot be convert to String;

Now in order to remove the error you have to change the return type of RechercherMot(String mot) to String and return a valid String using a return statement in the method.

Here what I understand from what you have stated that it should return the text formatted result set of the query that you are executing. So my suggestion is that you could declare a String variable say result and initialise to an empty string before you create the prepared statement like:

String result = "";

Then inside the loop while(resultat.next()) replace

System.out.println("mot = " +resultat.getString("mot")+" "+"definition = "+resultat.getString("definition")+" ");

With

result += "mot = " +resultat.getString("mot")+" "+"definition = "+resultat.getString("definition")+" \n";

And finally in the last line of the method, after the catch block insert :

return result;

Of course change the return type of the method from void to String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM