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)); }
public void registerKeyboardAction(String command, int key) { panel.registerKeyboardAction( new ControlKeyAction(command), KeyStroke.getKeyStroke(key, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); }