Java Code Examples for java.awt.event.FocusEvent#getComponent()

The following examples show how to use java.awt.event.FocusEvent#getComponent() . 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: KeyboardFocusManager.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 2
Source File: FileSelector.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public void focusLost(FocusEvent aEvent) {
  if (aEvent.getComponent() == this.field) {
    //only modify file chooser if it has already been created
    if (this.fileChooser != null) {
      String path = this.getSelected();
      if (path.length() == 0) {
        path = System.getProperty("user.dir");
      }
      File file = new File(path);

      if (this.fileChooser.getFileSelectionMode() == JFileChooser.FILES_ONLY && file.isDirectory()) {
        this.fileChooser.setCurrentDirectory(file);
      } else {
        this.fileChooser.setSelectedFile(file);
      }
    }
  }

}
 
Example 3
Source File: KeyboardFocusManager.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 4
Source File: KeyboardFocusManager.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new FocusEvent(source, fe.getID(), temporary, opposite,
                                    FocusEvent.Cause.UNEXPECTED);
    }
}
 
Example 5
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void focusGained(FocusEvent e) {
  JTextComponent textField = (JTextComponent) e.getComponent();
  String str = textField.getText();
  Color col = textField.getForeground();
  if (ghostMessage.equals(str) && INACTIVE_COLOR.equals(col)) {
    textField.setForeground(ORIGINAL_COLOR);
    textField.setText("");
  }
}
 
Example 6
Source File: DefaultKeyboardFocusManager.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private void restoreFocus(FocusEvent fe, Window newFocusedWindow) {
    Component realOppositeComponent = this.realOppositeComponentWR.get();
    Component vetoedComponent = fe.getComponent();

    if (newFocusedWindow != null && restoreFocus(newFocusedWindow,
                                                 vetoedComponent, false))
    {
    } else if (realOppositeComponent != null &&
               doRestoreFocus(realOppositeComponent, vetoedComponent, false)) {
    } else if (fe.getOppositeComponent() != null &&
               doRestoreFocus(fe.getOppositeComponent(), vetoedComponent, false)) {
    } else {
        clearGlobalFocusOwnerPriv();
    }
}
 
Example 7
Source File: FlatSpinnerUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
@Override
public void focusGained( FocusEvent e ) {
	spinner.repaint();

	// if spinner gained focus, transfer it to the editor text field
	if( e.getComponent() == spinner ) {
		JTextField textField = getEditorTextField( spinner.getEditor() );
		if( textField != null )
			textField.requestFocusInWindow();
	}
}
 
Example 8
Source File: FocusWatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
public final void focusGained(final FocusEvent e){
  final Component component = e.getComponent();
  if(e.isTemporary()||!component.isShowing()){
    return;
  }
  setFocusedComponentImpl(component, e);
  setNearestFocusableComponent(component.getParent());
}
 
Example 9
Source File: SelectAllListener.java    From WIFIADB with Apache License 2.0 5 votes vote down vote up
@Override
public void focusGained(FocusEvent e) {
    final Component current = e.getComponent();

    if (current instanceof JTextField){
        ((JTextField) current).selectAll();
    }
}
 
Example 10
Source File: DataDetails.java    From chipster with MIT License 5 votes vote down vote up
public void focusGained(FocusEvent e) {
	if (e.getComponent() == notesField) {
		setNotesActive(true);
		// user starts writing notes, so remove "please add notes" if needed
		if (PLEASE_ADD_NOTES.equals(notesField.getText())) {
			notesField.setSelectionStart(0);
			notesField.setSelectionEnd(notesField.getText().length());
			notesField.setBorder(new LineBorder(Color.gray));
		}
	} else if (e.getComponent() == titleField) {
		setTitleActive(true);
	}
}
 
Example 11
Source File: ChangeMoveDialog.java    From mylizzie with GNU General Public License v3.0 5 votes vote down vote up
private void handleSpinnerMoveNumberEditorFocusGained(FocusEvent e) {
    Component component = e.getComponent();

    if (component instanceof JTextComponent) {
        final JTextComponent textComponent = (JTextComponent) component;
        Lizzie.miscExecutor.schedule(() -> SwingUtilities.invokeLater(textComponent::selectAll), 250, TimeUnit.MILLISECONDS);
    }
}
 
Example 12
Source File: DefaultKeyboardFocusManager.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void restoreFocus(FocusEvent fe, Window newFocusedWindow) {
    Component realOppositeComponent = this.realOppositeComponentWR.get();
    Component vetoedComponent = fe.getComponent();

    if (newFocusedWindow != null && restoreFocus(newFocusedWindow,
                                                 vetoedComponent, false))
    {
    } else if (realOppositeComponent != null &&
               doRestoreFocus(realOppositeComponent, vetoedComponent, false)) {
    } else if (fe.getOppositeComponent() != null &&
               doRestoreFocus(fe.getOppositeComponent(), vetoedComponent, false)) {
    } else {
        clearGlobalFocusOwnerPriv();
    }
}
 
