类javax.swing.FocusManager源码实例Demo

下面列出了怎么用javax.swing.FocusManager的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: openjdk-jdk9   文件: FocusTraversal.java
private static void isFocusOwner(Component queriedFocusOwner,
        String direction)
        throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            Component actualFocusOwner
                    = FocusManager.getCurrentManager().getFocusOwner();
            if (actualFocusOwner != queriedFocusOwner) {
                frame.dispose();
                throw new RuntimeException(
                        "Focus component is wrong after " + direction
                        + " direction ");

            }
        }
    });
}
 
源代码2 项目: Digital   文件: GuiTester.java
@Override
public void run(GuiTester guiTester) throws Exception {
    Window activeWindow = FocusManager.getCurrentManager().getActiveWindow();
    if (activeWindow == null || !expectedClass.isAssignableFrom(activeWindow.getClass())) {
        Thread.sleep(1000);
        activeWindow = FocusManager.getCurrentManager().getActiveWindow();
    }
    Assert.assertNotNull("no java window on top!", activeWindow);

    Assert.assertTrue(getClass().getSimpleName()
                    + ": wrong dialog on top! expected: <"
                    + expectedClass.getSimpleName()
                    + "> but was: <"
                    + activeWindow.getClass().getSimpleName()
                    + ">",
            expectedClass.isAssignableFrom(activeWindow.getClass()));
    try {
        checkWindow(guiTester, (W) activeWindow);
    } catch (Exception e) {
        Thread.sleep(1000);
        checkWindow(guiTester, (W) activeWindow);
    }
}
 
public DefaultHeaderComponent( final String title, final Insets insets, final double darkeningFactor ) {
  this.darkeningFactor = darkeningFactor;

  setLayout( new BorderLayout() );
  setOpaque( false );
  setBorder( BorderFactory.createMatteBorder( 0, 0, 1, 0, getDarkerColor( getBackground() ) ) );
  setGradientColors( new Color[] { getDarkerColor( getBackground() ), getBackground() } );
  setDirection( GradientPanel.Direction.DIRECTION_LEFT );

  final JLabel headerLabel = new JLabel( title );
  headerLabel.setBorder( BorderFactory.createEmptyBorder( insets.top, insets.left, insets.bottom, insets.right ) );
  add( headerLabel, BorderLayout.CENTER );

  final FocusManager currentManager = FocusManager.getCurrentManager();
  target = new FocusManagerChangeHandler();
  currentManager.addPropertyChangeListener( PERMANENT_FOCUS_OWNER, target );
}
 
源代码4 项目: consulo   文件: SingleInspectionProfilePanel.java
private static void setConfigPanel(final JPanel configPanelAnchor, final ScopeToolState state) {
  configPanelAnchor.removeAll();
  final JComponent additionalConfigPanel = state.getAdditionalConfigPanel();
  if (additionalConfigPanel != null) {
    final JScrollPane pane = ScrollPaneFactory.createScrollPane(additionalConfigPanel, SideBorder.NONE);
    FocusManager.getCurrentManager().addPropertyChangeListener("focusOwner", new PropertyChangeListener() {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
        if (!(evt.getNewValue() instanceof JComponent)) {
          return;
        }
        final JComponent component = (JComponent)evt.getNewValue();
        if (component.isAncestorOf(pane)) {
          pane.scrollRectToVisible(component.getBounds());
        }
      }
    });
    configPanelAnchor.add(pane);
  }
  UIUtil.setEnabled(configPanelAnchor, state.isEnabled(), true);
}
 
源代码5 项目: darklaf   文件: DarkPanelPopupUI.java
@Override
public void eventDispatched(final AWTEvent event) {
    if (event.getID() == FocusEvent.FOCUS_GAINED) {
        Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
        if (focusOwner instanceof JTabFrame) return;
        if (focusOwner instanceof JRootPane) return;
        boolean focus = DarkUIUtil.hasFocus(popupComponent);
        if (popupComponent.getTabFrame() != null) {
            Container container = popupComponent.getTabFrame().getContentPane()
                                                .getContainer(popupComponent.getAlignment());
            focus = focus || DarkUIUtil.hasFocus(container);
        }
        setHeaderBackground(focus);
    }
}
 
