Ajouter des données de la BDD à la JComboBox
Bonjour à tous.
Je voudrais avoir les "Items" de la JComboBox à partir des données d'une colonne d'une table dans PostgreSQL. J'ai pas su comment faire !
La connexion est impeccable.
J'ai réussi à envoyer et recevoir des données dans d'autre classe mais pour la JComboBox ...
Bon, j'ai créé un package DAO où il y a deux classes
Achat et AchatDAO et je voudrais récupérer les tuples de la colonne Ref d'une table Imprimante
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package DAO;
public class Achat {
private String reference;
private int quantite;
public Achat(String reference, int quantite) {
this.reference = reference;
this.quantite = quantite;
}
public Achat() {}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public int getQuantite() {
return quantite;
}
public void setQuantite(int quantite) {
this.quantite = quantite;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;
public class AchatDAO {
private Connection connexion = null;
protected Connection getConnected() {
String url = "jdbc:postgresql://localhost:5432/postgres";
String login = "postgres";
String password = "postgres";
try {
Class.forName("org.postgresql.Driver");
connexion = DriverManager.getConnection(url, login, password);
}
catch (Exception e) {
System.out.println("Problème lors de la connexion sur la base");
}
return connexion;
}
public void showReference(Achat c) {
connexion = this.getConnected();
try {
Statement state = connexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT Ref FROM Imprimante order by ref";
ResultSet res = state.executeQuery(query);
JComboBox reference = new JComboBox();
while (res.next()) {
reference.addItem(res.getString("Ref"));
}
res.close();
}
catch(SQLException e) {
System.out.println("Attention. Exception lors de l'excéution de la requête : " + e);
}
}
} |
Et, sur la package principal, il y a la classe ZDialog
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import DAO.Achat;
import DAO.AchatDAO;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ZDialog extends JDialog {
private ZDialogInfo zInfo = new ZDialogInfo();
private JLabel referenceLabel, quantiteLabel;
private JTextField quantite;
private JComboBox reference;
public ZDialog(JFrame parent, String title, boolean modal) {
super(parent, title, modal);
this.setSize(600, 150);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.initComponent();
}
public ZDialogInfo showZDialog() {
this.setVisible(true);
return this.zInfo;
}
private void initComponent() {
JPanel panReference = new JPanel();
panReference.setBackground(Color.white);
panReference.setPreferredSize(new Dimension(220, 60));
panReference.setBorder(BorderFactory.createTitledBorder("Référence de la cartouche"));
reference = new JComboBox();
AchatDAO achatDAO = new AchatDAO();
Achat a = new Achat();
achatDAO.showReference(a);
/*reference.addItem("Masculin");
reference.addItem("Féminin");
reference.addItem("Indéterminé");*/
referenceLabel = new JLabel("Référence : ");
panReference.add(referenceLabel);
panReference.add(reference);
JPanel panQuantite = new JPanel();
panQuantite.setBackground(Color.white);
panQuantite.setPreferredSize(new Dimension(220, 60));
panQuantite.setBorder(BorderFactory.createTitledBorder("Quantité d'achat"));
quantiteLabel = new JLabel("Quantité : ");
quantite = new JTextField("1");
quantite.setPreferredSize(new Dimension(90, 25));
panQuantite.add(quantiteLabel);
panQuantite.add(quantite);
JPanel content = new JPanel();
content.setBackground(Color.white);
content.add(panReference);
content.add(panQuantite);
JPanel control = new JPanel();
JButton okBouton = new JButton("OK");
okBouton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
zInfo = new ZDialogInfo((String)reference.getSelectedItem(), quantite.getText());
setVisible(false);
}
});
JButton cancelBouton = new JButton("Annuler");
cancelBouton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
}
});
control.add(okBouton);
control.add(cancelBouton);
this.getContentPane().add(content, BorderLayout.CENTER);
this.getContentPane().add(control, BorderLayout.SOUTH);
}
} |
C'est Quoi le problème exactement?
Pour mieux gérer les CmboBox, je te conseille d'utiliser les Modèles, voici un exemple de liaison d'une BD à un ComboBox:
http://sakoba.byethost13.com/?s=combobox&x=0&y=0