Java Code Examples for javax.swing.JComponent#getInsets()

The following examples show how to use javax.swing.JComponent#getInsets() . 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: FlatToolTipUI.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
@Override
public void paint( Graphics g, JComponent c ) {
	if( isMultiLine( c ) ) {
		FontMetrics fm = c.getFontMetrics( c.getFont() );
		Insets insets = c.getInsets();

		FlatUIUtils.setRenderingHints( (Graphics2D) g );
		g.setColor( c.getForeground() );

		List<String> lines = StringUtils.split( ((JToolTip)c).getTipText(), '\n' );

		int x = insets.left + 3;
		int x2 = c.getWidth() - insets.right - 3;
		int y = insets.top - fm.getDescent();
		int lineHeight = fm.getHeight();
		JComponent comp = ((JToolTip)c).getComponent();
		boolean leftToRight = (comp != null ? comp : c).getComponentOrientation().isLeftToRight();
		for( String line : lines ) {
			y += lineHeight;
			FlatUIUtils.drawString( c, g, line, leftToRight ? x : x2 - SwingUtilities.computeStringWidth( fm, line ), y );
		}
	} else
		super.paint( HiDPIUtils.createGraphicsTextYCorrection( (Graphics2D) g ), c );
}
 
Example 2
Source File: TextComponentPainter.java    From seaglass with Apache License 2.0 5 votes vote down vote up
/**
 * @param g
 * @param c
 * @param width
 * @param height
 */
private void paintLineSeparator(Graphics2D g, JComponent c, int width, int height) {
    g.setPaint(lineSeparatorEnabled);
    int lineYIncrement = g.getFontMetrics(c.getFont()).getHeight();
    int lineY = lineYIncrement+c.getInsets().top-1;
    while (lineY < height) {
        g.drawLine(c.getInsets().left, lineY, width-c.getInsets().right, lineY);
        lineY += lineYIncrement;
    }
}
 
Example 3
Source File: PaletteUI.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public Dimension getMinimumSize(JComponent c) {
	JPalette p = (JPalette) c;
	int cellHeight = p.getCellHeight();
	int cellWidth = p.getCellWidth();
	Color[][] colors = p.getColors();
	int rowCount = colors.length;
	int columnCount = colors[0].length;
	Insets i = c.getInsets();
	return new Dimension(columnCount * cellWidth + i.left + i.right,
			rowCount * cellHeight + i.top + i.bottom);
}
 
Example 4
Source File: PToolTipUI.java    From PolyGlot with MIT License 5 votes vote down vote up
@Override
public Dimension getPreferredSize(JComponent c) {
    FontMetrics fm = c.getFontMetrics(c.getFont());
    Insets insets = c.getInsets();

    Dimension prefSize = new Dimension(insets.left+insets.right,
                                       insets.top+insets.bottom);
    String text = ((JToolTip)c).getTipText();

    if (text != null && !text.isEmpty()) {
        prefSize.width += fm.stringWidth(text);
        prefSize.height += fm.getHeight();
    }
    return prefSize;
}
 
Example 5
Source File: InsetsEncapsulation.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void test(final JComponent component) {
    final Insets p = component.getInsets();
    p.top += 3;
    if (p.equals(component.getInsets())) {
        throw new RuntimeException("Insets altered by altering Insets!");
    }
}
 
Example 6
Source File: InsetsEncapsulation.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void test(final JComponent component) {
    final Insets p = component.getInsets();
    p.top += 3;
    if (p.equals(component.getInsets())) {
        throw new RuntimeException("Insets altered by altering Insets!");
    }
}
 
Example 7
Source File: StyledButtonUI.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void paint(Graphics graphics, JComponent button) {
	paintBackground(graphics, button);

	// Restore normal look after pressing ends, if needed
	if (button instanceof AbstractButton) {
		ButtonModel model = ((AbstractButton) button).getModel();
		if (!model.isPressed()) {
			// Try to avoid switching borders if the button has none or custom
			// borders
			if (button.getBorder().equals(style.getBorderDown())) {
				button.setBorder(style.getBorder());
			}
		}
		if (model.isRollover()) {
			hilite(graphics, button);
		}

		if (button instanceof JButton) {
			if (((JButton) button).isDefaultButton()) {
				Insets insets = button.getInsets();
				graphics.setColor(style.getShadowColor());
				int width = button.getWidth() - insets.right - insets.left - 3;
				int height = button.getHeight() - insets.top - insets.bottom - 3;
				graphics.drawRect(insets.left + 1, insets.right + 1, width, height);
			}
		}
	}

	super.paint(graphics, button);
}
 
Example 8
Source File: MotifScrollBarUI.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Insets insets = c.getInsets();
    int dx = insets.left + insets.right;
    int dy = insets.top + insets.bottom;
    return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
        ? new Dimension(dx + 11, dy + 33)
        : new Dimension(dx + 33, dy + 11);
}
 
Example 9
Source File: MotifScrollBarUI.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Insets insets = c.getInsets();
    int dx = insets.left + insets.right;
    int dy = insets.top + insets.bottom;
    return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
        ? new Dimension(dx + 11, dy + 33)
        : new Dimension(dx + 33, dy + 11);
}
 
Example 10
Source File: InsetsEncapsulation.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void test(final JComponent component) {
    final Insets p = component.getInsets();
    p.top += 3;
    if (p.equals(component.getInsets())) {
        throw new RuntimeException("Insets altered by altering Insets!");
    }
}
 
Example 11
Source File: InsetsEncapsulation.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void test(final JComponent component) {
    final Insets p = component.getInsets();
    p.top += 3;
    if (p.equals(component.getInsets())) {
        throw new RuntimeException("Insets altered by altering Insets!");
    }
}
 
Example 12
Source File: MetalViewTabDisplayerUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Dimension getPreferredSize(JComponent c) {
    FontMetrics fm = getTxtFontMetrics();
    int height = fm == null ?
            21 : fm.getAscent() + 2 * fm.getDescent() + 4;
    Insets insets = c.getInsets();
    prefSize.height = height + insets.bottom + insets.top;
    return prefSize;
}
 
Example 13
Source File: MotifScrollBarUI.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Insets insets = c.getInsets();
    int dx = insets.left + insets.right;
    int dy = insets.top + insets.bottom;
    return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
        ? new Dimension(dx + 11, dy + 33)
        : new Dimension(dx + 33, dy + 11);
}
 
Example 14
Source File: MotifScrollBarUI.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Insets insets = c.getInsets();
    int dx = insets.left + insets.right;
    int dy = insets.top + insets.bottom;
    return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
        ? new Dimension(dx + 11, dy + 33)
        : new Dimension(dx + 33, dy + 11);
}
 
Example 15
Source File: MotifScrollBarUI.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Insets insets = c.getInsets();
    int dx = insets.left + insets.right;
    int dy = insets.top + insets.bottom;
    return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
        ? new Dimension(dx + 11, dy + 33)
        : new Dimension(dx + 33, dy + 11);
}
 
Example 16
Source File: FlatScrollPaneUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
@Override
public void update( Graphics g, JComponent c ) {
	if( c.isOpaque() ) {
		FlatUIUtils.paintParentBackground( g, c );

		// paint background so that corners have same color as scroll bars
		Insets insets = c.getInsets();
		g.setColor( c.getBackground() );
		g.fillRect( insets.left, insets.top,
			c.getWidth() - insets.left - insets.right,
			c.getHeight() - insets.top - insets.bottom );
	}

	paint( g, c );
}
 
Example 17
Source File: MotifScrollBarUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    Insets insets = c.getInsets();
    int dx = insets.left + insets.right;
    int dy = insets.top + insets.bottom;
    return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
        ? new Dimension(dx + 11, dy + 33)
        : new Dimension(dx + 33, dy + 11);
}
 
