Java Code Examples for java.awt.Component#requestFocus()

The following examples show how to use java.awt.Component#requestFocus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicLabelUI.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void doRelease(JLabel label) {
    Component labelFor = label.getLabelFor();
    if (labelFor != null && labelFor.isEnabled()) {
        InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
        if (inputMap != null) {
            // inputMap should never be null.
            int dka = label.getDisplayedMnemonic();
            inputMap.remove(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true));
            inputMap.remove(KeyStroke.getKeyStroke(dka, 0, true));
            inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
        }
        if (labelFor instanceof Container &&
                ((Container) labelFor).isFocusCycleRoot()) {
            labelFor.requestFocus();
        } else {
            SwingUtilities2.compositeRequestFocus(labelFor);
        }
    }
}
 
Example 2
Source File: BasicLabelUI.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void doRelease(JLabel label) {
    Component labelFor = label.getLabelFor();
    if (labelFor != null && labelFor.isEnabled()) {
        InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
        if (inputMap != null) {
            // inputMap should never be null.
            int dka = label.getDisplayedMnemonic();
            inputMap.remove(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true));
            inputMap.remove(KeyStroke.getKeyStroke(dka, 0, true));
            inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
        }
        if (labelFor instanceof Container &&
                ((Container) labelFor).isFocusCycleRoot()) {
            labelFor.requestFocus();
        } else {
            SwingUtilities2.compositeRequestFocus(labelFor);
        }
    }
}
 
Example 3
Source File: CategoryList.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
    int selIndexBefore = getSelectedIndex();
    defaultAction.actionPerformed( e );
    int selIndexCurrent = getSelectedIndex();
    if( selIndexBefore != selIndexCurrent )
        return;
    
    if( focusNext && 0 == selIndexCurrent && getModel().getSize() > 1 && getModel().getSize() > getColumnCount() )
        return;
    
    KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    Container container = kfm.getCurrentFocusCycleRoot();
    FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
    if( null == policy )
        policy = kfm.getDefaultFocusTraversalPolicy();
    Component next = focusNext ? policy.getComponentAfter( container, CategoryList.this )
                              : policy.getComponentBefore( container, CategoryList.this );
    if( null != next && next instanceof CategoryButton ) {
        clearSelection();
        next.requestFocus();
    }
}
 
Example 4
Source File: BasicLabelUI.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private void doRelease(JLabel label) {
    Component labelFor = label.getLabelFor();
    if (labelFor != null && labelFor.isEnabled()) {
        InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
        if (inputMap != null) {
            // inputMap should never be null.
            int dka = label.getDisplayedMnemonic();
            inputMap.remove(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true));
            inputMap.remove(KeyStroke.getKeyStroke(dka, 0, true));
            inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
        }
        if (labelFor instanceof Container &&
                ((Container) labelFor).isFocusCycleRoot()) {
            labelFor.requestFocus();
        } else {
            SwingUtilities2.compositeRequestFocus(labelFor);
        }
    }
}
 
Example 5
Source File: DockContainer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
protected void setCurrentDockable(Dockable dockable, Component focusOn) {
    int index = mDockables.indexOf(dockable);
    if (index != -1) {
        int wasCurrent = mCurrent;
        mCurrent = index;
        for (Dockable one : mDockables) {
            one.setVisible(dockable == one);
        }
        mHeader.revalidate();
        repaint();
        focusOn.requestFocus();
        if (mActive && wasCurrent != mCurrent) {
            dockable.activated();
        }
    }
}
 
Example 6
Source File: BasicLabelUI.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void doRelease(JLabel label) {
    Component labelFor = label.getLabelFor();
    if (labelFor != null && labelFor.isEnabled()) {
        InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
        if (inputMap != null) {
            // inputMap should never be null.
            int dka = label.getDisplayedMnemonic();
            inputMap.remove(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true));
            inputMap.remove(KeyStroke.getKeyStroke(dka, 0, true));
            inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
        }
        if (labelFor instanceof Container &&
                ((Container) labelFor).isFocusCycleRoot()) {
            labelFor.requestFocus();
        } else {
            SwingUtilities2.compositeRequestFocus(labelFor);
        }
    }
}
 
Example 7
Source File: BasicLabelUI.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void doRelease(JLabel label) {
    Component labelFor = label.getLabelFor();
    if (labelFor != null && labelFor.isEnabled()) {
        InputMap inputMap = SwingUtilities.getUIInputMap(label, JComponent.WHEN_FOCUSED);
        if (inputMap != null) {
            // inputMap should never be null.
            int dka = label.getDisplayedMnemonic();
            inputMap.remove(KeyStroke.getKeyStroke(dka, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true));
            inputMap.remove(KeyStroke.getKeyStroke(dka, 0, true));
            inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true));
        }
        if (labelFor instanceof Container &&
                ((Container) labelFor).isFocusCycleRoot()) {
            labelFor.requestFocus();
        } else {
            SwingUtilities2.compositeRequestFocus(labelFor);
        }
    }
}
 
Example 8
Source File: ParametersPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void valueChanged(ListSelectionEvent e) {
    table.editCellAt(table.getSelectedRow(), table.getSelectedColumn());
    Component editor = table.getEditorComponent();
    if (editor instanceof JComboBox) {
        editor = ((JComboBox) editor).getEditor().getEditorComponent();
    }
    if (editor != null) {
        editor.requestFocus();
    }
    if (editor instanceof JTextComponent) {
        JTextComponent textComp = (JTextComponent) editor;
        textComp.selectAll();
    }
}
 
Example 9
Source File: CloseTabPaneUI.java    From iBioSim with Apache License 2.0 5 votes vote down vote up
boolean requestMyFocusForVisibleComponent() {
	Component visibleComponent = getVisibleComponent();
	if (visibleComponent.isFocusable()) {
		visibleComponent.requestFocus();
		return true;
	}
	else if (visibleComponent instanceof JComponent) {
		if (((JComponent) visibleComponent).requestFocusInWindow()) {
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: BaseTable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    setFocusCycleRoot(false);

    try {
        Container con = BaseTable.this.getFocusCycleRootAncestor();

        if (con != null) {
            Component target = BaseTable.this;

            if (getParent() instanceof JViewport) {
                target = getParent().getParent();

                if (target == con) {
                    target = BaseTable.this;
                }
            }

            EventObject eo = EventQueue.getCurrentEvent();
            boolean backward = false;

            if (eo instanceof KeyEvent) {
                backward = ((((KeyEvent) eo).getModifiers() & KeyEvent.SHIFT_MASK) != 0) &&
                    ((((KeyEvent) eo).getModifiersEx() & KeyEvent.SHIFT_DOWN_MASK) != 0);
            }

            Component to = backward ? con.getFocusTraversalPolicy().getComponentAfter(con, BaseTable.this)
                                    : con.getFocusTraversalPolicy().getComponentAfter(con, BaseTable.this);

            if (to == BaseTable.this) {
                to = backward ? con.getFocusTraversalPolicy().getFirstComponent(con)
                              : con.getFocusTraversalPolicy().getLastComponent(con);
            }

            to.requestFocus();
        }
    } finally {
        setFocusCycleRoot(true);
    }
}
 
Example 11
Source File: Dialogos.java    From brModelo with GNU General Public License v3.0 5 votes vote down vote up
public static int ShowMessageConfirm(Component parente, String msgTexto, boolean msgConfirm) {
    if (msgConfirm) {
        return ShowMessageConfirm(parente, msgTexto);
    }
    if (parente != null) parente.requestFocus();
    return (JOptionPane.showConfirmDialog(parente, (msgTexto.isEmpty() ? "?"  : " " + msgTexto),
            Editor.fromConfiguracao.getValor("Controler.MSG_CONFIRM_TITLE"),
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)); 
}
 
Example 12
Source File: FocusArrowListener.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
/** Shifts the focus in a certain direction.
 * 
 * @param dx the amount to increment x.
 * @param dy the amount to increment y.
 * @param src the source to traverse from.
 * @return true if another component requested the focus
 * as a result of this method.  This may return false if
 * no suitable component was found to shift focus to.
 * (If you press the right arrow key on the right-most
 * component, for example.)
 */
public static boolean shiftFocus(int dx,int dy,Component src) {
	if(dx==0 && dy==0) //this would result in an infinite loop
		throw new IllegalArgumentException("dx ("+dx+") and ("+dy+") cannot both be zero");
	
	Set<Component> focusableComponents = getFocusableComponents(src);
	
	int x = src.getWidth()/2;
	int y = src.getHeight()/2;
	Window window = SwingUtilities.getWindowAncestor(src);
	if(window==null) return false;
	Point p = SwingUtilities.convertPoint(src, x, y, window);
	
	Component comp = null;
	int windowWidth = window.getWidth();
	int windowHeight = window.getHeight();
	
	while(p.x>0 && p.x<windowWidth && p.y>0 && p.y<windowHeight && (comp==null || comp==src || (comp instanceof JPanel))) {
		p.x += dx;
		p.y += dy;
		comp = SwingUtilities.getDeepestComponentAt(window, p.x, p.y);
		boolean canAcceptFocus = focusableComponents.contains( comp );
		if(comp!=null && canAcceptFocus==false)
			comp = null;
	}
	
	//TODO: implement a more robust searching mechanism instead of the above
	//If a component is below the src, but to the left or right of the center:
	//it should still be detected when you press the down arrow key.

	if(comp!=null && comp!=src && comp!=window && (!(comp instanceof JPanel))) {
		comp.requestFocus();
		return true;
	}
	return false;
}
 
Example 13
Source File: ProfilerTopComponent.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
protected void componentActivated() {
    super.componentActivated();
    if (lastFocusOwner != null) {
        lastFocusOwner.requestFocus();
    } else {
        Component defaultFocusOwner = defaultFocusOwner();
        if (defaultFocusOwner != null) defaultFocusOwner.requestFocus();
    }
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
            addPropertyChangeListener("focusOwner", focusListener); // NOI18N
}
 
Example 14
Source File: ProfilerTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void componentActivated() {
    super.componentActivated();
    if (lastFocusOwner != null) {
        lastFocusOwner.requestFocus();
    } else {
        Component defaultFocusOwner = defaultFocusOwner();
        if (defaultFocusOwner != null) defaultFocusOwner.requestFocus();
    }
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
            addPropertyChangeListener("focusOwner", focusListener); // NOI18N
}
 
Example 15
Source File: OptionsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void valueChanged(ListSelectionEvent e) {
    optionsTable.editCellAt(optionsTable.getSelectedRow(), optionsTable.getSelectedColumn());
    Component editor = optionsTable.getEditorComponent();

    if (editor != null) {
        editor.requestFocus();
    }
    if (editor instanceof JTextComponent) {
        JTextComponent textComp = (JTextComponent) editor;
        textComp.selectAll();
    }
    updateButtons();
}
 
Example 16
Source File: RefactoringPanelContainer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void requestFocus() {
    super.requestFocus();
    Component comp = getRefactoringPanelComp();
    if(comp != null) {
        comp.requestFocus();
    }
}
 
Example 17
Source File: PopupMenu.java    From javamoney-examples with Apache License 2.0 5 votes vote down vote up
/**
 * This method is invoked when the specific mouse event occurs.
 *
 * @param event The mouse event that occurred.
 */
public
void
mouseReleased(MouseEvent event)
{
  Component component = event.getComponent();

  if(component.hasFocus() == false)
  {
    component.requestFocus();
  }

  if(component.isEnabled() == true)
  {
    if(behaveLikeMenu() == true)
    {
      if(event.getButton() == BUTTON1)
      {
        show(component, 0, component.getHeight());
      }
    }
    else if(event.isPopupTrigger() == true)
    {
      int yCoordinate = 0;

      if(allowFloatingY() == true)
      {
        yCoordinate = event.getY();
      }

      show(component, event.getX(), yCoordinate);
    }
  }
}
 
Example 18
Source File: ETable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
    setFocusCycleRoot(false);
    try {
        Container con = ETable.this.getFocusCycleRootAncestor();
        if (con != null) {
            /*
            Component target = ETable.this;
            if (getParent() instanceof JViewport) {
                target = getParent().getParent();
                if (target == con) {
                    target = ETable.this;
                }
            }
            */

            EventObject eo = EventQueue.getCurrentEvent();
            boolean backward = false;
            if (eo instanceof KeyEvent) {
                backward = 
                    (((KeyEvent) eo).getModifiers() 
                    & KeyEvent.SHIFT_MASK) 
                    != 0 && (((KeyEvent) eo).getModifiersEx() & 
                    KeyEvent.SHIFT_DOWN_MASK) != 0;
            }

            Component c = ETable.this;
            Component to;
            Container parentWithFTP = null;
            do {
                FocusTraversalPolicy ftp = con.getFocusTraversalPolicy();
                to = backward ? ftp.getComponentBefore(con, c)
                              : ftp.getComponentAfter(con, c);


                if (to == ETable.this) {
                    to = backward ? ftp.getFirstComponent(con)
                                  : ftp.getLastComponent(con);
                }
                if (to == ETable.this) {
                    parentWithFTP = con.getParent();
                    if (parentWithFTP != null) {
                        parentWithFTP = parentWithFTP.getFocusCycleRootAncestor();
                    }
                    if (parentWithFTP != null) {
                        c = con;
                        con = parentWithFTP;
                    }
                }
            } while (to == ETable.this && parentWithFTP != null);
            if (to != null) {
                to.requestFocus();
            }
        }
    } finally {
        setFocusCycleRoot(true);
    }
}
 
Example 19
Source File: NbPresenter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void propertyChange(final java.beans.PropertyChangeEvent evt) {
    if( !SwingUtilities.isEventDispatchThread() ) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                propertyChange(evt);
            }
        });
        return;
    }
    boolean update = false;

    if (DialogDescriptor.PROP_OPTIONS.equals(evt.getPropertyName())) {
        initializeButtons();
        update = true;
    } else if (DialogDescriptor.PROP_OPTION_TYPE.equals(evt.getPropertyName())) {
        initializeButtons();
        update = true;
    } else if (DialogDescriptor.PROP_OPTIONS_ALIGN.equals(evt.getPropertyName())) {
        initializeButtons();
        update = true;
    } else if (DialogDescriptor.PROP_MESSAGE.equals(evt.getPropertyName())) {
        initializeMessage();
        requestFocusForMessage();
        // In case change of help ID on component message:
        updateHelp();
        update = true;
    } else if (DialogDescriptor.PROP_MESSAGE_TYPE.equals(evt.getPropertyName())) {
        initializeMessage();
        requestFocusForMessage();
        update = true;
    } else if (DialogDescriptor.PROP_TITLE.equals(evt.getPropertyName())) {
        setTitle(descriptor.getTitle());
    } else if (DialogDescriptor.PROP_NO_DEFAULT_CLOSE.equals(evt.getPropertyName())) {
        setDefaultCloseOperation( descriptor instanceof WizardDescriptor || descriptor.isNoDefaultClose()
                ? JDialog.DO_NOTHING_ON_CLOSE : JDialog.DISPOSE_ON_CLOSE );
    } else if (DialogDescriptor.PROP_HELP_CTX.equals(evt.getPropertyName())) {
        // bugfix #40057, restore focus owner after help update
        Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getFocusOwner ();
        updateHelp();
        // In case buttons have changed: //just buttons!!
        // note, currentButtonsPanel may be null
        if (currentButtonsPanel != null) {
            currentButtonsPanel.revalidate();
        }
        if (currentButtonsPanel != null) {
            currentButtonsPanel.repaint();
        }
        if (fo != null) fo.requestFocus();
    } else if (DialogDescriptor.PROP_VALID.equals(evt.getPropertyName())) {
        updateOKButton(((Boolean)(evt.getNewValue())).booleanValue());
    } else if (NotifyDescriptor.PROP_INFO_NOTIFICATION.equals (evt.getPropertyName ())) {
        // XXX: need set update on true?
        updateNotificationLine (MSG_TYPE_INFO, evt.getNewValue ());
    } else if (NotifyDescriptor.PROP_WARNING_NOTIFICATION.equals (evt.getPropertyName ())) {
        // XXX: need set update on true?
        updateNotificationLine (MSG_TYPE_WARNING, evt.getNewValue ());
    } else if (NotifyDescriptor.PROP_ERROR_NOTIFICATION.equals (evt.getPropertyName ())) {
        // XXX: need set update on true?
        updateNotificationLine (MSG_TYPE_ERROR, evt.getNewValue ());
    }

    if (update) {
        Dimension sz = getSize();
        Dimension prefSize = getPreferredSize();
        if (prefSize.width > sz.width || prefSize.height > sz.height) {
            setSize(Math.max(prefSize.width, sz.width),
                    Math.max(prefSize.height, sz.height));
        }
        invalidate();
        validate();
        repaint();
    }
}
 
