Affichage table

Messages postés
1
Date d'inscription
mercredi 16 mars 2016
Dernière intervention
16 mars 2016
-
Salut je suis nouveau sur le site et sur java developpement.en effet j'ai pris un tuto que j'aimerais refaire mais je coince sur l'affichage dans la table mais la selection d'une ligne affiche les donnees dans les champs.le code est :

import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

class connexion {
}

/*
 * 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.
 */
/**
 * @author Brown
 */
public class Gestion_des_Notes extends javax.swing.JFrame {
   /**
    * Creates new form Gestion_des_Notes
    */
   connexion con = new connexion();
   Statement stm;
   ResultSet St;
   DefaultTableModel model = new DefaultTableModel();

   public Gestion_des_Notes() {
      initComponents();
      model.addColumn("id");
      model.addColumn("Nom");
      model.addColumn("Prenom");
      model.addColumn("branche");
      model.addColumn("Note");
      try {
         stm = con.obtenirconnexion().createStatement();
         ResultSet St = stm.executeQuery("Select * from etudiant");
         while (St.next()) {
            model.addRow(new Object[] { St.getString("idetudiant"), St.getString("Nom"), St.getString("Prenom"),
                  St.getString("branche"), St.getString("Note") });
         }
      } catch (Exception e) {
         System.err.println(e);
         table.setModel(model);
      }
   }

