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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
| public class Banque extends JFrame {
private JPanel contentPane;
private static JTable table;
private JScrollPane scroll;
public static void main(String[] args) {
Banque frame4 = new Banque();
frame4.setVisible(true);
ArrayList<MyParameters> list= Banque.getParameters(); //tu récupères la liste de tes parametre
//fait appel à cette méthode là ou tu veux
}
public Banque() {
// Paramètres de la fenêtre
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Fermeture lors de du clic sur la croix
setBounds(100, 100, 450, 650); // Dimension de la fenêtre
setContentPane(contentPane);
setResizable(false); // Blocage du redimensionement manuel de la fenêtre
setLocationRelativeTo(null); // Placer la fenêtre au centre de l'écran
// Déclaration des images
ImageIcon icon= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Chemise-Marron-Bureau-Carreaux.jpg");
ImageIcon icon2= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Chemise-Blanche-Classe-Simple.jpg");
ImageIcon icon3= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Chemise-Gris-Détendu-Carreaux.jpg");
ImageIcon icon4= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/T-Shirt-Rouge-Sport-Imprimé1.jpg");
ImageIcon icon5= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Chemise-Bleu-Bureau-Simple.jpg");
ImageIcon icon6= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Chemise-Noire-Classe-Simple.jpg");
ImageIcon icon7= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Pantalon-Noir-Sport-Simple.jpg");
ImageIcon icon8= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Veste-Bleu-Bureau-Simple.jpg");
ImageIcon icon9= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Pantalon-Beige-Bureau-Simple.jpg");
ImageIcon icon10= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Pantalon-Noir-Classe-Simple.jpg");
ImageIcon icon11= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Veste-Noir-Classe-Simple.jpg");
ImageIcon icon12= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Veste-Noir-Sport-Simple.jpg");
ImageIcon icon13= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Pantalon-Bleu-Détendu-Simple.jpg");
ImageIcon icon14= new ImageIcon("C:/Users/Lucas/Documents/Cours/BrainyDress/src/images/Pull-Gris-Détendu-Imprimé.jpg");
// Déclaration du tableau
Object[] [] data= new Object[][] {
{"Habit 1", "Haut", icon, "Marron","Carreaux", "Bureau", "Modérée"},
{"Habit 2", "Haut", icon2, "Blanc","Simple", "Classe","Modérée"},
{"Habit 3", "Haut", icon3, "Marron","Carreaux", "Détendu", "Fraîche"},
{"Habit 4", "Haut", icon4, "Rouge","Imprimé", "Sport"},
{"Habit 5", "Haut", icon5, "Bleu","Simple", "Bureau", "Modérée"},
{"Habit 6", "Haut", icon6, "Noir","Simple", "Classe","Modérée"},
{"Habit 7", "Bas", icon7, "Noir","Simple", "Sport"},
{"Habit 8", "Veste", icon8, "Bleu","Simple", "Bureau", "Fraîche"},
{"Habit 9", "Bas", icon9, "Beige","Simple", "Bureau", "Modérée"},
{"Habit 10", "Bas", icon10, "Noir","Simple", "Classe", "Modérée"},
{"Habit 11", "Veste", icon11, "Noir","Simple", "Classe", "Modérée"},
{"Habit 12", "Veste", icon12, "Noir","Simple", "Sport"},
{"Habit 13", "Bas", icon13, "Bleu","Simple", "Détendu","Fraîche"},
{"Habit 14", "Veste", icon14, "Gris","Imprimé", "Détendu", "Fraîche"},
};
String[] title= new String[] {"Nom", "Type", "Image", "Couleur", "Motif", "Style", "T°"};
//Propriétés du tableau
final DefaultTableModel model= new DefaultTableModel (data, title);
table= new JTable (model);
table.getColumn("Image").setCellRenderer(new IconRenderer ()); // Affichage des images dans la colonne "Image"
table.setRowHeight (100); // Taille des lignes
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scroll= new JScrollPane(table); // Curseur de défilement de page
scroll.setBounds(7, 100, 430, 417); // Taille du tableau
this.getContentPane().add(scroll);
table.getColumn("Nom").setCellRenderer(new CenterTableRenderer()); // Permet
table.getColumn("Couleur").setCellRenderer(new CenterTableRenderer()); // de
table.getColumn("Style").setCellRenderer(new CenterTableRenderer()); // centrer
table.getColumn("Motif").setCellRenderer(new CenterTableRenderer()); // le contenu
table.getColumn("Type").setCellRenderer(new CenterTableRenderer()); // de chaques
table.getColumn("T°").setCellRenderer(new CenterTableRenderer()); // colonnes
table.getTableHeader().setReorderingAllowed(false); // Pour empêcher de pouvoir déplacer les colonnes
table.getTableHeader().setResizingAllowed(false); // Pour empêcher de pouvoir modifier la taille des colonnes
TableColumn column = null; // Dimension de chaque colonnes
for (int i = 0; i <= 6; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 0) {column.setPreferredWidth(50);} // Colonne 0 : "Nom"
else {
if (i == 1) {column.setPreferredWidth(40);} // Colonne 1 : "Type"
else {
if (i == 2) {column.setPreferredWidth(95);} // Colonne 2 : "Image"
else {
if (i == 3) {column.setPreferredWidth(62);} // Colonne 3 : "Couleur"
else {
if (i == 4) {column.setPreferredWidth(58);} // Colonne 4 : "Motif"
else {
if (i == 5) {column.setPreferredWidth(52);} // Colonne 5 : "Style"
else {
if (i == 6) {column.setPreferredWidth(55);} // Colonne 6 : "T°"
}
}
}
}
}
}
}
String[] values1= {"Veste", "Haut", "Bas"};
JComboBox box1= new JComboBox(values1);
TableColumn col1 = table.getColumnModel().getColumn(1);
col1.setCellEditor(new MyComboBoxEditor(box1));
String[] values3= {"Beige", "Blanc", "Bleu", "Gris", "Jaune", "Marron", "Noir", "Rouge", "Vert"};
JComboBox box3= new JComboBox(values3);
TableColumn col3 = table.getColumnModel().getColumn(3);
col3.setCellEditor(new MyComboBoxEditor(box3));
String[] values4= {"Carreaux", "Imprimé", "Simple"};
JComboBox box4= new JComboBox(values4);
TableColumn col4 = table.getColumnModel().getColumn(4);
col4.setCellEditor(new MyComboBoxEditor(box4));
String[] values5= {"Bureau", "Classe", "Détendu", "Sport"};
JComboBox box5= new JComboBox(values5);
TableColumn col5 = table.getColumnModel().getColumn(5);
col5.setCellEditor(new MyComboBoxEditor(box5));
String[] values6= {"Fraiche", "Modérée", "Chaude"};
JComboBox box6= new JComboBox(values6);
TableColumn col6 = table.getColumnModel().getColumn(6);
col6.setCellEditor(new MyComboBoxEditor(box6));
//Création du bouton retour
JButton retour = new JButton("Retour");
retour.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 12));
retour.setBounds(0, 0, 105, 35);
contentPane.add(retour);
// Fonction ajouter un vêtement
JButton Ajouter = new JButton("Ajouter un vêtement");
Ajouter.setBounds(10, 555, 190, 50);
Ajouter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Object[] rowData= new Object[6];
model.addRow(rowData);
}
});
contentPane.add(Ajouter);
// Fonction Supprimer un vêtement
JButton Supprimer = new JButton("Supprimer un vêtement");
Supprimer.setBounds(245, 555, 190, 50);
Supprimer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.removeRow(table.getSelectedRow());
}
});
contentPane.add(Supprimer);
// Titre
JLabel lblModifierLaBanque = new JLabel("Modifier la banque d'habits");
lblModifierLaBanque.setFont(new Font("Monotype Corsiva", Font.PLAIN, 43));
lblModifierLaBanque.setBounds(7, 34, 422, 55);
contentPane.add(lblModifierLaBanque);
//Création du fond d'écran
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setFont(new Font("Bradley Hand ITC", Font.PLAIN, 38));
lblNewLabel.setBackground(new Color(135, 206, 250));
lblNewLabel.setForeground(new Color(0, 191, 255));
lblNewLabel.setIcon(new ImageIcon(BrainyDressv1.class.getResource("/images/wallpaper.jpg")));
lblNewLabel.setBounds(0, 0, 559, 622);
contentPane.add(lblNewLabel);
}
class IconRenderer extends DefaultTableCellRenderer { // Création de la classe pour pouvoir afficher les photos
public IconRenderer() {
super();
}
protected void setValue(Object value) {
if(value == null)
setText("");
else
setIcon((Icon) value);
}
}
class CenterTableRenderer extends DefaultTableCellRenderer { // Création de la classe pour centrer le contenu des cellules
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
this.setHorizontalAlignment(JLabel.CENTER);
return this;
}
}
class MyComboBoxEditor extends DefaultCellEditor {
protected JComboBox box1;
protected JComboBox box3;
protected JComboBox box4;
private MyComboBoxListener listen= new MyComboBoxListener();
public MyComboBoxEditor(JComboBox jComboBox) {
super(jComboBox);
box1= jComboBox;
box3= jComboBox;
box4= jComboBox;
box1.addActionListener(listen);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
listen.setRow(row);
listen.setTable(table);
box1.setSelectedItem(value.toString());
return box1;
}
class MyComboBoxListener implements ActionListener {
private int row;
private JTable table;
public void setRow(int row_) {this.row= row_;} //permet de récupérer le numéro de ligne ou cas ou on en a besoin
public void setTable(JTable table_) {this.table= table_;} //permet de récupérer la table au cas on en a besoin
public void actionPerformed(ActionEvent e) {
//juste si par exemple tu as envie de faire une action au moment ou tu sélectionnes l'item
//sinon tu enlèves le listener du JComboBox
}
}
}
public static ArrayList<MyParameters> getParameters() {
ArrayList<MyParameters> params= new ArrayList<MyParameters>();
for(int i=0; i<table.getRowCount(); i++) {
String num= (String) table.getValueAt(i, 0);
String couleur= (String) table.getValueAt(i, 3);
String motif= (String) table.getValueAt(i, 4);
String type= (String) table.getValueAt(i, 1);
String style= (String) table.getValueAt(i, 5);
String temperature= (String) table.getValueAt(i, 6);
MyParameters param= new MyParameters(num, couleur, motif, type, style, temperature);
params.add(param); //enregistrer le tout dans la liste params
System.out.println(param.toString());
}
return params;
}
} |