Example 20
Source File: FocusArrowListener.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Shifts the focus in a certain direction.
 *
 * @param dx  the amount to increment x.
 * @param dy  the amount to increment y.
 * @param src the source to traverse from.
 * @return true if another component requested the focus
 * as a result of this method.  This may return false if
 * no suitable component was found to shift focus to.
 * (If you press the right arrow key on the right-most
 * component, for example.)
 */
public static boolean shiftFocus(int dx, int dy, Component src) {
    if (dx == 0 && dy == 0) //this would result in an infinite loop
    {
        throw new IllegalArgumentException("dx (" + dx + ") and (" + dy + ") cannot both be zero");
    }

    Set<Component> focusableComponents = getFocusableComponents(src);

    int x = src.getWidth() / 2;
    int y = src.getHeight() / 2;
    Window window = SwingUtilities.getWindowAncestor(src);
    if (window == null) {
        return false;
    }
    Point p = SwingUtilities.convertPoint(src, x, y, window);

    Component comp = null;
    int windowWidth = window.getWidth();
    int windowHeight = window.getHeight();

    while (p.x > 0 && p.x < windowWidth && p.y > 0 && p.y < windowHeight && (comp == null || comp == src || (comp instanceof JPanel))) {
        p.x += dx;
        p.y += dy;
        comp = SwingUtilities.getDeepestComponentAt(window, p.x, p.y);
        boolean canAcceptFocus = focusableComponents.contains(comp);
        if (comp != null && !canAcceptFocus) {
            comp = null;
        }
    }

    //TODO: implement a more robust searching mechanism instead of the above
    //If a component is below the src, but to the left or right of the center:
    //it should still be detected when you press the down arrow key.

    if (comp != null && comp != src && comp != window && !(comp instanceof JPanel)) {
        comp.requestFocus();
        return true;
    }
    return false;
}