Example 18
Source File: BasicLinkButtonUI.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public void paint(Graphics g, JComponent c) {
  AbstractButton b = (AbstractButton)c;
  ButtonModel model = b.getModel();

  FontMetrics fm = g.getFontMetrics();

  Insets i = c.getInsets();

  viewRect.x = i.left;
  viewRect.y = i.top;
  viewRect.width = b.getWidth() - (i.right + viewRect.x);
  viewRect.height = b.getHeight() - (i.bottom + viewRect.y);

  textRect.x = textRect.y = textRect.width = textRect.height = 0;
  iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;

  Font f = c.getFont();
  g.setFont(f);

  // layout the text and icon
  String text =
    SwingUtilities.layoutCompoundLabel(
      c,
      fm,
      b.getText(),
      b.getIcon(),
      b.getVerticalAlignment(),
      b.getHorizontalAlignment(),
      b.getVerticalTextPosition(),
      b.getHorizontalTextPosition(),
      viewRect,
      iconRect,
      textRect,
      b.getText() == null ? 0 : b.getIconTextGap());

  clearTextShiftOffset();

  // perform UI specific press action, e.g. Windows L&F shifts text
  if (model.isArmed() && model.isPressed()) {
    paintButtonPressed(g, b);
  }

  // Paint the Icon
  if (b.getIcon() != null) {
    paintIcon(g, c, iconRect);
  }

  Composite oldComposite = ((Graphics2D)g).getComposite();

  if (model.isRollover()) {
    ((Graphics2D)g).setComposite(
      AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
  }

  if (text != null && !text.equals("")) {
    View v = (View)c.getClientProperty(BasicHTML.propertyKey);
    if (v != null) {
      textRect.x += getTextShiftOffset();
      textRect.y += getTextShiftOffset();
      v.paint(g, textRect);
      textRect.x -= getTextShiftOffset();
      textRect.y -= getTextShiftOffset();
    } else {
      paintText(g, b, textRect, text);
    }
  }

  if (b.isFocusPainted() && b.hasFocus()) {
    // paint UI specific focus
    paintFocus(g, b, viewRect, textRect, iconRect);
  }

  ((Graphics2D)g).setComposite(oldComposite);
}
 
Example 19
Source File: ToggleButtonUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c) {
	AbstractButton b = (AbstractButton) c;

	Dimension size = b.getSize();
	FontMetrics fm = g.getFontMetrics();

	Insets i = c.getInsets();

	Rectangle viewRect = new Rectangle(size);

	viewRect.x += i.left;
	viewRect.y += i.top;
	viewRect.width -= i.right + viewRect.x;
	viewRect.height -= i.bottom + viewRect.y;

	Rectangle iconRect = new Rectangle();
	Rectangle textRect = new Rectangle();

	Font f = c.getFont();
	g.setFont(f);

	String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(), b.getIcon(), b.getVerticalAlignment(),
			b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect,
			textRect, b.getText() == null ? 0 : b.getIconTextGap());

	g.setColor(b.getBackground());

	if (b.isContentAreaFilled()) {
		if (RapidLookTools.isToolbarButton(b)) {
			RapidLookTools.drawToolbarButton(g, b);
		} else {
			RapidLookTools.drawButton(b, g, RapidLookTools.createShapeForButton(b));
		}
	}

	if (b.getIcon() != null) {
		paintIcon(g, b, iconRect);
	}

	if (text != null && !text.equals("")) {
		View v = (View) c.getClientProperty(BasicHTML.propertyKey);
		if (v != null) {
			v.paint(g, textRect);
		} else {
			paintText(g, b, textRect, text);
		}
	}

	if (b.isFocusPainted() && b.hasFocus()) {
		paintFocus(g, b, viewRect, textRect, iconRect);
	}

	if (!RapidLookTools.isToolbarButton(b)) {
		if (b.isBorderPainted()) {
			RapidLookTools.drawButtonBorder(b, g, RapidLookTools.createBorderShapeForButton(b));
		}
	}
}
 
Example 20
Source File: PToolTipUI.java    From PolyGlot with MIT License 4 votes vote down vote up
/**
 * To override font, override createToolTip() on component creating tooltip.
 * On creation of ToolTip, change to desired font.
 * @param g
 * @param c 
 */
@Override
public void paint(Graphics g, JComponent c) {
    String tipText = ((JToolTip)c).getTipText();
    tipText = tipText == null ? "" : tipText;
    String[] tipLines = tipText.split("\n");
    Font font = c.getFont();
    
    g.setFont(font);
    
    FontMetrics metrics = g.getFontMetrics();
    int fontHeight = metrics.getHeight();
    int height = (fontHeight * tipLines.length) + 2;
    int width = this.getWidestStringText(tipLines, metrics) + 10;
    
    int fontSize = font.getSize();
    fontSize = fontSize == 0 ? PGTUtil.DEFAULT_FONT_SIZE.intValue() : fontSize;
    c.setFont(font.deriveFont(fontSize));
    ((JToolTip)c).setTipText(tipText);
    
    
    Dimension size = new Dimension(width, height);
    
    c.setSize(size);
    c.getParent().setSize(size);
    
    Insets insets = c.getInsets();
    Rectangle paintTextR = new Rectangle(
        insets.left,
        insets.top,
        size.width - (insets.left + insets.right),
        size.height - (insets.top + insets.bottom));
    
    ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
    g.setColor(Color.black);
    g.fillRect(insets.left,
        insets.top,
        size.width - (insets.left + insets.right),
        size.height - (insets.top + insets.bottom));
    
    g.setColor(Color.white);
    
    for (int i = 0 ; i < tipLines.length; i++) {
        g.drawString(tipLines[i], paintTextR.x + 5,
                paintTextR.y + metrics.getAscent() + (i * (fontHeight)));
    }
}