Partage
  • Partager sur Facebook
  • Partager sur Twitter

jointure sql entre jcombobox et jtable

    26 avril 2016 à 14:39:34

    salut tout le monde svp je voudrais le code dont quand je selectionne un article dans un jcombobox et affiche son type dans un jtabe

    voici mon code 

    package disignes;

    import javax.swing.JPanel;

    import java.awt.Frame;

    import java.awt.BorderLayout;

    import javax.swing.JDialog;

    import java.awt.Dimension;

    import java.awt.Rectangle;

    import java.awt.GridBagLayout;

    import javax.swing.JLabel;

    import javax.swing.JComboBox;

    import javax.swing.JOptionPane;

    import javax.swing.JScrollPane;

    import javax.swing.JTable;

    import javax.swing.JButton;

    import javax.swing.JTextField;

    import javax.swing.ListSelectionModel;

    import java.awt.GridBagConstraints;

    import java.awt.Color;

    import javax.swing.BorderFactory;

    import javax.swing.table.DefaultTableModel;

    import sun.awt.image.ShortInterleavedRaster;

    import com.sun.istack.internal.NotNull;

    import com.sun.org.apache.xerces.internal.impl.dv.dtd.NOTATIONDatatypeValidator;

    import doa.Singleton;

    import java.awt.SystemColor;

    import java.sql.Connection;

    import java.sql.ResultSet;

    import java.sql.Statement;

    import java.awt.Font;

    import javax.swing.ImageIcon;

    public class TypeArtcle extends JDialog {

    private static final long serialVersionUID = 1L;

    DefaultTableModel md=null;

    private JPanel jContentPane = null;

    private JPanel jPanel = null;

    private JPanel jPanel1 = null;

    private JPanel jPanel2 = null;

    private JLabel jLabel = null;

    private JComboBox jComarticl = null;

    private JPanel jPanel3 = null;

    private JPanel jPanel4 = null;

    private JScrollPane jScrollPane = null;

    private JTable jTableArt = null;

    private JPanel jPanel5 = null;

    private JButton jBajout = null;

    private JButton jBSup = null;

    private JPanel jPanel6 = null;

    private JPanel jPanel7 = null;

    private JPanel jPanel8 = null;

    private JLabel jLabel1 = null;

    private JTextField jTypart = null;

    private JLabel jLabel2 = null;

    private JButton jBenrg = null;

    private JButton jBmodif = null;

    private JLabel jLabel3 = null;

    private JLabel jLabel4 = null;

    private JLabel jLabel5 = null;

    private JLabel jLabel6 = null;

    private JButton jButton = null;

    private Statement pst;  //  @jve:decl-index=0:

    private Connection cn;

    private ResultSet rst;

    private JLabel jLabenouvel = null;

    /**

    * @param owner

    */

    public TypeArtcle(Frame owner) {

    super(owner);

    cn=Singleton.getCon();

    initialize();

    }

    /**

    * This method initializes this

    * @return void

    */

    private void initialize() {

    this.setBounds(new Rectangle(250, 80, 548, 599));

    this.setModal(true);

    this.setContentPane(getJContentPane());

    }

    /**

    * This method initializes jContentPane

    * @return javax.swing.JPanel

    */

    private JPanel getJContentPane() {

    if (jContentPane == null) {

    jContentPane = new JPanel();

    jContentPane.setLayout(null);

    jContentPane.setBackground(new Color(255, 255, 204));

    jContentPane.add(getJPanel(), null);

    jContentPane.add(getJPanel6(), null);

    jContentPane.add(getJButton(), null);

    }

    return jContentPane;

    }

    /**

    * This method initializes jPanel

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel() {

    if (jPanel == null) {

    jLabel6 = new JLabel();

    jLabel6.setBounds(new Rectangle(22, 312, 255, 16));

    jLabel6.setFont(new Font("Dialog", Font.ITALIC, 12));

    jLabel6.setText("ou modifier ou supprimer encore ce dernier");

    jLabel5 = new JLabel();

    jLabel5.setBounds(new Rectangle(25, 296, 409, 16));

    jLabel5.setFont(new Font("Dialog", Font.ITALIC, 12));

    jLabel5.setText("cliquer sur un enregistrement de la liste ci-dessu pour voir les dedail  ");

    jPanel = new JPanel();

    jPanel.setLayout(null);

    jPanel.setBounds(new Rectangle(18, 15, 501, 341));

    jPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue));

    jPanel.setBackground(SystemColor.inactiveCaptionBorder);

    jPanel.add(getJPanel1(), null);

    jPanel.add(getJPanel3(), null);

    jPanel.add(jLabel5, null);

    jPanel.add(jLabel6, null);

    }

    return jPanel;

    }

    /**

    * This method initializes jPanel1

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel1() {

    if (jPanel1 == null) {

    jLabel = new JLabel();

    jLabel.setBounds(new Rectangle(8, 43, 53, 21));

    jLabel.setText("Article");

    jPanel1 = new JPanel();

    jPanel1.setLayout(null);

    jPanel1.setBounds(new Rectangle(8, 12, 480, 80));

    jPanel1.setBackground(Color.white);

    jPanel1.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));

    jPanel1.add(getJPanel2(), null);

    jPanel1.add(jLabel, null);

    jPanel1.add(getJComarticl(), null);

    }

    return jPanel1;

    }

    /**

    * This method initializes jPanel2

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel2() {

    if (jPanel2 == null) {

    jLabel3 = new JLabel();

    jLabel3.setText("CHOISIR UN ARTICLE");

    jLabel3.setBounds(new Rectangle(172, 2, 136, 16));

    jPanel2 = new JPanel();

    jPanel2.setLayout(null);

    jPanel2.setBounds(new Rectangle(2, 0, 478, 20));

    jPanel2.setBackground(new Color(153, 153, 255));

    jPanel2.add(jLabel3, null);

    }

    return jPanel2;

    }

    /**

    * This method initializes jComarticl

    *

    * @return javax.swing.JComboBox

    */

    @SuppressWarnings("unchecked")

    private JComboBox getJComarticl() {

    if (jComarticl == null) {

    jComarticl = new JComboBox();

    jComarticl.setBounds(new Rectangle(85, 42, 258, 25));

    jComarticl.setBackground(new Color(233, 255, 153));

    //appel des articles

    try{

    pst=cn.createStatement();

    rst=pst.executeQuery("SELECT * from articticle ");

    while (rst.next()){

    while (rst.next()){

    //rst.getInt(1);

    jComarticl.addItem(rst.getString("nm_art"));

    }

    }}catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }

    finally{try{cn.createStatement();}catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }

    }

    jComarticl.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

    public void propertyChange(java.beans.PropertyChangeEvent e) {

    if ((e.getPropertyName().equals("selectedIndex"))) {

    try{

    pst=cn.createStatement();

    rst=pst.executeQuery("SELECT   * From type_articlei ");

    //rst=pst.executeQuery("SELECT * From  articticle a1 JOIN type_articlei a2 ON a1.id_art=a2.id_type");

    while (rst.next()){

    //rst.getInt(1);

    md.addRow(new Object[]{

    //rst.getString("id_type"),

    rst.getString("nom_type")});

    }

    }catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }

    finally{try{cn.createStatement();}catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }}

    }

    }

    });

    }

    return jComarticl;

    }

    /**

    * This method initializes jPanel3

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel3() {

    if (jPanel3 == null) {

    jPanel3 = new JPanel();

    jPanel3.setLayout(null);

    jPanel3.setBounds(new Rectangle(14, 106, 476, 184));

    jPanel3.setBackground(Color.white);

    jPanel3.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));

    jPanel3.add(getJPanel4(), null);

    jPanel3.add(getJScrollPane(), null);

    jPanel3.add(getJPanel5(), null);

    }

    return jPanel3;

    }

    /**

    * This method initializes jPanel4

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel4() {

    if (jPanel4 == null) {

    jLabel4 = new JLabel();

    jLabel4.setText("LISTE DES TYPES ARTICLE");

    jLabel4.setBounds(new Rectangle(131, -1, 170, 16));

    jPanel4 = new JPanel();

    jPanel4.setLayout(null);

    jPanel4.setBounds(new Rectangle(1, 2, 473, 19));

    jPanel4.setBackground(new Color(153, 153, 255));

    jPanel4.add(jLabel4, null);

    }

    return jPanel4;

    }

    /**

    * This method initializes jScrollPane

    *

    * @return javax.swing.JScrollPane

    */

    private JScrollPane getJScrollPane() {

    if (jScrollPane == null) {

    jScrollPane = new JScrollPane();

    jScrollPane.setBounds(new Rectangle(2, 21, 284, 162));

    jScrollPane.setViewportView(getJTableArt());

    }

    return jScrollPane;

    }

    /**

    * This method initializes jTableArt

    *

    * @return javax.swing.JTable

    */

    ///requette d'affichage des element type article

    private JTable getJTableArt() {

    if (jTableArt == null) {

    @SuppressWarnings("unused")

    Object[][]DD={};

    @SuppressWarnings("unused")

    final String []EE={"ID_TYPE","Non_Type_Article"};

    md=new DefaultTableModel(DD, EE);

    /*try{

    pst=cn.createStatement();

    //rst=pst.executeQuery("SELECT nom_type,nm_art from articticle JOIN type_articlei ON articticle.id_art =type_articlei.id_type");

    rst=pst.executeQuery("SELECT * FROM type_articlei");

    while (rst.next()){

    int i=0;

    md.addRow(new Object[]{

    rst.getString("id_type"),

    rst.getString("nom_type")});

    }

    }catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }

    finally{try{cn.createStatement();}catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }

    }*/

    jTableArt = new JTable(md);

    jTableArt.setCellSelectionEnabled(true);

    jTableArt.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    jTableArt.setRowHeight(20);

    jTableArt.setRowSelectionAllowed(true);

    jTableArt.setToolTipText("Selectionner l'aricle");

    jTableArt.setColumnSelectionAllowed(false);

    jTableArt.setSelectionBackground(new Color(184, 140, 60));

    jTableArt.setShowGrid(true);

    }

    return jTableArt;

    }

    /**

    * This method initializes jPanel5

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel5() {

    if (jPanel5 == null) {

    jPanel5 = new JPanel();

    jPanel5.setLayout(null);

    jPanel5.setBounds(new Rectangle(312, 29, 155, 113));

    jPanel5.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));

    jPanel5.add(getJBajout(), null);

    jPanel5.add(getJBSup(), null);

    }

    return jPanel5;

    }

    /**

    * This method initializes jBajout

    *

    * @return javax.swing.JButton

    */

    private JButton getJBajout() {

    if (jBajout == null) {

    jBajout = new JButton();

    jBajout.setBounds(new Rectangle(21, 7, 115, 32));

    jBajout.addActionListener(new java.awt.event.ActionListener() {

    @SuppressWarnings("deprecation")

    public void actionPerformed(java.awt.event.ActionEvent e) {

    jLabenouvel.setVisible(true);

    jTypart.setEditable(true);

    jBenrg.setEnabled(true);

    }

    });

    }

    return jBajout;

    }

    /**

    * This method initializes jBSup

    *

    * @return javax.swing.JButton

    */

    private JButton getJBSup() {

    if (jBSup == null) {

    jBSup = new JButton();

    jBSup.setBounds(new Rectangle(20, 62, 121, 30));

    jBSup.addActionListener(new java.awt.event.ActionListener() {

    public void actionPerformed(java.awt.event.ActionEvent e) {

    }

    });

    }

    return jBSup;

    }

    /**

    * This method initializes jPanel6

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel6() {

    if (jPanel6 == null) {

    jLabenouvel = new JLabel();

    jLabenouvel.setBounds(new Rectangle(24, 33, 141, 16));

    jLabenouvel.setText("nouvel enregistrement............");

    jLabenouvel.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 12));

    jLabenouvel.setForeground(new Color(51, 58, 253));

    jLabenouvel.setVisible(false);

    jLabel1 = new JLabel();

    jLabel1.setText("Nom Type Artcle");

    jLabel1.setBounds(new Rectangle(8, 56, 101, 25));

    jPanel6 = new JPanel();

    jPanel6.setLayout(null);

    jPanel6.setBounds(new Rectangle(20, 371, 497, 144));

    jPanel6.setBackground(Color.white);

    jPanel6.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.blue));

    jPanel6.add(getJPanel7(), null);

    jPanel6.add(getJPanel8(), null);

    jPanel6.add(jLabel1, null);

    jPanel6.add(getJTypart(), null);

    jPanel6.add(jLabenouvel, null);

    }

    return jPanel6;

    }

    /**

    * This method initializes jPanel7

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel7() {

    if (jPanel7 == null) {

    jLabel2 = new JLabel();

    jLabel2.setText("FORMULAIRE DE SAISIE DES TYPES ARTCLES");

    jLabel2.setBounds(new Rectangle(97, 4, 272, 17));

    jPanel7 = new JPanel();

    jPanel7.setLayout(null);

    jPanel7.setBounds(new Rectangle(1, 2, 495, 25));

    jPanel7.setBackground(new Color(153, 153, 255));

    jPanel7.add(jLabel2, null);

    }

    return jPanel7;

    }

    /**

    * This method initializes jPanel8

    *

    * @return javax.swing.JPanel

    */

    private JPanel getJPanel8() {

    if (jPanel8 == null) {

    jPanel8 = new JPanel();

    jPanel8.setLayout(null);

    jPanel8.setBounds(new Rectangle(329, 30, 155, 104));

    jPanel8.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));

    jPanel8.add(getJBenrg(), null);

    jPanel8.add(getJBmodif(), null);

    }

    return jPanel8;

    }

    /**

    * This method initializes jTypart

    *

    * @return javax.swing.JTextField

    */

    private JTextField getJTypart() {

    if (jTypart == null) {

    jTypart = new JTextField();

    jTypart.setBounds(new Rectangle(124, 57, 188, 23));

    jTypart.setToolTipText("zone de saisie de nouvel element");

    jTypart.setEditable(false);

    jTypart.setBackground(new Color(233, 255, 153));

    }

    return jTypart;

    }

    /**

    * This method initializes jBenrg

    *

    * @return javax.swing.JButton

    */

    private JButton getJBenrg() {

    if (jBenrg == null) {

    jBenrg = new JButton();

    jBenrg.setBounds(new Rectangle(13, 11, 134, 35));

    jBenrg.setIcon(new ImageIcon(getClass().getResource("/boutons/Bouton enregistrer.png")));

    jBenrg.setEnabled(false);

    jBenrg.addActionListener(new java.awt.event.ActionListener() {

    public void actionPerformed(java.awt.event.ActionEvent e) {

    if(jTypart.getText().trim().length()>0){

    @SuppressWarnings("unused")

    int selectedIndex = jComarticl.getSelectedIndex();

       @SuppressWarnings("unused")

    Object selectedItem = jComarticl.getSelectedItem();

    try{

    pst=cn.createStatement();

    //pst=cn.createStatement();

    String req="INSERT INTO type_articlei VALUES('"+DO_NOTHING_ON_CLOSE +"','"+jComarticl.getSelectedIndex()+"','"+jTypart.getText()+"')";

    pst.executeUpdate(req);

    jTypart.setText("");

    jBenrg.setEnabled(false);

    jLabenouvel.setVisible(false);

    jTypart.setEditable(false);

    }catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }

    finally{try{cn.createStatement();}catch(Exception e1){e1.printStackTrace();

    JOptionPane.showMessageDialog(null,"Erreur de connexion\n"+e1.getMessage());

    }

    }

    }else JOptionPane.showMessageDialog(null, "Veillez saisir dans la zone de texte\n pour  enregistrer", "Message Box", 0);}

    });

    }

    return jBenrg;

    }

    /**

    * This method initializes jBmodif

    *

    * @return javax.swing.JButton

    */

    private JButton getJBmodif() {

    if (jBmodif == null) {

    jBmodif = new JButton();

    jBmodif.setBounds(new Rectangle(18, 56, 125, 36));

    }

    return jBmodif;

    }

    /**

    * This method initializes jButton

    *

    * @return javax.swing.JButton

    */

    private JButton getJButton() {

    if (jButton == null) {

    jButton = new JButton();

    jButton.setBounds(new Rectangle(402, 522, 110, 32));

    jButton.setActionCommand("dispose");

    jButton.addActionListener(new java.awt.event.ActionListener() {

    public void actionPerformed(java.awt.event.ActionEvent e) {

    int option = JOptionPane.showConfirmDialog(null,

    "Voulez-vous Vraiment fermer ?",

    "confirmation de dialogue",

    JOptionPane.YES_NO_OPTION,

    JOptionPane.QUESTION_MESSAGE);

    if(option == JOptionPane.OK_OPTION){System.exit(0);}

    }

    });

    }

    return jButton;

    }

    }  //  @jve:decl-index=0:visual-constraint="50,-5"

    -
    Edité par camara_dif 17 mai 2016 à 22:49:51

    • Partager sur Facebook
    • Partager sur Twitter
      28 avril 2016 à 10:45:43

      Je déplace dans la section Java
      • Partager sur Facebook
      • Partager sur Twitter

      Je ne suis plus modérateur, ne me contactez plus pour des demandes, je n'y répondrai pas.

        28 avril 2016 à 11:04:17

        Salut,

        Recuperer une vaelur d'un jtextField : jtextfield.gettext();

        combobox : combobox.getSelectedItem (ou dans le genre... regarde la doc !)

        Pour l'enregistrement dans une bdd, tout dépend de la techno utilisée... Mais tu trouvera facilement des tuto !

        • Partager sur Facebook
        • Partager sur Twitter

        jointure sql entre jcombobox et jtable

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown