Example usage for javax.swing JComponent WHEN_FOCUSED

List of usage examples for javax.swing JComponent WHEN_FOCUSED

Introduction

In this page you can find the example usage for javax.swing JComponent WHEN_FOCUSED.

Prototype

int WHEN_FOCUSED

To view the source code for javax.swing JComponent WHEN_FOCUSED.

Click Source Link

Document

Constant used for registerKeyboardAction that means that the command should be invoked when the component has the focus.

Usage

From source file:Main.java

/**
 * Obtine actiunea inregistrata pentru apasarea de tasta
 * @return actiune inregistrata pentru tasta sau null
 *//*from  w w w  . j  a  v  a  2s.  c  om*/
public static Action getActionForKeystroke(JComponent component, KeyStroke keyStroke) {
    Action whenFocused = getActionForKeystroke(component, JComponent.WHEN_FOCUSED, keyStroke);
    Action whenAncestorOfFocused = getActionForKeystroke(component,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, keyStroke);
    Action whenInWindow = getActionForKeystroke(component, JComponent.WHEN_IN_FOCUSED_WINDOW, keyStroke);

    //Ordinea preferata pentru a le returna
    if (whenFocused != null)
        return whenFocused;
    if (whenAncestorOfFocused != null)
        return whenAncestorOfFocused;
    if (whenInWindow != null)
        return whenInWindow;

    //Valoare default
    return null;
}

From source file:Main.java

public static void installActions(JComponent comp, Action actions[]) {
    installActions(comp, actions, JComponent.WHEN_FOCUSED);
}

From source file:Main.java

public static void bindAction(JComponent aComponent, String aCommand, Action anAction, KeyStroke aKeyStroke) {
    aComponent.getInputMap(JComponent.WHEN_FOCUSED).put(aKeyStroke, aCommand);
    aComponent.getActionMap().put(aCommand, anAction);
}

From source file:com.mgmtp.perfload.loadprofiles.ui.util.SwingUtils.java

/**
 * Registers the enter key a {@link JButton}.
 * //www.  jav a 2s .  c  o  m
 * @param button
 *            The button
 */
public static void enterPressesWhenFocused(final JButton button) {
    button.registerKeyboardAction(
            button.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
            KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED);

    button.registerKeyboardAction(
            button.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
            KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), JComponent.WHEN_FOCUSED);
}

From source file:Main.java

/**
 * install focus forward and backward//from www . ja  v  a 2s  . c o m
 */
public static void installDefaultFocusHandling(Container c) {
    // focus TAB
    HashSet<KeyStroke> set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
    c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

    // focus shift-TAB
    set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
    c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);

    // make input map WHEN_FOCUSED non-empty for Focus Traversal policy to work
    // correctly
    if (c instanceof JComponent) {
        JComponent jc = (JComponent) c;
        InputMap inputMapWhenFocused = jc.getInputMap(JComponent.WHEN_FOCUSED);
        if (inputMapWhenFocused.size() == 0) {
            inputMapWhenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, KeyEvent.KEY_TYPED),
                    "swingDummyFocusKey");
        }
    }
}

From source file:Main.java

/**
 * Registers the keystroke of the given action as "command" of the given
 * component.//from   ww w. j  a  va 2  s.com
 * <p>
 * This code is based on the Sulky-tools, found at
 * &lt;http://github.com/huxi/sulky&gt;.
 * </p>
 * 
 * @param aComponent
 *          the component that should react on the keystroke, cannot be
 *          <code>null</code>;
 * @param aAction
 *          the action of the keystroke, cannot be <code>null</code>;
 * @param aCommandName
 *          the name of the command to register the keystore under.
 */
public static void registerKeystroke(final JComponent aComponent, final Action aAction,
        final String aCommandName) {
    final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY);
    if (keyStroke == null) {
        return;
    }

    InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = aComponent.getActionMap();
    inputMap.put(keyStroke, aCommandName);
    actionMap.put(aCommandName, aAction);

    inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED);
    Object value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }

    inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }
}

From source file:com.croer.javaorange.diviner.SimpleOrangeTextPane.java

public SimpleOrangeTextPane() {
    CONFIGURATION = Configuration.getCONFIGURATION();

    //Create the style array to show the colors 
    List<Object> colorList = CONFIGURATION.getList("ColorWord");
    styles = new Style[colorList.size()];
    StyleContext sc = new StyleContext();
    for (int i = 0; i < colorList.size(); i++) {
        styles[i] = sc.addStyle((String) colorList.get(i), sc.getStyle(StyleContext.DEFAULT_STYLE));
        StyleConstants.setForeground(styles[i], ColorUtils.getColorByName((String) colorList.get(i)));
        StyleConstants.setBold(styles[i], true);
    }/*from   w ww  .  ja  v  a 2 s . c o m*/

    //Deactive key bindings 
    List<Object> navigationList = CONFIGURATION.getList("PageNavigation");
    for (Object object : navigationList) {
        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(object.toString()), "none");
    }

    //Get the document for adding a document listener
    dsd = (DefaultStyledDocument) getDocument();
    dsd.addDocumentListener(new DocumentListenerTextPane());

    //...and setting a document filter
    documentFilter = new MyDocumentFilter();
    dsd.setDocumentFilter(documentFilter);
}

From source file:UndoExample5.java

public UndoExample5() {
    super("Undo/Redo Example 5");

    pane = new JTextPane();
    pane.setEditable(true); // Editable
    getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER);

    // Add a menu bar
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);/* w  ww.  ja v  a2s  .c  o  m*/

    // Populate the menu bar
    createMenuBar();

    // Create the undo manager and actions
    MonitorableUndoManager manager = new MonitorableUndoManager();
    pane.getDocument().addUndoableEditListener(manager);

    Action undoAction = new UndoAction(manager);
    Action redoAction = new RedoAction(manager);

    // Add the actions to buttons
    JPanel panel = new JPanel();
    final JButton undoButton = new JButton("Undo");
    final JButton redoButton = new JButton("Redo");
    undoButton.addActionListener(undoAction);
    redoButton.addActionListener(redoAction);

    undoButton.setEnabled(false);
    redoButton.setEnabled(false);
    panel.add(undoButton);
    panel.add(redoButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    // Assign the actions to keys
    pane.registerKeyboardAction(undoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK),
            JComponent.WHEN_FOCUSED);
    pane.registerKeyboardAction(redoAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK),
            JComponent.WHEN_FOCUSED);

    // Handle events from the MonitorableUndoManager
    manager.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            MonitorableUndoManager m = (MonitorableUndoManager) evt.getSource();
            boolean canUndo = m.canUndo();
            boolean canRedo = m.canRedo();

            undoButton.setEnabled(canUndo);
            redoButton.setEnabled(canRedo);

            undoButton.setToolTipText(canUndo ? m.getUndoPresentationName() : null);
            redoButton.setToolTipText(canRedo ? m.getRedoPresentationName() : null);
        }
    });
}

From source file:de.tor.tribes.ui.views.DSWorkbenchSelectionFrame.java

/**
 * Creates new form DSWorkbenchSelectionFrame
 *///from  w ww . j av a  2  s  . c o  m
DSWorkbenchSelectionFrame() {
    initComponents();
    centerPanel = new GenericTestPanel(true);
    jSelectionPanel.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setChildComponent(jSelectionTreePanel);
    buildMenu();
    capabilityInfoPanel1.addActionListener(this);
    treeData = new LinkedList<>();
    jSelectionTree.setCellRenderer(new NodeCellRenderer());

    KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
    KeyStroke bbCopy = KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK, false);
    KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, false);
    KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false);
    KeyStroke cut = KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK, false);
    jSelectionTree.registerKeyboardAction(DSWorkbenchSelectionFrame.this, "Copy", copy,
            JComponent.WHEN_FOCUSED);
    jSelectionTree.registerKeyboardAction(DSWorkbenchSelectionFrame.this, "BBCopy", bbCopy,
            JComponent.WHEN_FOCUSED);
    jSelectionTree.registerKeyboardAction(DSWorkbenchSelectionFrame.this, "Delete", delete,
            JComponent.WHEN_FOCUSED);
    jSelectionTree.registerKeyboardAction(DSWorkbenchSelectionFrame.this, "Paste", paste,
            JComponent.WHEN_FOCUSED);
    jSelectionTree.registerKeyboardAction(DSWorkbenchSelectionFrame.this, "Cut", cut, JComponent.WHEN_FOCUSED);
    jSelectionTree.getActionMap().put("find", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //ignore find
        }
    });
    jSelectionTree.getSelectionModel().addTreeSelectionListener(DSWorkbenchSelectionFrame.this);

    buildTree();

    //<editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
    if (!Constants.DEBUG) {
        GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.selection_tool",
                GlobalOptions.getHelpBroker().getHelpSet());
    }
    //</editor-fold>
}

From source file:ec.util.chart.swing.JTimeSeriesChart.java

public JTimeSeriesChart() {
    super(Arrays.asList(MARKER, LINE, SPLINE, COLUMN, STACKED_COLUMN, AREA, STACKED_AREA));
    this.chartPanel = new ChartPanel(createTsChart(), false, false, false, false, false);
    this.notification = new ChartNotification(chartPanel.getChart());
    this.seriesSelectionModel = new DefaultListSelectionModel();
    this.mainPlot = (CombinedDomainXYPlot) chartPanel.getChart().getXYPlot();
    this.roSubPlots = mainPlot.getSubplots();
    this.seriesMapFactory = new SeriesMapFactory();
    this.revealObs = false;
    this.fontSupport = new SwingFontSupportImpl();

    notification.suspend();/*from  w  ww. j  av  a  2  s  . com*/

    // initialization
    onColorSchemeSupportChange();
    onLineThicknessChange();
    onPeriodFormatChange();
    onValueFormatChange();
    onSeriesRendererChange();
    onSeriesFormatterChange();
    onSeriesColoristChange();
    onObsFormatterChange();
    onObsColoristChange();
    onDashPredicateChange();
    onLegendVisibilityPredicateChange();
    onPlotDispatcherChange();
    onDatasetChange();
    onTitleChange();
    onNoDataMessageChange();
    onPlotWeightsChange();
    onElementVisibleChange();
    onCrosshairOrientationChange();
    onHoveredObsChange();
    onSelectedObsChange();
    onObsHighlighterChange();
    onTooltipTriggerChange();
    onCrosshairTriggerChange();
    onRevealObsChange();
    onFontSupportChange();

    Charts.avoidScaling(chartPanel);
    Charts.enableFocusOnClick(chartPanel);

    enableObsTriggering();
    enableRevealObs();
    enableSelection();
    enableProperties();

    chartPanel.setActionMap(getActionMap());
    chartPanel.setInputMap(JComponent.WHEN_FOCUSED, getInputMap());

    notification.resume();

    setLayout(new BorderLayout());
    this.add(chartPanel, BorderLayout.CENTER);

    if (Beans.isDesignTime()) {
        setTitle("Preview");
        setElementVisible(Element.TITLE, true);
        setPreferredSize(new Dimension(400, 300));
    }
}