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
| public class DemoJTable {
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTable());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static Component createTable() {
JPanel panel = new JPanel(new BorderLayout());
TableModel model = createModel();
JTable table = new JTable(model);
panel.add(table.getTableHeader(), BorderLayout.NORTH);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setColumnHeaderView(table.getTableHeader());
panel.add(scrollpane);
setNavigationKeys(table);
return panel;
}
private static void setNavigationKeys(JTable table) {
Action navigationEditAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if ( table.getRowCount()==0 || table.getColumnCount()==0 ) return; // table sans lignes/colonnes
int row = table.isEditing()?table.getEditingRow():table.getSelectedRow(); // ligne courante
int col = table.isEditing()?table.getEditingColumn():table.getSelectedColumn(); // colonne courante
if ( row==-1 && col==-1 ) { // pas encore de sélection
walkToNext(row, col);
}
else {
if ( table.isEditing() ) { // sélection en cours d'édition
walkToNext(row, col); // on cherche la prochaine cellule éditable
}
else {
if ( table.isCellEditable(row, col) ) { // si on est pas en édition, alors on passe en édition
table.editCellAt(row, col);
}
else { // sinon on cherche la prochaine cellule éditable
walkToNext(row, col);
}
}
}
}
private void walkToNext(int row, int col) {
int nextRow = row+1; // déplacement sur colonne, prioritaire à déplacement sur ligne (si row==-1 alors row devient 0 = première ligne)
int nextCol = col==-1?0:col; // col est -1 (si col==-1 alors col devient 0 = première ligne)
if ( nextRow>=table.getRowCount() ) { // on sort de la table (dernière ligne)
nextRow=0;
nextCol++;
if ( nextCol>=table.getColumnCount() ) { // on sort de la table (dernière colonne)
nextCol=0;
}
}
if ( row==-1 ) row=0; // pour éviter boucle infinie
if ( col==-1 ) col=0;// pour éviter boucle infinie
boolean loop=true;
boolean found=false;
do {
if ( table.isCellEditable(nextRow, nextCol) ) {
found=true;
loop=false;
}
else {
nextRow++;
if ( nextRow>=table.getRowCount() ) {
nextRow=0;
nextCol++;
if ( nextCol>=table.getColumnCount() ) {
nextCol=0;
}
}
if ( nextRow==row && nextCol==col ) { // empêche boucle infinie
loop=false; // pas de cellule éditable trouvée
}
}
}
while(loop);
if ( found ) {
if( table.isEditing() ) { // arrêt édition en cours
table.getCellEditor().stopCellEditing();
}
table.changeSelection(nextRow, nextCol, false, false); // changement de sélection
table.editCellAt(nextRow, nextCol); // activation édition nouvelle cellule
}
}
};
KeyStroke enterkey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
override(navigationEditAction, table, im, enterkey, "Enter");
}
private static void override(Action action, JTable table, InputMap im, KeyStroke key, String actionKey) {
ActionMap am = table.getActionMap();
im.put(key, actionKey);
am.put(actionKey, action);
}
private static TableModel createModel() {
String[] names = {"Col 1","Col 2","Col 3"};
String[][] values = new String[5][names.length];
for(String[] line : values) {
for(int i=0; i<line.length; i++) {
line[i]="abc";
}
}
DefaultTableModel model = new DefaultTableModel(values, names) {
@Override
public boolean isCellEditable(int row, int column) {
return column==0;
}
};
return model;
}
} |
Partager