public MainPanel() { super(new BorderLayout()); InputMap im = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); KeyStroke stab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK); KeyStroke senter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK); im.put(tab, im.get(enter)); im.put(stab, im.get(senter)); final Color orgColor = table.getSelectionBackground(); final Color tflColor = this.getBackground(); table.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent e) { table.setSelectionForeground(Color.WHITE); table.setSelectionBackground(orgColor); } @Override public void focusLost(FocusEvent e) { table.setSelectionForeground(Color.BLACK); table.setSelectionBackground(tflColor); } }); table.setComponentPopupMenu(new TablePopupMenu()); add(new JScrollPane(table)); setPreferredSize(new Dimension(320, 240)); }
// --------> // original code: // ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java // modified by terai // private Hashtable<Object, ArrayList<KeyStroke>> buildReverseMap(InputMap im) { // Hashtable<Object, ArrayList<KeyStroke>> h = new Hashtable<>(); // if (Objects.isNull(im.allKeys())) { // return h; // } // for (KeyStroke ks: im.allKeys()) { // Object name = im.get(ks); // if (h.containsKey(name)) { // h.get(name).add(ks); // } else { // ArrayList<KeyStroke> keylist = new ArrayList<>(); // keylist.add(ks); // h.put(name, keylist); // } // } // return h; // } private void loadBindingMap(Integer focusType, InputMap im, ActionMap am) { if (Objects.isNull(im.allKeys())) { return; } ActionMap tmpAm = new ActionMap(); for (Object actionMapKey : am.allKeys()) { tmpAm.put(actionMapKey, am.get(actionMapKey)); } for (KeyStroke ks : im.allKeys()) { Object actionMapKey = im.get(ks); Action action = am.get(actionMapKey); if (Objects.isNull(action)) { model.addBinding(new Binding(focusType, "____" + actionMapKey.toString(), ks.toString())); } else { model.addBinding(new Binding(focusType, actionMapKey.toString(), ks.toString())); } tmpAm.remove(actionMapKey); } if (Objects.isNull(tmpAm.allKeys())) { return; } for (Object actionMapKey : tmpAm.allKeys()) { model.addBinding(new Binding(focusType, actionMapKey.toString(), "")); } }
private static void installActions(JTable table) { InputMap inputMap = table.getInputMap(WHEN_FOCUSED); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0), "selectLastRow"); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0), "selectFirstRow"); inputMap.put( KeyStroke.getKeyStroke(KeyEvent.VK_HOME, KeyEvent.SHIFT_DOWN_MASK), "selectFirstRowExtendSelection"); inputMap.put( KeyStroke.getKeyStroke(KeyEvent.VK_END, KeyEvent.SHIFT_DOWN_MASK), "selectLastRowExtendSelection"); }