Example 13
Source File: SessionDetails.java    From chipster with MIT License 5 votes vote down vote up
public void focusLost(FocusEvent e) {
	// do nothing, content is stored already
	
	if (e.getComponent() == notesField) {
		setNotes(application.getSessionManager().getSessionNotes());
	}
}
 
Example 14
Source File: KeyboardFocusManager.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
static AWTEvent retargetFocusEvent(AWTEvent event) {
    if (clearingCurrentLightweightRequests) {
        return event;
    }

    KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        if (event instanceof FocusEvent || event instanceof WindowEvent) {
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
        if (focusLog.isLoggable(PlatformLogger.Level.FINER) && event instanceof KeyEvent) {
            focusLog.finer("    focus owner is {0}",
                           String.valueOf(manager.getGlobalFocusOwner()));
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
    }

    synchronized(heavyweightRequests) {
        /*
         * This code handles FOCUS_LOST event which is generated by
         * DefaultKeyboardFocusManager for FOCUS_GAINED.
         *
         * This code based on knowledge of DefaultKeyboardFocusManager's
         * implementation and might be not applicable for another
         * KeyboardFocusManager.
         *
         * Fix for 4472032
         */
        if (newFocusOwner != null &&
            event.getID() == FocusEvent.FOCUS_LOST)
        {
            FocusEvent fe = (FocusEvent)event;

            if (manager.getGlobalFocusOwner() == fe.getComponent() &&
                fe.getOppositeComponent() == newFocusOwner)
            {
                newFocusOwner = null;
                return event;
            }
        }
    }

    processCurrentLightweightRequests();

    switch (event.getID()) {
        case FocusEvent.FOCUS_GAINED: {
            event = retargetFocusGained((FocusEvent)event);
            break;
        }
        case FocusEvent.FOCUS_LOST: {
            event = retargetFocusLost((FocusEvent)event);
            break;
        }
        default:
            /* do nothing */
    }
    return event;
}
 
Example 15
Source File: KeyboardFocusManager.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static AWTEvent retargetFocusEvent(AWTEvent event) {
    if (clearingCurrentLightweightRequests) {
        return event;
    }

    KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        if (event instanceof FocusEvent || event instanceof WindowEvent) {
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
        if (focusLog.isLoggable(PlatformLogger.Level.FINER) && event instanceof KeyEvent) {
            focusLog.finer("    focus owner is {0}",
                           String.valueOf(manager.getGlobalFocusOwner()));
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
    }

    synchronized(heavyweightRequests) {
        /*
         * This code handles FOCUS_LOST event which is generated by
         * DefaultKeyboardFocusManager for FOCUS_GAINED.
         *
         * This code based on knowledge of DefaultKeyboardFocusManager's
         * implementation and might be not applicable for another
         * KeyboardFocusManager.
         *
         * Fix for 4472032
         */
        if (newFocusOwner != null &&
            event.getID() == FocusEvent.FOCUS_LOST)
        {
            FocusEvent fe = (FocusEvent)event;

            if (manager.getGlobalFocusOwner() == fe.getComponent() &&
                fe.getOppositeComponent() == newFocusOwner)
            {
                newFocusOwner = null;
                return event;
            }
        }
    }

    processCurrentLightweightRequests();

    switch (event.getID()) {
        case FocusEvent.FOCUS_GAINED: {
            event = retargetFocusGained((FocusEvent)event);
            break;
        }
        case FocusEvent.FOCUS_LOST: {
            event = retargetFocusLost((FocusEvent)event);
            break;
        }
        default:
            /* do nothing */
    }
    return event;
}
 
Example 16
Source File: KeyboardFocusManager.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static FocusEvent retargetFocusGained(FocusEvent fe) {
    assert (fe.getID() == FocusEvent.FOCUS_GAINED);

    Component currentFocusOwner = getCurrentKeyboardFocusManager().
        getGlobalFocusOwner();
    Component source = fe.getComponent();
    Component opposite = fe.getOppositeComponent();
    Component nativeSource = getHeavyweight(source);

    synchronized (heavyweightRequests) {
        HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();

        if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
        {
            return retargetUnexpectedFocusEvent(fe);
        }

        if (source != null && nativeSource == null && hwFocusRequest != null) {
            // if source w/o peer and
            // if source is equal to first lightweight
            // then we should correct source and nativeSource
            if (source == hwFocusRequest.getFirstLightweightRequest().component)
            {
                source = hwFocusRequest.heavyweight;
                nativeSource = source; // source is heavuweight itself
            }
        }
        if (hwFocusRequest != null &&
            nativeSource == hwFocusRequest.heavyweight)
        {
            // Focus change as a result of a known call to requestFocus(),
            // or known click on a peer focusable heavyweight Component.

            heavyweightRequests.removeFirst();

            LightweightFocusRequest lwFocusRequest =
                hwFocusRequest.lightweightRequests.removeFirst();

            Component newSource = lwFocusRequest.component;
            if (currentFocusOwner != null) {
                /*
                 * Since we receive FOCUS_GAINED when current focus
                 * owner is not null, correcponding FOCUS_LOST is supposed
                 * to be lost.  And so,  we keep new focus owner
                 * to determine synthetic FOCUS_LOST event which will be
                 * generated by KeyboardFocusManager for this FOCUS_GAINED.
                 *
                 * This code based on knowledge of
                 * DefaultKeyboardFocusManager's implementation and might
                 * be not applicable for another KeyboardFocusManager.
                 */
                newFocusOwner = newSource;
            }

            boolean temporary = (opposite == null ||
                                 isTemporary(newSource, opposite))
                    ? false
                    : lwFocusRequest.temporary;

            if (hwFocusRequest.lightweightRequests.size() > 0) {
                currentLightweightRequests =
                    hwFocusRequest.lightweightRequests;
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            processCurrentLightweightRequests();
                        }
                    });
            }

            // 'opposite' will be fixed by
            // DefaultKeyboardFocusManager.realOppositeComponent
            return new CausedFocusEvent(newSource,
                                  FocusEvent.FOCUS_GAINED, temporary,
                                  opposite, lwFocusRequest.cause);
        }

        if (currentFocusOwner != null
            && currentFocusOwner.getContainingWindow() == source
            && (hwFocusRequest == null || source != hwFocusRequest.heavyweight))
        {
            // Special case for FOCUS_GAINED in top-levels
            // If it arrives as the result of activation we should skip it
            // This event will not have appropriate request record and
            // on arrival there will be already some focus owner set.
            return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_GAINED, false,
                                        null, CausedFocusEvent.Cause.ACTIVATION);
        }

        return retargetUnexpectedFocusEvent(fe);
    } // end synchronized(heavyweightRequests)
}
 
