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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
public class TestActions extends JDialog {
protected JPanel panelTable;
protected JPanel panelTxt;
protected JPanel panelButtons;
protected JTable maTable;
protected JFormattedTextField txt;
protected JButton bClose;
// Bouton servant à donner le focus à autre chose que la JTable
// => dans ce cas, "Esc" fonctionne correctement...
protected JButton bPourLeFocus;
public TestActions() {
super();
setTitle("Test");
setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
Insets buttonsMargins = new Insets(0, 0, 0, 0);
panelTxt = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0));
panelTxt.setSize(450, 80);
txt = new JFormattedTextField(new DecimalFormat());
txt.setSize(60, 20);
txt.setPreferredSize(new Dimension(60, 20));
txt.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
txt_keyPressed(ke);
}
});
panelTxt.add(txt);
panelTable = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0));
panelTable.setSize(450, 150);
maTable = new JTable(new MyTableModel());
maTable.setPreferredScrollableViewportSize(new Dimension(365, 98));
maTable.getTableHeader().setReorderingAllowed(false);
maTable.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
drillingTable_keyPressed(ke);
}
});
JScrollPane scrollPane = new JScrollPane(maTable);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panelTable.add(scrollPane);
panelButtons = new JPanel(new FlowLayout(FlowLayout.CENTER, 8, 8));
panelButtons.setSize(400, 45);
/** Ferme la fenêtre */
bClose = new JButton("Fermer");
bClose.setMargin(buttonsMargins);
bClose.setSize(new Dimension(65, 25));
bClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
/** Sert juste à donner le focus à un autre composant que la JTable */
bPourLeFocus = new JButton("Focus");
bPourLeFocus.setMargin(buttonsMargins);
bPourLeFocus.setSize(new Dimension(65, 25));
panelButtons.add(bClose);
panelButtons.add(bPourLeFocus);
getContentPane().add(panelTxt, BorderLayout.NORTH);
getContentPane().add(panelTable, BorderLayout.CENTER);
getContentPane().add(panelButtons, BorderLayout.SOUTH);
this.pack();
//************** Initialisation des actions!**************
initKeyManagement();
setLocationRelativeTo(null);
setVisible(true);
}
/***********************
* Gestion du clavier. *
***********************/
protected void txt_keyPressed(KeyEvent ke) {
// if (ke.getKeyChar() == KeyEvent.VK_ESCAPE) {
// System.out.println("esc");
// escapeActionPerformed();
// }
// else if (ke.getKeyChar() == KeyEvent.VK_ENTER) {
// System.out.println("entrée");
// escapeActionPerformed();
// }
}
protected void drillingTable_keyPressed(KeyEvent ke) {
if (ke.getKeyChar() == KeyEvent.VK_DELETE) {
removeRow();
}
//test avec AbstractActions
// else {
// dispatchEvent(ke);
// }
// else if (ke.getKeyChar() == KeyEvent.VK_ESCAPE) {
// if (drillingTable.getSelectedRow() != -1) {
// drillingTable.clearSelection();
// }
// else {
// /** Si on a déjà désélectionné la ligne, on propage l'évènement à la JDialog */
// escapeActionPerformed();
// }
// }
}
protected void escapeActionPerformed() {
System.out.println("escapeActionPerformed()!");
System.exit(0);
}
/**
* Supprime une ligne du tableau.
*/
public void removeRow() {
if (maTable.getRowCount() > 0) {
if (maTable.getSelectedRow() > -1) {
((MyTableModel) maTable.getModel()).removeRow(maTable.getSelectedRow());
}
}
}
/***************************************
********** MyTableModel.class **********
****************************************/
/**
* Modèle du tableau.
*/
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"A", "B", "C", "D"};
private ArrayList data;
public MyTableModel() {
data = new ArrayList(5);
for (int i=0; i<5; i++) {
data.add(new String[] {"1","2","3","4","5"});
}
}
public void removeRow(int row) {
if (data.size() > 0 && row < data.size()) {
data.remove(row);
}
fireTableDataChanged();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return ((Object[]) data.get(row))[col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col) {
((Object[]) data.get(row))[col] = value;
}
public int getMaxLength() {
int res = -1;
for (int i=0; i<data.size(); i++) {
for (int j = 0; j < ((Object[]) data.get(i)).length; j++) {
res = Math.max(res, ((Object[]) data.get(i))[j].toString().length());
}
}
return res;
}
}
/**
* Méthode de MyDialog redéfinie afin de gérer les KeyEvent de manière
* spécifique à cette IHM.
*/
protected void initKeyManagement() {
/** Définition des combinaisons de touches prises en compte */
KeyStroke ksEscape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
KeyStroke ksEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
/** Définition des actions à exécuter */
/** Touche "Esc" */
AbstractAction escapeAction = new AbstractAction() {
public void actionPerformed(ActionEvent arg0) {
if (maTable.getSelectedRow() != -1) {
maTable.clearSelection();
System.out.println("Ligne désélectionnée!");
}
else {
/** Si on a déjà désélectionné la ligne, on ferme la JDialog */
System.out.println("esc");
escapeActionPerformed();
}
}
};
/** Touche "Entrée" */
AbstractAction enterAction = new AbstractAction() {
public void actionPerformed(ActionEvent arg0) {
/** On valide la boîte de dialogue */
System.out.println("entrée");
escapeActionPerformed();
}
};
/** Association KeyStroke -> Action */
setKeyStrokeAction(ksEscape, escapeAction, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
setKeyStrokeAction(ksEnter, enterAction, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
/**
* Association de l'ActionMap de la JDialog comme ActionMap parente de
* celle des champs texte afin de propager l'évènement KeyPressed à la JDialog
* si aucune entrée correspondant à la clé recherchée n'est trouvée dans
* l'ActionMap des champs texte.
*/
ActionMap tableMinActionMap = maTable.getActionMap();
if (tableMinActionMap == null)
tableMinActionMap = new ActionMap();
tableMinActionMap.setParent(getRootPane().getActionMap());
ActionMap txtMinActionMap = txt.getActionMap();
if (txtMinActionMap == null)
txtMinActionMap = new ActionMap();
txtMinActionMap.setParent(getRootPane().getActionMap());
}
/**
* Méthode permettant d'ajouter un couple KeyStroke - Action à la Dialog.
* Utilisé pour faire du Key Binding.
* cf http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
*
* @param KeyStroke ks : la combinaison de touches associée à l'action
* @param AbstractAction action : l'action à exécuter lors de la combinaison de touches
* @param inputMapType : le contexte dans lequel on doit exécuter l'action
* lorsqu'on détecte la combinaison de touches.
*
* Le paramètre inputMapType peut avoir les valeurs :
* - JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
* - JComponent.WHEN_IN_FOCUSED_WINDOW
* - JComponent.WHEN_FOCUSED
*/
public void setKeyStrokeAction(KeyStroke ks, AbstractAction action, int inputMapType) {
/** Récupération de l'InputMap correspondant au type passé en paramètre*/
InputMap im = getRootPane().getInputMap(inputMapType);
/** Récupération de l'ActionMap de la dialog */
ActionMap am = getRootPane().getActionMap();
if (am == null)
am = new ActionMap();
/** Ajout du couple KeyStroke - Action */
Object command = new Object();
im.put(ks, command);
am.put(command, action);
}
public static void main(String args[]) {
TestActions test = new TestActions();
test.getClass();
}
} |
Partager