ComboBox dynamique en java à partir d'une table access

babillonnik Messages postés 8 Date d'inscription samedi 11 juillet 2009 Statut Membre Dernière intervention 10 octobre 2010 - 15 juil. 2009 à 17:49
babillonnik Messages postés 8 Date d'inscription samedi 11 juillet 2009 Statut Membre Dernière intervention 10 octobre 2010 - 17 juil. 2009 à 09:40
Bonjours;
j'ai fas comme tu m'a dit Dark et j'ai travaillé sur un programme mais il ne veux pas fonctionner, le comboBoc (Emplacement) n'affiche rien
Voici mon programme qui est exécutable, si vous m'aider je vous serai reconnaissant:

package test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class Ajout extends javax.swing.JFrame {

static String j="";

public Ajout(String j) {
this.j=j;
initComponents();
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setTitle("Ajout d'un produit");
}

private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jTextField4 = new javax.swing.JComboBox();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jTextField5 = new javax.swing.JTextField();
jTextField6 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N

jLabel1.setText("Saisissez les coordonnées du nouveau produit:");

jLabel2.setText("Identifiant");

jLabel3.setText("Utilisateur");

jLabel4.setText("Fournisseur");

jLabel6.setText("Libellé");

jLabel7.setText("Catégorie");

jTextField5.setText("");

jTextField6.setText("");

jButton1.setText("<=");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
dispose();
//new Menu(j).setVisible(true);
}
catch (Exception e) {
System.out.println(e.toString());
}
jButton1ActionPerformed(evt);
}
});

jButton4.setText("Reset");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
dispose();
new Ajout(j).setVisible(true);
}
catch (Exception e) {
System.out.println(e.toString());
}
jButton4ActionPerformed(evt);
}
});

jButton3.setText("Valider");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
int i=0;
String a=jTextField1.getText();
String b=jTextField2.getText();
String c=jTextField3.getText();
String e=jTextField5.getText();
String f=jTextField6.getText();
String d=jTextField4.getActionCommand();

try{
if(c.equals("")||b.equals("")||e.equals("")||f.equals("")){
JOptionPane.showMessageDialog(null, "Champs non saisie!","",JOptionPane.WARNING_MESSAGE);
}
else{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Techno";
Connection conn = DriverManager.getConnection(url,"","");
Statement stat = conn.createStatement();
ResultSet result= stat.executeQuery("select Nom from Emplacement where Nom='"+d+"'");

if(!result.next()){
JOptionPane.showMessageDialog(null, "Emplacement non existant!","",JOptionPane.WARNING_MESSAGE);
}
else{
Statement stat2 = conn.createStatement();
ResultSet r= stat2.executeQuery("select * from Emplacement ");
while(r.next()){
Statement stat3 = conn.createStatement();
ResultSet rest= stat3.executeQuery("select Utilisateur from e"+r.getString(2)+" where Identifiant='"+c+"'");
if(rest.next()){
JOptionPane.showMessageDialog(null, "Cet identifiant existe déjà, veuillez le changer","",JOptionPane.WARNING_MESSAGE);
i=1;
}
rest.close();
stat3.close();
}
r.close();
stat2.close();

if(i==0){
Statement sta3 = conn.createStatement();
sta3.execute("insert into e"+d+" values ('"+c+"','"+e+"','"+f+"','"+b+"','"+a+"')");
JOptionPane.showMessageDialog(null, "Ajout réussit!","",JOptionPane.WARNING_MESSAGE);
dispose();
//new Menu(j).setVisible(true);
sta3.close();
}
}
result.close();
stat.close();
conn.close();
}
}
catch (Exception ex) {
System.out.println(ex.toString());
}
jButton3ActionPerformed(evt);
}
});

jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");

jLabel5.setText("Emplacement");

/* try{
jTextField4.removeAllItems();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Techno";
Connection conn = DriverManager.getConnection(url,"","");
Statement stat = conn.createStatement();
ResultSet res= stat.executeQuery("select id_empl,Nom,Etage from Emplacement ");
while (res.next())
{
jTextField4.addItem(res.getString(2));
}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
//jTextField4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "En utilisation", "Déplacé", "Inutilisable"}));
//jTextField4.setName("jComboBox1");
*/


jTextField4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try{
jTextField4.removeAllItems();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Techno";
Connection conn = DriverManager.getConnection(url,"","");
Statement stat = conn.createStatement();
ResultSet result= stat.executeQuery("select id_empl,Nom,Etage from Emplacement ");

while (result.next())
{
jTextField4.addItem(result.getString(2));

}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}

jComboBox1ActionPerformed(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()
.addComponent(jLabel1)
.addGap(167, 167, 167))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(287, Short.MAX_VALUE)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7)
.addComponent(jLabel6))
.addGap(351, 351, 351))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(85, 85, 85))
.addComponent(jLabel4))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField1)
.addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
.addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
.addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
.addContainerGap(130, Short.MAX_VALUE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(306, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(21, 21, 21)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(13, 13, 13)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4))
.addContainerGap())
);

pack();
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
}

public void keyTyped(KeyEvent ee){}

public void keyPressed(KeyEvent eee){}

public void keyReleased(KeyEvent e){

try{
jTextField4.removeAllItems();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Techno";
Connection conn = DriverManager.getConnection(url,"","");
Statement stat = conn.createStatement();
ResultSet result= stat.executeQuery("select id_empl,Nom,Etage from Emplacement ");

while (result.next())
{
jTextField4.addItem(result.getString(2));

}
}
catch(Exception ex)
{
System.out.println(ex.toString());
}

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ajout("").setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
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.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JComboBox jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
// End of variables declaration

}







/******************************************/



Je pense que le probléme réside dans l'appel des fonctions :
public void keyTyped(KeyEvent ee){}

public void keyPressed(KeyEvent eee){}

public void keyReleased(KeyEvent e){}

Mais si je veux que le remplissage de la comboBox soit dynamisue selon ma base je doit les utilisées
C'est quoi le probléme ?????

1 réponse

babillonnik Messages postés 8 Date d'inscription samedi 11 juillet 2009 Statut Membre Dernière intervention 10 octobre 2010
17 juil. 2009 à 09:40
Svp réponder moi car lorsque je n'utilise pa netbeans j'airéussi à créer mon combobox mé dé que je doi utiliser le netBeans , je ne sais plus ou introduir lé données.
Ne lisez pas tout, seulement la partie du 'ActionPerformer' du jTextField4 qui est ma combobox et les 3 methodesquil fau introduir
merci de me donner umpeu de votre précieux temps
0
Rejoignez-nous