源代码6 项目: CrossMobile   文件: SwingTextFieldWrapper.java
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (placeholder != null && !placeholder.isEmpty() && getText().isEmpty() && !(FocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == this)) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setColor(new Color(0x80000000, true));
        int deltaY = (int) ((getIOSWidget().frame().getSize().getHeight() - placeholderMetrics.getHeight()) / 2f);
        TextLayout layout = new TextLayout(placeholder, g2.getFont(), g2.getFontRenderContext());
        g2.drawString(placeholder, 5, deltaY + layout.getAscent());
        g2.dispose();
    }
}
 
源代码7 项目: ghidra   文件: DockableComponent.java
private Component findFocusedComponent() {
	if (focusedComponent != null && focusedComponent.isShowing()) {
		return focusedComponent;
	}

	DefaultFocusManager dfm = (DefaultFocusManager) FocusManager.getCurrentManager();
	Component component = dfm.getComponentAfter(this, this);

	// component must be a child of this DockableComponent
	if (component != null && SwingUtilities.isDescendingFrom(component, this)) {
		return component;
	}
	return null;
}
 
源代码8 项目: netbeans   文件: ResultView.java
public boolean isFocused() {
    ResultViewPanel rvp = getCurrentResultViewPanel();
    if (rvp != null) {
        Component owner = FocusManager.getCurrentManager().getFocusOwner();
        return owner != null && SwingUtilities.isDescendingFrom(owner, rvp);
    } else {
        return false;
    }
}
 
源代码9 项目: netbeans   文件: NbPresenter.java
/** Requests focus for <code>currentMessage</code> component.
 * If it is of <code>JComponent</code> type it tries default focus
 * request first. */
private void requestFocusForMessage() {
    Component comp = currentMessage;

    if(comp == null) {
        return;
    }

    if (/*!Constants.AUTO_FOCUS &&*/ FocusManager.getCurrentManager().getActiveWindow() == null) {
        // Do not steal focus if no Java window have it
        Component defComp = null;
        Container nearestRoot =
            (comp instanceof Container && ((Container) comp).isFocusCycleRoot()) ? (Container) comp : comp.getFocusCycleRootAncestor();
        if (nearestRoot != null) {
            defComp = nearestRoot.getFocusTraversalPolicy().getDefaultComponent(nearestRoot);
        }
        if (defComp != null) {
            defComp.requestFocusInWindow();
        } else {
            comp.requestFocusInWindow();
        }
    } else {
        if (!(comp instanceof JComponent)
            || !((JComponent)comp).requestDefaultFocus()) {

            comp.requestFocus();
        }
    }
}
 
源代码10 项目: netbeans   文件: NavigatorController.java
public void actionPerformed (ActionEvent evt) {
    Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
    // move focus away only from navigator AWT children,
    // but not combo box to preserve its ESC functionality
    if (lastActivatedRef == null ||
        focusOwner == null ||
        !SwingUtilities.isDescendingFrom(focusOwner, navigatorTC.getTopComponent()) ||
        focusOwner instanceof JComboBox) {
        return;
    }
    TopComponent prevFocusedTc = lastActivatedRef.get();
    if (prevFocusedTc != null) {
        prevFocusedTc.requestActive();
    }
}
 
源代码11 项目: Digital   文件: ScreenShots.java
@Override
public void run(GuiTester guiTester) throws Exception {
    Window main = FocusManager.getCurrentManager().getActiveWindow();
    while (!(main instanceof Main)) {
        main = (Window) main.getParent();
        if (main == null)
            throw new RuntimeException("Main not found!");
    }

    BufferedImage image = guiTester.getRobot().createScreenCapture(main.getBounds());
    File file = new File(Resources.getRoot().getParentFile().getParentFile().getParentFile(), name);
    ImageIO.write(image, "png", file);
}
 
源代码12 项目: Digital   文件: GuiTester.java
public static Container getBaseContainer() {
    Container baseContainer = FocusManager.getCurrentManager().getActiveWindow();
    if (baseContainer instanceof JDialog)
        baseContainer = ((JDialog) baseContainer).getContentPane();
    else if (baseContainer instanceof JFrame)
        baseContainer = ((JFrame) baseContainer).getContentPane();
    return baseContainer;
}
 