   /**
    * 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();
      jLabel3 = new javax.swing.JLabel();
      jLabel4 = new javax.swing.JLabel();
      jLabel5 = new javax.swing.JLabel();
      txtid = new javax.swing.JTextField();
      txtnom = new javax.swing.JTextField();
      txtprenom = new javax.swing.JTextField();
      txtnote = new javax.swing.JTextField();
      txtbranche = new javax.swing.JComboBox();
      jPanel2 = new javax.swing.JPanel();
      jScrollPane1 = new javax.swing.JScrollPane();
      table = new javax.swing.JTable();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jButton3 = new javax.swing.JButton();
      jButton4 = new javax.swing.JButton();
      jButton5 = new javax.swing.JButton();
      jButton6 = new javax.swing.JButton();
      jTextField6 = new javax.swing.JTextField();
      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setPreferredSize(new java.awt.Dimension(500, 600));
      getContentPane().setLayout(null);
      jLabel1.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
      jLabel1.setText("Id :");
      jLabel2.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
      jLabel2.setText("Nom :");
      jLabel3.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
      jLabel3.setText("Prénom :");
      jLabel4.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
      jLabel4.setText("Branche :");
      jLabel5.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
      jLabel5.setText("Note :");
      txtid.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
      txtid.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtidActionPerformed(evt);
         }
      });
      txtnom.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
      txtnom.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtnomActionPerformed(evt);
         }
      });
      txtprenom.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
      txtprenom.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtprenomActionPerformed(evt);
         }
      });
      txtnote.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
      txtnote.setForeground(new java.awt.Color(0, 51, 51));
      txtnote.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtnoteActionPerformed(evt);
         }
      });
      txtbranche.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Info", "Ace", "Tuto" }));
      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                  .addContainerGap()
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
                              javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtid, javax.swing.GroupLayout.Alignment.TRAILING,
                              javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                              javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtnom, javax.swing.GroupLayout.Alignment.TRAILING,
                              javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                              javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtprenom, javax.swing.GroupLayout.Alignment.TRAILING,
                              javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                              javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtnote, javax.swing.GroupLayout.Alignment.TRAILING,
                              javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                              javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtbranche, javax.swing.GroupLayout.Alignment.TRAILING,
                              javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                              javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(26, 26, 26)));
      jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup().addGap(24, 24, 24)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1).addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2).addComponent(txtnom, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(18, 18, 18)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3).addComponent(txtprenom, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(18, 18, 18)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4).addComponent(txtbranche, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5).addComponent(txtnote, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addContainerGap(46, Short.MAX_VALUE)));
      getContentPane().add(jPanel1);
      jPanel1.setBounds(10, 11, 286, 240);
      table.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][] { { null, null, null, null, null }, { null, null, null, null, null },
                  { null, null, null, null, null }, { null, null, null, null, null },
                  { null, null, null, null, null }, { null, null, null, null, null },
                  { null, null, null, null, null } },
            new String[] { "Title 1", "Title 2", "Title 3", "Title 4", "Title 5" }) {
         Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class,
               java.lang.String.class, java.lang.String.class };

         public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
         }
      });
      table.addMouseListener(new java.awt.event.MouseAdapter() {
         public void mouseClicked(java.awt.event.MouseEvent evt) {
            tableMouseClicked(evt);
         }
      });
      jScrollPane1.setViewportView(table);
      javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
      jPanel2.setLayout(jPanel2Layout);
      jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE));
      jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE));
      getContentPane().add(jPanel2);
      jPanel2.setBounds(300, 10, 460, 420);
      jButton1.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
      jButton1.setText("Rechercher");
      getContentPane().add(jButton1);
      jButton1.setBounds(10, 270, 90, 30);
      jButton2.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
      jButton2.setText("Ajouter");
      getContentPane().add(jButton2);
      jButton2.setBounds(10, 320, 90, 30);
      jButton3.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
      jButton3.setText("Modifier");
      getContentPane().add(jButton3);
      jButton3.setBounds(110, 320, 90, 30);
      jButton4.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
      jButton4.setText("Supprimer");
      getContentPane().add(jButton4);
      jButton4.setBounds(210, 320, 90, 30);
      jButton5.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
      jButton5.setText("Actualiser");
      getContentPane().add(jButton5);
      jButton5.setBounds(10, 360, 90, 30);
      jButton6.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
      jButton6.setText("Réaliser par");
      getContentPane().add(jButton6);
      jButton6.setBounds(110, 360, 90, 30);
      jTextField6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
      getContentPane().add(jTextField6);
      jTextField6.setBounds(112, 270, 180, 30);
      jTextField6.getAccessibleContext().setAccessibleDescription("");
      pack();
   }// </editor-fold>

   private void deplace(int i) {
      try {
         txtid.setText(model.getValueAt(i, 0).toString());
         txtnom.setText(model.getValueAt(i, 1).toString());
         txtprenom.setText(model.getValueAt(i, 2).toString());
         txtbranche.setSelectedItem(model.getValueAt(i, 3).toString());
         txtnote.setText(model.getValueAt(i, 4).toString());
      } catch (Exception e) {
         System.err.println(e);
         JOptionPane.showMessageDialog(null, "Erreur de déplacement" + e.getLocalizedMessage());
      }
   }

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

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

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

   private void tableMouseClicked(java.awt.event.MouseEvent evt) {
      // TODO add your handling code here:
      try {
         int i = table.getSelectedRow();
         deplace(i);
      } catch (Exception e) {
         JOptionPane.showMessageDialog(null, "Erreur de deplacement de " + e.getLocalizedMessage());
      }
   }

   private void txtnoteActionPerformed(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(Gestion_des_Notes.class.getName()).log(java.util.logging.Level.SEVERE,
               null, ex);
      } catch (InstantiationException ex) {
         java.util.logging.Logger.getLogger(Gestion_des_Notes.class.getName()).log(java.util.logging.Level.SEVERE,
               null, ex);
      } catch (IllegalAccessException ex) {
         java.util.logging.Logger.getLogger(Gestion_des_Notes.class.getName()).log(java.util.logging.Level.SEVERE,
               null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
         java.util.logging.Logger.getLogger(Gestion_des_Notes.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 Gestion_des_Notes().setVisible(true);
         }
      });
   }

   // Variables declaration - do not modify
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;
   private javax.swing.JButton jButton5;
   private javax.swing.JButton jButton6;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JLabel jLabel4;
   private javax.swing.JLabel jLabel5;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTextField jTextField6;
   private javax.swing.JTable table;
   private javax.swing.JComboBox txtbranche;
   private javax.swing.JTextField txtid;
   private javax.swing.JTextField txtnom;
   private javax.swing.JTextField txtnote;
   private javax.swing.JTextField txtprenom;
   // End of variables declaration
}
Afficher la suite 

Votre réponse

1 réponse

Messages postés
4
Date d'inscription
jeudi 31 mars 2016
Dernière intervention
31 mars 2016
0
Merci
Bonjour mon frère, ton code est illisible, met le dans un objet code
proposé par l'éditeur!!!
Whismeril
Messages postés
12362
Date d'inscription
mardi 11 mars 2003
Statut
Contributeur
Dernière intervention
10 décembre 2018
-
C.est ce qu'il a fait mais y a un bug, qui a été signalé à peine quelques heures après son post
KX
Messages postés
15855
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
10 décembre 2018
-
J'ai réédité le message pour prendre en compte le langage.
Les balises <code > ne suffisent pas, c'est <code java > qu'il faut utiliser.
Commenter la réponse de kupata

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.