Example 17
Source File: ListDemo.java    From Darcula with Apache License 2.0 4 votes vote down vote up
public void focusGained(FocusEvent e) {
    JComponent c = (JComponent) e.getComponent();
    c.scrollRectToVisible(new Rectangle(0, 0, c.getWidth(), c.getHeight()));
}
 
Example 18
Source File: KeyboardFocusManager.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static AWTEvent retargetFocusEvent(AWTEvent event) {
    if (clearingCurrentLightweightRequests) {
        return event;
    }

    KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        if (event instanceof FocusEvent || event instanceof WindowEvent) {
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
        if (focusLog.isLoggable(PlatformLogger.Level.FINER) && event instanceof KeyEvent) {
            focusLog.finer("    focus owner is {0}",
                           String.valueOf(manager.getGlobalFocusOwner()));
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
    }

    synchronized(heavyweightRequests) {
        /*
         * This code handles FOCUS_LOST event which is generated by
         * DefaultKeyboardFocusManager for FOCUS_GAINED.
         *
         * This code based on knowledge of DefaultKeyboardFocusManager's
         * implementation and might be not applicable for another
         * KeyboardFocusManager.
         *
         * Fix for 4472032
         */
        if (newFocusOwner != null &&
            event.getID() == FocusEvent.FOCUS_LOST)
        {
            FocusEvent fe = (FocusEvent)event;

            if (manager.getGlobalFocusOwner() == fe.getComponent() &&
                fe.getOppositeComponent() == newFocusOwner)
            {
                newFocusOwner = null;
                return event;
            }
        }
    }

    processCurrentLightweightRequests();

    switch (event.getID()) {
        case FocusEvent.FOCUS_GAINED: {
            event = retargetFocusGained((FocusEvent)event);
            break;
        }
        case FocusEvent.FOCUS_LOST: {
            event = retargetFocusLost((FocusEvent)event);
            break;
        }
        default:
            /* do nothing */
    }
    return event;
}
 
Example 19
Source File: KeyboardFocusManager.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
static FocusEvent retargetFocusGained(FocusEvent fe) {
    assert (fe.getID() == FocusEvent.FOCUS_GAINED);

    Component currentFocusOwner = getCurrentKeyboardFocusManager().
        getGlobalFocusOwner();
    Component source = fe.getComponent();
    Component opposite = fe.getOppositeComponent();
    Component nativeSource = getHeavyweight(source);

    synchronized (heavyweightRequests) {
        HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();

        if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
        {
            return retargetUnexpectedFocusEvent(fe);
        }

        if (source != null && nativeSource == null && hwFocusRequest != null) {
            // if source w/o peer and
            // if source is equal to first lightweight
            // then we should correct source and nativeSource
            if (source == hwFocusRequest.getFirstLightweightRequest().component)
            {
                source = hwFocusRequest.heavyweight;
                nativeSource = source; // source is heavyweight itself
            }
        }
        if (hwFocusRequest != null &&
            nativeSource == hwFocusRequest.heavyweight)
        {
            // Focus change as a result of a known call to requestFocus(),
            // or known click on a peer focusable heavyweight Component.

            heavyweightRequests.removeFirst();

            LightweightFocusRequest lwFocusRequest =
                hwFocusRequest.lightweightRequests.removeFirst();

            Component newSource = lwFocusRequest.component;
            if (currentFocusOwner != null) {
                /*
                 * Since we receive FOCUS_GAINED when current focus
                 * owner is not null, corresponding FOCUS_LOST is supposed
                 * to be lost.  And so,  we keep new focus owner
                 * to determine synthetic FOCUS_LOST event which will be
                 * generated by KeyboardFocusManager for this FOCUS_GAINED.
                 *
                 * This code based on knowledge of
                 * DefaultKeyboardFocusManager's implementation and might
                 * be not applicable for another KeyboardFocusManager.
                 */
                newFocusOwner = newSource;
            }

            boolean temporary = (opposite == null ||
                                 isTemporary(newSource, opposite))
                    ? false
                    : lwFocusRequest.temporary;

            if (hwFocusRequest.lightweightRequests.size() > 0) {
                currentLightweightRequests =
                    hwFocusRequest.lightweightRequests;
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            processCurrentLightweightRequests();
                        }
                    });
            }

            // 'opposite' will be fixed by
            // DefaultKeyboardFocusManager.realOppositeComponent
            return new FocusEvent(newSource,
                                  FocusEvent.FOCUS_GAINED, temporary,
                                  opposite, lwFocusRequest.cause);
        }

        if (currentFocusOwner != null
            && currentFocusOwner.getContainingWindow() == source
            && (hwFocusRequest == null || source != hwFocusRequest.heavyweight))
        {
            // Special case for FOCUS_GAINED in top-levels
            // If it arrives as the result of activation we should skip it
            // This event will not have appropriate request record and
            // on arrival there will be already some focus owner set.
            return new FocusEvent(currentFocusOwner, FocusEvent.FOCUS_GAINED, false,
                                        null, FocusEvent.Cause.ACTIVATION);
        }

        return retargetUnexpectedFocusEvent(fe);
    } // end synchronized(heavyweightRequests)
}
 
