thermos111
Messages postés20Date d'inscriptiondimanche 30 octobre 2016StatutMembreDernière intervention12 février 2017
-
Modifié par KX le 12/02/2017 à 21:41
Bonjour,
Je réalise un formulaire pour rajouter mes restaurant moi même sur mon application java et je veut faire un ComboBox pour mes clé étrangère mais je ne sais pas comment faire est ce que quelqu’un peut m'expliquer, j'utilise la méthode MVC en POO.
Voici le code de ma vue (Ps: j'ai créer une méthode pour sélectionner le champs de ma table que je veut)
VueAjouterRestaurant.java
package Vues;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import Controleur.Restaurant;
import Modele.ModeleRestaurant;
public class VueAjouterRestaurant extends JPanel implements ActionListener
{
//champs
private JTextField nomRestaurant = new JTextField();
private JTextField heureOuverture = new JTextField();
private JTextField heureFermeture = new JTextField();
private JTextField FermetureExcept = new JTextField();
private JTextField nombreTables = new JTextField();
private JTextField nombreCouverts = new JTextField();
private JTextField telephoneResto = new JTextField();
private JComboBox idTypeResto = new JComboBox();
private JComboBox codePosVille = new JComboBox();
//bouton
private JButton btAnnuler = new JButton("Annuler");
private JButton btEnregistrer = new JButton("Insérer");
public VueAjouterRestaurant()
{
this.setBounds(0, 0, 350, 325); //('', '','largeur', 'longeur')
this.setLayout(new GridLayout(5, 2)); //colonne - ligne
//ajout label et bouton sur fenêtre
this.add(new JLabel("Nom du restautant"));
this.add(this.nomRestaurant);
this.add(new JLabel("heure d'ouverture"));
this.add(this.heureOuverture);
this.add(new JLabel("heure de fermeture"));
this.add(this.heureFermeture);
this.add(new JLabel(" Fermeture exceptionelle "));
this.add(this.FermetureExcept);
this.add(new JLabel(" Nombre de tables"));
this.add(this.nombreTables);
this.add(new JLabel("Nombre de couverts"));
this.add(this.nombreCouverts);
this.add(new JLabel(" Telephone "));
this.add(this.telephoneResto);
this.add(new JLabel(" type restaurant "));
this.add(this.idTypeResto);
this.add(new JLabel(" code postal ville "));
this.add(this.codePosVille);
this.add(this.btAnnuler);
this.add(this.btEnregistrer);
this.btAnnuler.addActionListener(this);
this.btEnregistrer.addActionListener(this); //ActionListener : evenement cliquable
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this.btAnnuler)
{
this.nomRestaurant.setText("");
this.heureOuverture.setText("");
this.heureOuverture.setText("");
this.FermetureExcept.setText("");
this.nombreTables.setText("");
this.nombreCouverts.setText("");
this.telephoneResto.setText("");
}
else if(e.getSource() == this.btEnregistrer)
{
String nomRestaurant = this.nomRestaurant.getText();
String ferExcp = this.FermetureExcept.getText();
int id, HO, HF,nbT, nbC, Tel, TPR, Cp;
try
{
id = 0;
HO = Integer.parseInt(this.heureOuverture.getText());
HF = Integer.parseInt(this.heureFermeture.getText());
nbT = Integer.parseInt(this.nombreTables.getText());
nbC = Integer.parseInt(this.nombreCouverts.getText());
Tel = Integer.parseInt(this.telephoneResto.getText());
TPR = Integer.parseInt(this.idTypeResto.getToolTipText());
Cp = Integer.parseInt(this.codePosVille.getToolTipText());
Restaurant unTypeResto = new Restaurant(id, nomRestaurant, HO, HF, ferExcp, nbT, nbC, Tel, TPR, Cp);
}
catch(NumberFormatException exp)
{
JOptionPane.showMessageDialog(this, "insertion raté"); //message
}
}
}
public Object [][] extraireTyperestaurant()
{
ArrayList<Restaurant> lesTypesRestaurants = ModeleRestaurant.selectAll();
Object [][]donnees = new Object[lesTypesRestaurants.size()][1];
int i = 0;
for(Restaurant unTypeRestaurant : lesTypesRestaurants)
{
donnees [i][0] = unTypeRestaurant.getTypeResto();
i++;
}
return donnees;
}
}