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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| package GestionTablett;
import static GestionTablett.Connecter.getCon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class TestCombo extends javax.swing.JFrame {
PreparedStatement pst;
ResultSet rs;
ResultSet rst;
public TestCombo() throws SQLException {
initComponents();
jComboBox1.addItem("----");
String query1 = "SELECT DISTINCT nomregion FROM region";
pst=getCon().prepareStatement(query1);
ResultSet rst = pst.executeQuery();
while(rst.next()) {
jComboBox1.addItem(rst.getString("nomregion"));
}
rst.close();
jComboBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String comboBox1Selected=jComboBox1.getSelectedItem().toString();
try{
String query2 = "SELECT nomdepartement FROM departement, region where iddepartement = ?";
pst=getCon().prepareStatement(query2);
pst.setString(1, comboBox1Selected );
ResultSet rs = pst.executeQuery();
// jComboBox2.removeAllItems();
while(rs.next()) {
jComboBox2.addItem(rs.getString("nomdepartement"));}
}
catch (SQLException ex1) {JOptionPane.showMessageDialog(null,"Erreur"+ex1.getLocalizedMessage()); return;}
}
});
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox<>();
jComboBox2 = new javax.swing.JComboBox<>();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(102, 102, 102)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jComboBox2, 0, 195, Short.MAX_VALUE))
.addContainerGap(173, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(55, 55, 55)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(55, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TestCombo().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(TestCombo.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JComboBox<String> jComboBox2;
// End of variables declaration
} |
Partager