源代码13 项目: Digital   文件: GuiTester.java
@Override
public void run(GuiTester guiTester) throws Exception {
    Point p = new Point(x, y);

    Component t = searchComponent(FocusManager.getCurrentManager().getActiveWindow(), target);
    if (t == null)
        throw new RuntimeException("Component " + target.getSimpleName() + " not found!");

    SwingUtilities.convertPointToScreen(p, t);
    Thread.sleep(100);
    guiTester.getRobot().mouseMove(p.x, p.y);
    Thread.sleep(100);
    cpi.checkColor(guiTester.getRobot().getPixelColor(p.x, p.y));
}
 
源代码14 项目: Digital   文件: GuiTester.java
@Override
public void run(GuiTester gt) throws Exception {
    Component baseContainer = searchComponent(FocusManager.getCurrentManager().getActiveWindow(), target);
    if (baseContainer == null)
        throw new RuntimeException("Component " + target.getSimpleName() + " not found!");

    label.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent keyEvent) {
            if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
                PointerInfo inf = MouseInfo.getPointerInfo();
                Point p = inf.getLocation();
                Color col = gt.getRobot().getPixelColor(p.x, p.y);
                SwingUtilities.convertPointFromScreen(p, baseContainer);
                System.out.print(".add(new GuiTester.ColorPicker(");
                System.out.print(target.getSimpleName());
                System.out.print(".class, ");
                System.out.print(p.x + ", " + p.y);
                System.out.print(", new Color(");
                System.out.print(col.getRed() + "," + col.getGreen() + "," + col.getBlue());
                System.out.println(")))");
            } else if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE)
                System.exit(1);
        }
    });
    setVisible(true);
}
 
源代码15 项目: weblaf   文件: DefaultFocusTracker.java
/**
 * Constructs new {@link DefaultFocusTracker}.
 *
 * @param component         tracked {@link JComponent}
 * @param uniteWithChildren whether or not tracked {@link JComponent} and its children should be counted as a single focusable unit
 */
public DefaultFocusTracker ( @NotNull final JComponent component, final boolean uniteWithChildren )
{
    super ();
    this.component = component;
    this.uniteWithChildren = uniteWithChildren;
    this.focusableChildren = null;
    this.enabled = true;
    this.focused = isInvolved ( component, FocusManager.getCurrentManager ().getFocusOwner () );
}
 
源代码16 项目: weblaf   文件: SwingUtils.java
/**
 * Returns whether component or any of its children has focus or not.
 *
 * @param component component to process
 * @return true if component or any of its children has focus, false otherwise
 */
public static boolean hasFocusOwner ( @Nullable final Component component )
{
    final Component focusOwner = FocusManager.getCurrentManager ().getFocusOwner ();
    return component != null && component == focusOwner ||
            component instanceof Container && ( ( Container ) component ).isAncestorOf ( focusOwner );
}
 
