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 103 104 105 106 107 108
|
public FenetreTest() {
this.setTitle("AppShopXwork2-B");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 300);
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/mabase","root","");
statement = connection.createStatement();
result = statement.executeQuery("SELECT types_pdt FROM Cat_boissons");
while(result.next())
{
dlm.addElement(result.getString("types_pdt"));
}
list.setModel(dlm);
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel pan5 = new JPanel();
pan5.setBackground(Color.BLUE);
pan5.add(tab);
JPanel pan6 = new JPanel();
pan6.setBackground(Color.cyan);
JPanel pan7 = new JPanel();
pan7.setBackground(Color.GREEN);
pan7.add(lab);
pan7.add(list);
split2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pan7, pan5);
split1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, split2, pan6);
this.getContentPane().add(split1, BorderLayout.CENTER);
}catch (Exception e) {
e.printStackTrace();
}
list.addListSelectionListener(new AccesInfosPdt());
this.setVisible(true);
}
class AccesInfosPdt implements ListSelectionListener{
public void valueChanged(ListSelectionEvent arg0) {
if (!arg0.getValueIsAdjusting()) {
String pdt = (String) list.getSelectedValue();
System.out.println("Choix : " +pdt);
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/mabase","root","");
statement = connection.createStatement();
result = statement.executeQuery("select Shops, Prix from "+ pdt +" ");
String columns[] = {"Shops","Prix"};
String data[][] = new String[4][2];
int i = 0;
while(result.next()) {
String shop = result.getString("Shops");
int prix = result.getInt("Prix");
data[i][0]= shop;
data[i][1] = prix + "";
i++;
System.out.println(" dans la boutique " +shop+ "au prix de : " +prix);
}
DefaultTableModel model = new DefaultTableModel(data, columns);
JTable tab = new JTable(model);
tab.setShowGrid(false);
tab.setRowHeight(30);
tab.setTableHeader(null);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
} |
Partager