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
| public class DemoTableCellSelectionPlus {
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Set<Cell> persoselection = new HashSet<>(); // un set qui représente la sélection "plus", qui stocke donc les cellulles sélectionnées
JTable table = createTable();
JButton buttonSelection = new JButton("Sélectionner");
buttonSelection.setEnabled(false);
JButton buttonDeselection = new JButton("Désélectionner");
buttonDeselection.setEnabled(false);
table.getSelectionModel().addListSelectionListener(e-> {
if ( table.getSelectedColumn()!=-1 && table.getSelectedRow()!=-1 ) {
Cell cell = new Cell(table.getSelectedColumn(), table.getSelectedRow());
enableButtons(buttonSelection, buttonDeselection, table, persoselection, cell);
}
else {
buttonSelection.setEnabled(false);
buttonDeselection.setEnabled(false);
}
});
table.getColumnModel().addColumnModelListener(new TableColumnModelListener() {
@Override
public void columnSelectionChanged(ListSelectionEvent e) {
if ( table.getSelectedColumn()!=-1 && table.getSelectedRow()!=-1 ) {
Cell cell = new Cell(table.getSelectedColumn(), table.getSelectedRow());
enableButtons(buttonSelection, buttonDeselection, table, persoselection, cell);
}
else {
buttonSelection.setEnabled(false);
buttonDeselection.setEnabled(false);
}
}
@Override
public void columnRemoved(TableColumnModelEvent e) {
// si on peut supprimer et ajouter des colonnes, il faudra gérer l'enablement des boutons;
}
@Override
public void columnMoved(TableColumnModelEvent e) {
}
@Override
public void columnMarginChanged(ChangeEvent e) {
}
@Override
public void columnAdded(TableColumnModelEvent e) {
// si on peut supprimer et ajouter des colonnes, il faudra gérer l'enablement des boutons;
}
});
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if ( persoselection.contains(new Cell(column,row)) ) { // signifie que la cellule est sélectionnée
if ( isSelected || hasFocus ) {
isSelected = false;
hasFocus = false;
setBackground(Color.RED.darker());
}
else {
setBackground(Color.RED);
}
}
else {
setBackground(null);
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
});
buttonSelection.addActionListener(e-> {
if ( table.getSelectedColumn()!=-1 && table.getSelectedRow()!=-1 ) {
Cell cell = new Cell(table.getSelectedColumn(), table.getSelectedRow());
if ( persoselection.add(cell) ) {
enableButtons(buttonSelection, buttonDeselection, table, persoselection, cell);
table.repaint();
}
}
});
buttonDeselection.addActionListener(e-> {
Cell cell = new Cell(table.getSelectedColumn(), table.getSelectedRow());
if ( persoselection.remove(cell) ) {
enableButtons(buttonSelection, buttonDeselection, table, persoselection, cell);
table.repaint();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(buttonSelection);
buttonPanel.add(buttonDeselection);
frame.getContentPane().add(table);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void enableButtons(JButton buttonSelection, JButton buttonDeselection, JTable table,
Set<Cell> persoselection, Cell cell) {
final boolean selected = persoselection.contains(cell);
buttonSelection.setEnabled(!selected);
buttonDeselection.setEnabled(selected);
}
private static JTable createTable() {
JTable table = new JTable(new Object[][]{
{"Data 1","Data 2","Date 3"},
{"Data 1","Data 2","Date 3"},
{"Data 1","Data 2","Date 3"}
}, new String[]{"Col 1","Col 2", "Col 3"});
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(true);
table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
return table;
}
public final static class Cell {
private final int x;
private final int y;
public Cell(int x, int y) {
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public boolean equals(Object obj) {
if ( obj!=null && Cell.class.equals(obj.getClass()) ) {
Cell cell = (Cell)obj;
return cell.x==x && cell.y==y;
}
return false;
}
}
} |
Partager