源代码17 项目: netbeans   文件: NotifyExcPanel.java
/** Updates the visual state of the dialog.
*/
private void update () {
    // JST: this can be improved in future...
    boolean isLocalized = current.isLocalized();

    boolean repack;
    boolean visNext = next.isVisible();
    boolean visPrev = previous.isVisible();
    next.setVisible (exceptions.existsNextElement());
    previous.setVisible (exceptions.existsPreviousElement());
    repack = next.isVisible() != visNext || previous.isVisible() != visPrev;

    if (showDetails) {
        Mnemonics.setLocalizedText(details, org.openide.util.NbBundle.getBundle(NotifyExcPanel.class).getString("CTL_Exception_Hide_Details"));
        details.getAccessibleContext().setAccessibleDescription(
            org.openide.util.NbBundle.getBundle(NotifyExcPanel.class).getString("ACSD_Exception_Hide_Details"));
    } else {
        Mnemonics.setLocalizedText(details, org.openide.util.NbBundle.getBundle(NotifyExcPanel.class).getString("CTL_Exception_Show_Details"));
        details.getAccessibleContext().setAccessibleDescription(
            org.openide.util.NbBundle.getBundle(NotifyExcPanel.class).getString("ACSD_Exception_Show_Details"));
    }

    //    setText (current.getLocalizedMessage ());
    String title = org.openide.util.NbBundle.getBundle(NotifyExcPanel.class).getString("CTL_Title_Exception");

    if (showDetails) {
        descriptor.setMessage (this);
        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // XXX #28191: some other piece of code should underline these, etc.
                    StringWriter wr = new StringWriter();
                    current.printStackTrace(new PrintWriter(wr, true));
                    output.setText(wr.toString());
                    output.getCaret().setDot(0);
                    if (!AUTO_FOCUS && FocusManager.getCurrentManager().getActiveWindow() == null) {
                        // Do not steal focus if no Java window have it
                        output.requestFocusInWindow();
                    } else {
                        output.requestFocus ();
                    }
            }
        });
    } else {
        if (isLocalized) {
            String msg = current.getLocalizedMessage ();
            if (msg != null) {
                descriptor.setMessage (msg);
            }
        } else {
            ResourceBundle curBundle = NbBundle.getBundle (NotifyExcPanel.class);
            if (current.getSeverity() == Level.WARNING) {
                // less scary message for warning level
                descriptor.setMessage (
                    java.text.MessageFormat.format(
                        curBundle.getString("NTF_ExceptionWarning"),
                        new Object[] {
                            current.getClassName ()
                        }
                    )
                );
                title = curBundle.getString("NTF_ExceptionWarningTitle"); // NOI18N
            } else {
                // emphasize user-non-friendly exceptions
                //      if (this.getMessage() == null || "".equals(this.getMessage())) { // NOI18N
                descriptor.setMessage (
                    java.text.MessageFormat.format(
                        curBundle.getString("NTF_ExceptionalException"),
                        new Object[] {
                            current.getClassName (),
                            CLIOptions.getLogDir ()
                        }
                    )
                );

                title = curBundle.getString("NTF_ExceptionalExceptionTitle"); // NOI18N
            }
        }
    }

    descriptor.setTitle (title);
    if (repack) {
        dialog.pack();
    }
   
}
 
源代码18 项目: Digital   文件: GuiTester.java
@Override
public void run(GuiTester guiTester) {
    Window activeWindow = FocusManager.getCurrentManager().getActiveWindow();
    Assert.assertNotNull("no java window on top!", activeWindow);
    activeWindow.dispose();
}
 
源代码19 项目: pentaho-reporting   文件: RequestFocusHandler.java
public void componentShown( final ComponentEvent e ) {
  FocusManager.getCurrentManager().focusNextComponent( e.getComponent() );
}
 
源代码20 项目: pentaho-reporting   文件: DefaultHeaderComponent.java
public void dispose() {
  FocusManager.getCurrentManager().removePropertyChangeListener( PERMANENT_FOCUS_OWNER, target );
}
 
源代码21 项目: javamelody   文件: MSwingUtilities.java
/**
 * Retourne le focusOwner permanent.<br/>
 * Le focusOwner permanent est défini comme le dernier Component à avoir reçu un événement FOCUS_GAINED permanent.<br/>
 * Le focusOwner et le focusOwner permanent sont équivalent sauf si un changement temporaire de focus<br/>
 * est en cours. Si c'est le cas, le focusOwner permanent redeviendra &galement<br/>
 * le focusOwner à la fin de ce changement de focus temporaire.
 *
 * @return Component
 */
public static Component getPermanentFocusOwner() {
	// return new DefaultKeyboardFocusManager().getPermanentFocusOwner();
	return FocusManager.getCurrentManager().getPermanentFocusOwner();
}
 
源代码22 项目: javamelody   文件: MSwingUtilities.java
/**
 * Retourne la fenêtre possédant le focus.
 *
 * @return Component
 */
public static Window getFocusedWindow() {
	// return new DefaultKeyboardFocusManager().getFocusedWindow();
	return FocusManager.getCurrentManager().getFocusedWindow();
}
 
 类所在包
 类方法
 同包方法