Example 20
Source File: KeyboardFocusManager.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
static AWTEvent retargetFocusEvent(AWTEvent event) {
    if (clearingCurrentLightweightRequests) {
        return event;
    }

    KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        if (event instanceof FocusEvent || event instanceof WindowEvent) {
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
        if (focusLog.isLoggable(PlatformLogger.Level.FINER) && event instanceof KeyEvent) {
            focusLog.finer("    focus owner is {0}",
                           String.valueOf(manager.getGlobalFocusOwner()));
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
    }

    synchronized(heavyweightRequests) {
        /*
         * This code handles FOCUS_LOST event which is generated by
         * DefaultKeyboardFocusManager for FOCUS_GAINED.
         *
         * This code based on knowledge of DefaultKeyboardFocusManager's
         * implementation and might be not applicable for another
         * KeyboardFocusManager.
         *
         * Fix for 4472032
         */
        if (newFocusOwner != null &&
            event.getID() == FocusEvent.FOCUS_LOST)
        {
            FocusEvent fe = (FocusEvent)event;

            if (manager.getGlobalFocusOwner() == fe.getComponent() &&
                fe.getOppositeComponent() == newFocusOwner)
            {
                newFocusOwner = null;
                return event;
            }
        }
    }

    processCurrentLightweightRequests();

    switch (event.getID()) {
        case FocusEvent.FOCUS_GAINED: {
            event = retargetFocusGained((FocusEvent)event);
            break;
        }
        case FocusEvent.FOCUS_LOST: {
            event = retargetFocusLost((FocusEvent)event);
            break;
        }
        default:
            /* do nothing */
    }
    return event;
}