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

The following examples show how to use java.awt.Component#hasFocus() . These examples are extracted from open source projects. 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
/**
    * @param c
    *            Component to check on.
    * @return returns true if the component of one of its child has the focus
    */
   public static boolean isAncestorOfFocusedComponent(Component c) {
if (c.hasFocus()) {
    return true;
} else {
    if (c instanceof Container) {
	Container cont = (Container) c;
	int n = cont.getComponentCount();
	for (int i = 0; i < n; i++) {
	    Component child = cont.getComponent(i);
	    if (isAncestorOfFocusedComponent(child))
		return true;
	}
    }
}
return false;
   }
 
Example 2
public void paintBorder(Component c, Graphics g, int x, int y, int width,
		int height) {
	Insets insets = getBorderInsets(c);
	if(c.hasFocus()) {
		Rectangle rect = new Rectangle(x+insets.left, y+insets.top,
				width-insets.left-insets.right-1,
				height-insets.top-insets.bottom-1);
		Graphics2D g2 = (Graphics2D)g.create();
		GeneralPath focusOnly = new GeneralPath(Path2D.WIND_EVEN_ODD);
		focusOnly.append(new Rectangle(x,y,width,height), false);
		focusOnly.append(new Rectangle(x+insets.left,y+insets.top,
				width-insets.left-insets.right,
				height-insets.top-insets.bottom), false);
		g2.clip(focusOnly);
		PlafPaintUtils.paintFocus(g2, rect, 3);
	} else if(unfocusedBorder!=null) {
		Insets borderInsets = unfocusedBorder.getBorderInsets(c);
		unfocusedBorder.paintBorder(c, g, x+insets.left-borderInsets.left, 
				y+insets.top-borderInsets.top, 
				width-insets.left-insets.right+borderInsets.left+borderInsets.right,
				height-insets.top-insets.bottom+borderInsets.top+borderInsets.bottom);
	}
	
}
 
Example 3
Source Project: Spark   File: GraphicUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
    * @param c
    *            Component to check on.
    * @return returns true if the component of one of its child has the focus
    */
   public static boolean isAncestorOfFocusedComponent(Component c) {
if (c.hasFocus()) {
    return true;
} else {
    if (c instanceof Container) {
	Container cont = (Container) c;
	int n = cont.getComponentCount();
	for (int i = 0; i < n; i++) {
	    Component child = cont.getComponent(i);
	    if (isAncestorOfFocusedComponent(child))
		return true;
	}
    }
}
return false;
   }
 
Example 4
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    if (c.hasFocus()) {
        g.setColor(focus);
        g.drawRect(x, y, w-1, h-1);
    } else {
        g.setColor(control);
        g.drawRect(x, y, w-1, h-1);
    }
}
 
Example 5
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    if (c.hasFocus()) {
        g.setColor(focus);
        g.drawRect(x, y, w-1, h-1);
    } else {
        g.setColor(control);
        g.drawRect(x, y, w-1, h-1);
    }
}
 
Example 6
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 7
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    if (c.hasFocus()) {
        g.setColor(focus);
        g.drawRect(x, y, w-1, h-1);
    } else {
        g.setColor(control);
        g.drawRect(x, y, w-1, h-1);
    }
}
 
Example 8
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 9
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    if (c.hasFocus()) {
        g.setColor(focus);
        g.drawRect(x, y, w-1, h-1);
    } else {
        g.setColor(control);
        g.drawRect(x, y, w-1, h-1);
    }
}
 
Example 10
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 11
Source Project: JDKSourceCode1.8   File: MotifBorders.java    License: MIT License 5 votes vote down vote up
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    if (c.hasFocus()) {
        g.setColor(focus);
        g.drawRect(x, y, w-1, h-1);
    } else {
        g.setColor(control);
        g.drawRect(x, y, w-1, h-1);
    }
}
 
Example 12
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 13
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 14
Source Project: javamoney-examples   File: PopupMenu.java    License: 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 15
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 16
Source Project: Bytecoder   File: MotifBorders.java    License: Apache License 2.0 5 votes vote down vote up
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    if (c.hasFocus()) {
        g.setColor(focus);
        g.drawRect(x, y, w-1, h-1);
    } else {
        g.setColor(control);
        g.drawRect(x, y, w-1, h-1);
    }
}
 
Example 17
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 18
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    if (c.hasFocus()) {
        g.setColor(focus);
        g.drawRect(x, y, w-1, h-1);
    } else {
        g.setColor(control);
        g.drawRect(x, y, w-1, h-1);
    }
}
 
Example 19
private static void setFocus(final Component comp, final Robot r) {
    if (comp.hasFocus()) {
        return;
    }

    Util.clickOnComp(comp, r);
    Util.waitForIdle(r);

    if (!comp.hasFocus()) {
        throw new RuntimeException("can not set focus on " + comp);
    }
}
 
Example 20
Source Project: whyline   File: WhylineControlBorder.java    License: MIT License 4 votes vote down vote up
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

		if(!c.isVisible()) return;
		
		Graphics2D g2 = (Graphics2D)g.create();

		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

		Stroke old = g2.getStroke();

		g2.setStroke(new BasicStroke(UI.getBorderPadding()));
		g2.setColor(UI.getPanelLightColor());
		g2.drawRect(0, 0, width, height);

		int corner = UI.getRoundedness() - 1;
		
		int widthOfControlBackgroundStroke = UI.getBorderPadding() + 1;
		g2.setStroke(new BasicStroke(widthOfControlBackgroundStroke));
		g2.setColor(c.getBackground());
		g2.drawRoundRect(
			widthOfControlBackgroundStroke / 2 - 1, 
			widthOfControlBackgroundStroke / 2 - 1, 
			width - widthOfControlBackgroundStroke + 1, 
			height - widthOfControlBackgroundStroke + 1, 
			corner, corner);

		g2.setStroke(old);

		if(c.hasFocus()) {
			g2.setColor(UI.getFocusColor());
			float strokeWidth = 2.0f;
			g2.setStroke(new BasicStroke(strokeWidth));
			g2.drawRect((int)(strokeWidth / 2), (int)(strokeWidth / 2), (int)(width - strokeWidth - 1), (int)(height - strokeWidth - 1));
		}
		else {
			g2.setColor(c.isEnabled() ? UI.getControlBorderColor() : UI.getControlBorderColor().darker());
			g2.drawRoundRect(0, 0, width - 1, height - 1, corner, corner);
		}
		
	}