JCombobox

Fermé
fafall Messages postés 8 Date d'inscription lundi 29 octobre 2012 Statut Membre Dernière intervention 12 juillet 2013 - 9 juil. 2013 à 14:13
Bonjour ,
S'il vous plait j'ai besoin d'aide car cela fait des jours que je n'avance pas dans mon projet je veux alimenter mes Jcombobox par des donnée de ma table mais cela ne passe pas.

Voici le code que j'ai eu à écrire
package JInterfaceGraphique;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;

public class Paiement extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel Ltitre = null;
private JPanel P_label = null;
private JPanel P_texbox = null;
private JLabel L_numEtu = null;
private JLabel LnumeroChambre = null;
private JComboBox Cbnumcham = null;
private JLabel Année = null;
private JTextField T_année = null;
private JButton BValider = null;
private JButton Bannuler = null;
private JComboBox C_numEtu = null;

/**
* This is the default constructor
*/
public Paiement() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(394, 237);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel(), null);
jContentPane.add(getP_label(), null);
jContentPane.add(getP_texbox(), null);
}
return jContentPane;
}

/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
Ltitre = new JLabel();
Ltitre.setText("Formulaire de paiement");
Ltitre.setBounds(new Rectangle(82, 7, 162, 20));
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(1, 0, 377, 30));
jPanel.add(Ltitre, null);
}
return jPanel;
}

/**
* This method initializes P_label
*
* @return javax.swing.JPanel
*/
private JPanel getP_label() {
if (P_label == null) {
Année = new JLabel();
Année.setBounds(new Rectangle(4, 97, 49, 24));
Année.setText("Année");
LnumeroChambre = new JLabel();
LnumeroChambre.setBounds(new Rectangle(3, 56, 133, 24));
LnumeroChambre.setText("Numero de chambre");
L_numEtu = new JLabel();
L_numEtu.setText("Numero Etudiant");
L_numEtu.setBounds(new Rectangle(6, 19, 127, 24));
P_label = new JPanel();
P_label.setLayout(null);
P_label.setBounds(new Rectangle(-3, 31, 190, 166));
P_label.add(L_numEtu, null);
P_label.add(LnumeroChambre, null);
P_label.add(Année, null);
P_label.add(getBValider(), null);
}
return P_label;
}

/**
* This method initializes P_texbox
*
* @return javax.swing.JPanel
*/
private JPanel getP_texbox() {
if (P_texbox == null) {
P_texbox = new JPanel();
P_texbox.setLayout(null);
P_texbox.setBounds(new Rectangle(187, 30, 190, 168));
P_texbox.add(getCbnumcham(), null);
P_texbox.add(getT_année(), null);
P_texbox.add(getBannuler(), null);
//P_texbox.add(C_numbEtu, null);
P_texbox.add(getC_numEtu(), null);
}
return P_texbox;
}

/**
* This method initializes Cbnumcham
*
* @return javax.swing.JComboBox
*/
private JComboBox getCbnumcham() {
if (Cbnumcham == null) {
Cbnumcham = new JComboBox();
Cbnumcham.setBounds(new Rectangle(15, 60, 140, 25));

}
return Cbnumcham;
}

/**
* This method initializes T_année
*
* @return javax.swing.JTextField
*/

private JTextField getT_année() {
if (T_année == null) {
T_année = new JTextField();
T_année.setBounds(new Rectangle(15, 103, 139, 25));
}
return T_année;
}

/**
* This method initializes BValider
*
* @return javax.swing.JButton
*/
private JButton getBValider() {
if (BValider == null) {
BValider = new JButton();
BValider.setBounds(new Rectangle(22, 144, 104, 20));
BValider.setText("Valider");
}
return BValider;
}

/**
* This method initializes Bannuler
*
* @return javax.swing.JButton
*/
private JButton getBannuler() {
if (Bannuler == null) {
Bannuler = new JButton();
Bannuler.setBounds(new Rectangle(35, 145, 108, 21));
Bannuler.setText("Annuler");
}
return Bannuler;
}

/**
* This method initializes C_numEtu
*
* @return javax.swing.JComboBox
*/
private JComboBox getC_numEtu() {
if (C_numEtu == null) {
C_numEtu = new JComboBox();
C_numEtu.setBounds(new Rectangle(15, 20, 140, 26));
}
return C_numEtu;
}
public static Connection initConnection ()
{
Connection MaConnection= null;
String url = "jdbc:mysql://localhost:3306/loyer";
try{
//Le chargement du pilote
Class.forName("org.gjt.mm.mysql.Driver");
}catch (Exception e) {
System.out.println("Impossible décharger le pilote jdbc");
}
try{
//L'établissement de la connexion
MaConnection = DriverManager.getConnection(url,"root","admin");

}
catch (SQLException se) {
System.out.println("Connexion Impossible");
}
return MaConnection;
}

public void actionPerformed(ActionEvent e) {
if (e.getSource()== Cbnumcham){
Connection maCo = initConnection();
if (maCo == null) return;

String req= "SELECT numchambre FROM chambre ORDER BY Nom_Champ1";

try {
Statement stmt;

stmt = maCo.createStatement();
ResultSet res = stmt.executeQuery(req);
while(res.next())
{
Cbnumcham.addItem(res.getString(1));


}
res.close();
}
catch (SQLException se) {

se.printStackTrace();
}

}
//Fin remplir_Jcomb()



}






}
A voir également: