private void fillTextField(String value) { try{ String s = "select libelle from EtatMateriel where RefEtat='" + value + "'"; Statement st=Accueil.con.createStatement(); ResultSet rst=st.executeQuery(s); if(rst.next()){ getT1().setText(rst.getString("libelle")); } else System.err.println("No first value found :/"); } catch (SQLException e) { e.printStackTrace(); } }
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Test extends JFrame implements ActionListener { private JComboBox comboBox = null; private JTextField textField = null; public static void main(final String[] args) { new Test().setVisible(true); } public Test() { final JPanel panel = new JPanel(); panel.add(getComboBox()); panel.add(getTextField()); setContentPane(panel); getComboBox().setSelectedIndex(0);// pas oblige setDefaultCloseOperation(3); pack(); setLocationRelativeTo(null); } private JComboBox getComboBox() { if (comboBox == null) { comboBox = new JComboBox(); final DefaultComboBoxModel model = new DefaultComboBoxModel(); for (int i = 1; i < 11; ++i) model.addElement("Item " + i); comboBox.setModel(model); comboBox.setPreferredSize(new Dimension(150, 20)); comboBox.addActionListener(this); } return comboBox; } private JTextField getTextField() { if (textField == null) { textField = new JTextField(); textField.setPreferredSize(new Dimension(150, 20)); } return textField; } private void fillTextField(String value) { // cas 1 getTextField().setText(value); // cas 2 //recherche dans la db de la donnée que tu veux //...... } public void actionPerformed(final ActionEvent e) { final Object o = e.getSource(); if (o.equals(getComboBox())) { fillTextField("" + getComboBox().getSelectedItem()); } } }
import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.Dimension; import javax.swing.JDesktopPane; import java.awt.Color; import java.awt.SystemColor; import javax.swing.JComboBox; import java.awt.Rectangle; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; import javax.swing.JTextPane; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JProgressBar; import javax.swing.table.DefaultTableModel; public class ModifierMat extends JFrame { private static final long serialVersionUID = 1L; private ResultSet rs; private ResultSetMetaData rm; private DefaultTableModel model; private JPanel jContentPane = null; private static Statement st; // @jve:decl-index=0: private static JFrame jFrame; private JDesktopPane jDesktopPane = null; private static JComboBox jc = null; private JLabel NumSerie_l = null; private JLabel DateInstallation_l = null; private JLabel Marque_l = null; private JLabel Etat_l = null; private JLabel Type_l = null; private static JTextField DateInstallation_t = null; private JButton quitter = null; private JButton Modifier = null; private JLabel Titre = null; private static JComboBox jc2 = null; private static JComboBox jc1 = null; private static JComboBox jc3 = null; private JTextField t1 = null; private JTextField t2 = null; private JTextField t3 = null; /** * This is the default constructor */ public ModifierMat() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(751, 486); this.setContentPane(getJContentPane()); this.setTitle("Modifier Materiel"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getJDesktopPane(), BorderLayout.CENTER); } return jContentPane; } /** * This method initializes jDesktopPane * * @return javax.swing.JDesktopPane */ private JDesktopPane getJDesktopPane() { if (jDesktopPane == null) { Titre = new JLabel(); Titre.setBounds(new Rectangle(122, 40, 301, 47)); Titre.setFont(new Font("Footlight MT Light", Font.BOLD, 30)); Titre.setForeground(Color.white); Titre.setText(" Modifier Materiel"); Type_l = new JLabel(); Type_l.setBounds(new Rectangle(29, 271, 172, 36)); Type_l.setFont(new Font("Comic Sans MS", Font.BOLD, 18)); Type_l.setForeground(SystemColor.controlHighlight); Type_l.setText(" Type Du Materiel"); Marque_l = new JLabel(); Marque_l.setBounds(new Rectangle(30, 315, 199, 34)); Marque_l.setFont(new Font("Comic Sans MS", Font.BOLD, 18)); Marque_l.setForeground(SystemColor.controlHighlight); Marque_l.setText(" Marque Du Materiel"); Etat_l = new JLabel(); Etat_l.setBounds(new Rectangle(30, 223, 173, 36)); Etat_l.setFont(new Font("Comic Sans MS", Font.BOLD, 18)); Etat_l.setForeground(SystemColor.controlHighlight); Etat_l.setText(" Etat Du Materiel "); DateInstallation_l = new JLabel(); DateInstallation_l.setBounds(new Rectangle(30, 167, 173, 30)); DateInstallation_l.setFont(new Font("Comic Sans MS", Font.BOLD, 18)); DateInstallation_l.setForeground(SystemColor.controlHighlight); DateInstallation_l.setText(" Date d'Installation"); NumSerie_l = new JLabel(); NumSerie_l.setBounds(new Rectangle(30, 121, 175, 30)); NumSerie_l.setFont(new Font("Comic Sans MS", Font.BOLD, 18)); NumSerie_l.setForeground(SystemColor.controlHighlight); NumSerie_l.setText(" Numéro De Serie"); jDesktopPane = new JDesktopPane(); jDesktopPane.setForeground(SystemColor.controlHighlight); jDesktopPane.setBackground(SystemColor.activeCaption); jDesktopPane.setFont(new Font("Comic Sans MS", Font.BOLD, 18)); jDesktopPane.add(getJc(), null); jDesktopPane.add(NumSerie_l, null); jDesktopPane.add(DateInstallation_l, null); jDesktopPane.add(Etat_l, null); jDesktopPane.add(Marque_l, null); jDesktopPane.add(Type_l, null); jDesktopPane.add(getDateInstallation_t(), null); jDesktopPane.add(getQuitter(), null); jDesktopPane.add(getModifier(), null); jDesktopPane.add(Titre, null); jDesktopPane.add(getJc2(), null); jDesktopPane.add(getJc1(), null); jDesktopPane.add(getJc3(), null); jDesktopPane.add(getT1(), null); jDesktopPane.add(getT2(), null); jDesktopPane.add(getT3(), null); } return jDesktopPane; } /* ------ Method de remplissage du jComboBox NumSerie -----*/ public void remplir_JCombo(){ try { String s="select NumSerie from Materiel"; Statement st=Accueil.con.createStatement(); ResultSet rst=st.executeQuery(s); while(rst.next()){ jc.addItem( rst.getObject(1)); } } catch (SQLException e) { // TODO Bloc catch auto-généré e.printStackTrace(); } } /* ------ Method de remplissage du jComboBox Etat -------*/ public void remplir_JComboEtat(){ try { String s="select RefEtat from EtatMateriel"; Statement st=Accueil.con.createStatement(); ResultSet rst=st.executeQuery(s); while(rst.next()){ jc1.addItem( rst.getObject(1)); } } catch (SQLException e) { // TODO Bloc catch auto-généré e.printStackTrace(); } } /*---- Method de remplissage du jComboBox Type ----*/ public void remplir_JComboType(){ try { String s="select RefType from TypeMateriel"; Statement st=Accueil.con.createStatement(); ResultSet rst=st.executeQuery(s); while(rst.next()){ jc2.addItem( rst.getObject(1)); } } catch (SQLException e) { // TODO Bloc catch auto-généré e.printStackTrace(); } } /*---- Method de remplissage du jComboBox Marque ----*/ public void remplir_JComboMarque(){ try { String s="select RefMarque from Marque"; Statement st=Accueil.con.createStatement(); ResultSet rst=st.executeQuery(s); while(rst.next()){ jc3.addItem( rst.getObject(1)); } } catch (SQLException e) { // TODO Bloc catch auto-généré e.printStackTrace(); } } /*------ Methode de Remplissage JTextField t1 ------*/ public static void remplir_t1(){ try { if(jc1.getSelectedItem().toString()!=null){ String s="select libelle from EtatMateriel where RefEtat='"+(String)jc1.getSelectedItem()+"'"; Statement st=Accueil.con.createStatement(); ResultSet rst=st.executeQuery(s); } } catch (SQLException e) { e.printStackTrace(); } } /*------Remplir ComboBox Numeros de series du Materiel -----*/ private JComboBox getJc() { if (jc == null) { jc = new JComboBox(); jc.setBounds(new Rectangle(288, 120, 235, 28)); remplir_JCombo(); add(jc); } return jc; } /** * This method initializes Nom_t * * @return javax.swing.JTextField */ private JTextField getDateInstallation_t() { if (DateInstallation_t == null) { DateInstallation_t = new JTextField(); DateInstallation_t.setBounds(new Rectangle(288, 171, 236, 28)); } return DateInstallation_t; } /*------------- Modifier Materiel -----------*/ public static void ModifMat(){ try { st=Accueil.con.createStatement(); st.executeUpdate("update Materiel set DateInstallation='"+DateInstallation_t.getText()+"' ,RefEtat='"+jc1.getSelectedItem()+"' ,RefType='"+ jc2.getSelectedItem()+"' ,RefMarque='"+jc3.getSelectedItem()+"' where NumSerie='"+jc.getSelectedItem().toString()+"'"); JOptionPane.showMessageDialog(jFrame, "Modification avec succés","Confirmation", JOptionPane.INFORMATION_MESSAGE, null); st.close(); } catch (SQLException e) { e.printStackTrace(); } } /** * This method initializes Modifier * * @return javax.swing.JButton */ public static void mouseClicked(MouseEvent event) { System.out.println("mouse clicked"); } private JButton getModifier() { if (Modifier == null) { Modifier = new JButton(); Modifier.setBounds(new Rectangle(396, 378, 97, 28)); Modifier.setForeground(SystemColor.activeCaption); Modifier.setFont(new Font("Dialog", Font.BOLD, 14)); Modifier.setText("Modifier"); Modifier.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { ModifMat(); } }); } return Modifier; } /** * This method initializes jc2 * * @return javax.swing.JComboBox */ private JComboBox getJc2() { if (jc2 == null) { jc2 = new JComboBox(); jc2.setBounds(new Rectangle(287, 269, 100, 28)); remplir_JComboType(); add(jc2); } return jc2; } /** * This method initializes jc1 * * @return javax.swing.JComboBox */ private JComboBox getJc1() { if (jc1 == null) { jc1 = new JComboBox(); jc1.setBounds(new Rectangle(287, 222, 99, 29)); remplir_JComboEtat(); add(jc1); } return jc1; } /** * This method initializes jc3 * * @return javax.swing.JComboBox */ private JComboBox getJc3() { if (jc3 == null) { jc3 = new JComboBox(); jc3.setBounds(new Rectangle(287, 316, 98, 27)); remplir_JComboMarque(); add(jc3); } return jc3; } /** * This method initializes t1 * * @return javax.swing.JTextField */ private JTextField getT1() { if (t1 == null) { t1 = new JTextField(); t1.setBounds(new Rectangle(388, 224, 136, 26)); t1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { remplir_t1(); add(t1); } }); } return t1; } private void fillTextField(String value) { getT1().setText(value); } public void actionPerformed(final ActionEvent e) { final Object o = e.getSource(); if (o.equals(getJc1())) { fillTextField("" + getJc1().getSelectedItem()); } } /** * This method initializes t2 * * @return javax.swing.JTextField */ private JTextField getT2() { if (t2 == null) { t2 = new JTextField(); t2.setBounds(new Rectangle(388, 270, 136, 27)); } return t2; } /** * This method initializes t3 * * @return javax.swing.JTextField */ private JTextField getT3() { if (t3 == null) { t3 = new JTextField(); t3.setBounds(new Rectangle(386, 315, 138, 27)); } return t3; } /** * This method initializes quitter * * @return javax.swing.JButton */ private JButton getQuitter() { if (quitter == null) { quitter = new JButton(); quitter.setBounds(new Rectangle(493, 378, 88, 28)); quitter.setForeground(SystemColor.activeCaption); quitter.setFont(new Font("Dialog", Font.BOLD, 14)); quitter.setText("Quitter"); quitter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { mouseClicked(null); dispose(); } }); } return quitter; } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprivate void fillTextField(String value) { String s = "select libelle from EtatMateriel where RefEtat='" + value + "'"; Statement st=Accueil.con.createStatement(); ResultSet rst=st.executeQuery(s); // récupération du résultat et affectation de ce dernier au champ texte getT1().setText(le résultat de ta requete); }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * combo.java * * Created on 21 mai 2012, 22:58:20 */ /** * * @author Microbox */ /*--------------------------------------------------------------------------------------------------------------*/ public class combo extends JFrame implements ActionListener { /** * */ JLabel recup_item; /** Creates new form combo */ public combo() { initComponents(); this.setLocationRelativeTo(null); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox(); btn_ok = new javax.swing.JButton(); btn_ok.setText("ok"); /*--------------------------------------------------------------------------------------------------------------*/ btn_ok.addActionListener(new label(this)); /*--------------------------------------------------------------------------------------------------------------*/ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "rouge", "vert", "bleu" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { /*--------------------------------------------------------------------------------------------------------------*/ public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); /*--------------------------------------------------------------------------------------------------------------*/ btn_ok.setText("ok"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(89, 89, 89) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(btn_ok) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(63, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(71, 71, 71) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE) .addComponent(btn_ok) .addGap(51, 51, 51)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); pack(); }// </editor-fold> private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { //public void itemStateChanged(ItemEvent ie) { recup_item.setText(jComboBox1.getSelectedItem().toString()); //} // TODO add your handling code here: } /*--------------------------------------------------------------------------------------------------------------*/ private void btn_okActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //new label().setVisible(true); } /*--------------------------------------------------------------------------------------------------------------*/ /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new combo().setVisible(true); } }); } // Variables declaration - do not modify public javax.swing.JButton btn_ok; public javax.swing.JComboBox jComboBox1; private javax.swing.JPanel jPanel1; // End of variables declaration /*--------------------------------------------------------------------------------------------------------------*/ public JComboBox getjComboBox1(){ //JLabel recup_item; return jComboBox1; } /*--------------------------------------------------------------------------------------------------------------*/ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import javax.swing.JComboBox; import javax.swing.JLabel; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * label.java * * Created on 21 mai 2012, 23:02:10 */ /** * * @author Microbox */ /*--------------------------------------------------------------------------------------------------------------*/ public class label extends javax.swing.JFrame implements ActionListener { private combo combo1; public label(combo combo1) { this.combo1=combo1; } /** Creates new form label */ public label() { initComponents(); this.setLocationRelativeTo(null); //pour ne pas fermer la fenetre précedente aprés close de label// setDefaultCloseOperation(this.DISPOSE_ON_CLOSE); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { //public void actionPerformed1(ActionEvent arg0) { jPanel1 = new javax.swing.JPanel(); couleur = new javax.swing.JLabel(); recup_item = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); couleur.setText("couleur choisi"); recup_item.setForeground(new java.awt.Color(255, 51, 0)); recup_item.setOpaque(true); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(28, 28, 28) .addComponent(couleur, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(30, 30, 30) .addComponent(recup_item, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(45, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(64, 64, 64) .addComponent(recup_item, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(77, 77, 77) .addComponent(couleur))) .addContainerGap(151, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new label().setVisible(true); } }); } // Variables declaration - do not modify public javax.swing.JLabel couleur; private javax.swing.JPanel jPanel1; public javax.swing.JLabel recup_item; // End of variables declaration @Override /*--------------------------------------------------------------------------------------------------------------*/ public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub //recup_item.setText(combo1.jComboBox1.getSelectedItem().toString()); String jComboBox1=combo1.getjComboBox1().getSelectedItem().toString(); // recup_item.setText("jComboBox1"); System.out.println(jComboBox1); new label().setVisible(true); } /*--------------------------------------------------------------------------------------------------------------*/ }