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"); }
/** Constructs cell editor. */ public CellEditor() { super(new JFormattedTextField()); final JFormattedTextField ftf = (JFormattedTextField) getComponent(); // Set GUI behaviour of text field ftf.setValue(null); ftf.setHorizontalAlignment(JTextField.LEADING); ftf.setFocusLostBehavior(JFormattedTextField.PERSIST); // Set that one click on cell is enough for editing setClickCountToStart(1); // Special handling code for ENTER ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check"); ftf.getActionMap() .put( "check", new AbstractAction() { public void actionPerformed(ActionEvent e) { if (!ftf.isEditValid()) { if (askEditOrRevert(ftf, null)) { ftf.setValue(ftf.getValue()); ftf.postActionEvent(); } } else try { ftf.commitEdit(); ftf.postActionEvent(); } catch (java.text.ParseException exc) { // nothing to do } } }); }
/** * This method updates the input and action maps with new ToggleAction. * * @param isToggleDynamic * @param isToggleLarger * @param key * @param keyStroke */ private void installToggleAction( boolean isToggleDynamic, boolean isToggleLarger, String key, String keyStroke) { Action action = new ToggleAction(isToggleDynamic, isToggleLarger); KeyStroke ks = KeyStroke.getKeyStroke(keyStroke); table.getInputMap().put(ks, key); table.getActionMap().put(key, action); }
/** * This method updates the input and action maps with a new ColumnAction. * * @param isSelectedColumn * @param isAdjust * @param key * @param keyStroke */ private void installColumnAction( boolean isSelectedColumn, boolean isAdjust, String key, String keyStroke) { Action action = new ColumnAction(isSelectedColumn, isAdjust); KeyStroke ks = KeyStroke.getKeyStroke(keyStroke); table.getInputMap().put(ks, key); table.getActionMap().put(key, action); }
private JMenuItem closeAction() { JMenuItem result = new JMenuItem("Close"); result.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.META_MASK)); result.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { LookSpike.this.dispose(); } }); return result; }
public TextAreaCellEditor() { super(); scroll = new JScrollPane(this); scroll.setBorder(BorderFactory.createEmptyBorder()); // scroll.setViewportBorder(BorderFactory.createEmptyBorder()); setLineWrap(true); setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4)); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK); getInputMap(JComponent.WHEN_FOCUSED).put(enter, KEY); getActionMap() .put( KEY, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { stopCellEditing(); } }); }
public static void makeMenuBar(JFrame frame, final AirspaceBuilderController controller) { JMenuBar menuBar = new JMenuBar(); final JCheckBoxMenuItem resizeNewShapesItem; final JCheckBoxMenuItem enableEditItem; JMenu menu = new JMenu("File"); { JMenuItem item = new JMenuItem("Open..."); item.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); item.setActionCommand(OPEN); item.addActionListener(controller); menu.add(item); item = new JMenuItem("Open URL..."); item.setActionCommand(OPEN_URL); item.addActionListener(controller); menu.add(item); item = new JMenuItem("Save..."); item.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); item.setActionCommand(SAVE); item.addActionListener(controller); menu.add(item); menu.addSeparator(); item = new JMenuItem("Load Demo Shapes"); item.setActionCommand(OPEN_DEMO_AIRSPACES); item.addActionListener(controller); menu.add(item); } menuBar.add(menu); menu = new JMenu("Shape"); { JMenu subMenu = new JMenu("New"); for (final AirspaceFactory factory : defaultAirspaceFactories) { JMenuItem item = new JMenuItem(factory.toString()); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { controller.createNewEntry(factory); } }); subMenu.add(item); } menu.add(subMenu); resizeNewShapesItem = new JCheckBoxMenuItem("Fit new shapes to viewport"); resizeNewShapesItem.setActionCommand(SIZE_NEW_SHAPES_TO_VIEWPORT); resizeNewShapesItem.addActionListener(controller); resizeNewShapesItem.setState(controller.isResizeNewShapesToViewport()); menu.add(resizeNewShapesItem); enableEditItem = new JCheckBoxMenuItem("Enable shape editing"); enableEditItem.setActionCommand(ENABLE_EDIT); enableEditItem.addActionListener(controller); enableEditItem.setState(controller.isEnableEdit()); menu.add(enableEditItem); } menuBar.add(menu); menu = new JMenu("Selection"); { JMenuItem item = new JMenuItem("Deselect"); item.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_D, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); item.setActionCommand(CLEAR_SELECTION); item.addActionListener(controller); menu.add(item); item = new JMenuItem("Delete"); item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); item.setActionCommand(REMOVE_SELECTED); item.addActionListener(controller); menu.add(item); } menuBar.add(menu); frame.setJMenuBar(menuBar); controller.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if (SIZE_NEW_SHAPES_TO_VIEWPORT.equals((e.getPropertyName()))) { resizeNewShapesItem.setSelected(controller.isResizeNewShapesToViewport()); } else if (ENABLE_EDIT.equals(e.getPropertyName())) { enableEditItem.setSelected(controller.isEnableEdit()); } } }); }
private ElementsChooser(@Nullable List<T> elements, boolean marked, boolean elementsCanBeMarked) { super(new BorderLayout()); myTableModel = new MyTableModel(elementsCanBeMarked); myTable = new Table(myTableModel); myTable.setShowGrid(false); myTable.setIntercellSpacing(new Dimension(0, 0)); myTable.setTableHeader(null); myTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); myTable.setColumnSelectionAllowed(false); JScrollPane pane = ScrollPaneFactory.createScrollPane(myTable); pane.setPreferredSize(new Dimension(100, 155)); int width = new JCheckBox().getPreferredSize().width; TableColumnModel columnModel = myTable.getColumnModel(); if (elementsCanBeMarked) { TableColumn checkMarkColumn = columnModel.getColumn(myTableModel.CHECK_MARK_COLUM_INDEX); checkMarkColumn.setPreferredWidth(width); checkMarkColumn.setMaxWidth(width); checkMarkColumn.setCellRenderer( new CheckMarkColumnCellRenderer(myTable.getDefaultRenderer(Boolean.class))); } columnModel .getColumn(myTableModel.ELEMENT_COLUMN_INDEX) .setCellRenderer(new MyElementColumnCellRenderer()); add(pane, BorderLayout.CENTER); myTable.registerKeyboardAction( new ActionListener() { public void actionPerformed(ActionEvent e) { final int[] selectedRows = myTable.getSelectedRows(); boolean currentlyMarked = true; for (int selectedRow : selectedRows) { currentlyMarked = myTableModel.isElementMarked(selectedRow); if (!currentlyMarked) { break; } } myTableModel.setMarked(selectedRows, !currentlyMarked); } }, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_FOCUSED); final SpeedSearchBase<JBTable> speedSearch = new SpeedSearchBase<JBTable>(myTable) { public int getSelectedIndex() { return myTable.getSelectedRow(); } @Override protected int convertIndexToModel(int viewIndex) { return myTable.convertRowIndexToModel(viewIndex); } public Object[] getAllElements() { final int count = myTableModel.getRowCount(); Object[] elements = new Object[count]; for (int idx = 0; idx < count; idx++) { elements[idx] = myTableModel.getElementAt(idx); } return elements; } public String getElementText(Object element) { return getItemText((T) element); } public void selectElement(Object element, String selectedText) { final int count = myTableModel.getRowCount(); for (int row = 0; row < count; row++) { if (element.equals(myTableModel.getElementAt(row))) { final int viewRow = myTable.convertRowIndexToView(row); myTable.getSelectionModel().setSelectionInterval(viewRow, viewRow); TableUtil.scrollSelectionToVisible(myTable); break; } } } }; speedSearch.setComparator(new SpeedSearchBase.SpeedSearchComparator(false)); setElements(elements, marked); installActions(myTable); }