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
| public class TableHF extends JFrame implements ActionListener {
JButton btngreen;
JButton valid = new JButton("Confirmer");
ModeleStatique model = new ModeleStatique();
JTable table;
boolean test;
public static void main(String[] args) {
TableHF thf = new TableHF();
}
public TableHF() {
//setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panelHaut = new JPanel();
valid.addActionListener(this);
panelHaut.add(btnCreate());
panelHaut.add(valid);
table = new JTable(model);
table.setDefaultRenderer(String.class, new ColorCellRenderer());
add(panelHaut);
add(new JScrollPane(table),BorderLayout.CENTER);
add(panelHaut,BorderLayout.NORTH);
pack();
setVisible(true);
}
public JPanel btnCreate () {
JPanel panel = new JPanel();
btngreen = new JButton("Activé");
panel.add(btngreen);
btngreen.addActionListener(this);
return panel;
}
public void actionPerformed(ActionEvent e) {
btngreen.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
btngreen.setBackground(Color.green);
//JE VEUX QUE QUAND ON CLICK SUR LA CASE L'ETAT CHANGE EN TRUE ET DU COUP COLOR EN VERT
if(!e.getSource().equals(test)) {
e.getSource().equals(test=true);
}
table.repaint();
}
});
}
public class ModeleStatique extends AbstractTableModel {
private final Planning[] data;
private final String[] title = {null, "8h-9h", "9h-10h", "10h-11h"};
public ModeleStatique() {
super();
data = new Planning[]{
new Planning("Lundi", false, false, false),
new Planning("Mardi", false, false, false),
new Planning("Mercredi", false, false, false),
new Planning("Jeudi", false, false, false),
new Planning("Vendredi",false, false, false),
new Planning("Samedi", false, false, false),
new Planning("Dimanche", false, false, false),
};
}
public int getRowCount() {
return data.length;
}
public int getColumnCount() {
return title.length;
}
public String getColumnName(int columnIndex) {
return title[columnIndex];
}
public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex){
case 0:
return data[rowIndex].getJours();
default:
return null;
}
}
public Class getColumnClass(int columnIndex){
return String.class;
}
}
public class Planning {
private String jours;
private boolean huit;
private boolean neuf;
private boolean dix;
public Planning(String jours, boolean huit, boolean neuf, boolean dix) {
this.jours=jours;
this.huit = huit;
this.neuf=neuf;
this.dix=dix;
}
public String getJours() {
return jours;
}
public void setHuit(boolean huit) {
this.huit = huit;
}
public boolean isHuit() {
return huit;
}
public boolean isNeuf() {
return neuf;
}
public void setNeuf(boolean neuf) {
this.neuf = neuf;
}
public boolean isDix() {
return dix;
}
public void setDix(boolean dix) {
this.dix = dix;
}
public void setJours(String jours) {
this.jours = jours;
}
}
public class ColorCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (test=true && model.data[row].isHuit() || model.data[row].isNeuf() || model.data[row].isDix())
setBackground(Color.green);
else if (test=false && !model.data[row].isHuit() || !model.data[row].isNeuf()|| model.data[row].isDix())
setBackground(Color.white);
return this;
}
}
} |
Partager