Java Code Examples for javax.swing.text.JTextComponent#getInsets()

The following examples show how to use javax.swing.text.JTextComponent#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: FlatTextFieldUI.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
static void paintPlaceholder( Graphics g, JTextComponent c, Color placeholderForeground ) {
	// check whether text component is empty
	if( c.getDocument().getLength() > 0 )
		return;

	// check for JComboBox
	Container parent = c.getParent();
	JComponent jc = (parent instanceof JComboBox) ? (JComboBox<?>) parent : c;

	// get placeholder text
	Object placeholder = jc.getClientProperty( FlatClientProperties.PLACEHOLDER_TEXT );
	if( !(placeholder instanceof String) )
		return;

	// compute placeholder location
	Insets insets = c.getInsets();
	FontMetrics fm = c.getFontMetrics( c.getFont() );
	int x = insets.left;
	int y = insets.top + fm.getAscent() + ((c.getHeight() - insets.top - insets.bottom - fm.getHeight()) / 2);

	// paint placeholder
	g.setColor( placeholderForeground );
	FlatUIUtils.drawString( c, g, (String) placeholder, x, y );
}
 
Example 2
Source File: MaterialComponentField.java    From material-ui-swing with MIT License 6 votes vote down vote up
protected void paintLine(Graphics graphics) {
    if (graphics == null) {
        return;
    }
    JTextComponent c = getComponent();

    if (drawLine) {
        int x = c.getInsets().left;
        int y = c.getInsets().top;
        int w = c.getWidth() - c.getInsets().left - c.getInsets().right;
        if(textComponent.isEnabled()){
            graphics.setColor(colorLine);
        }else{
            graphics.setColor(disabledBackground);
        }

        graphics.fillRect(x, c.getHeight() - y, w, 1);
    }
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void paint(Graphics g, JComponent c) {
  super.paint(g, c);
  if (c instanceof JLayer) {
    JTextComponent tc = (JTextComponent) ((JLayer<?>) c).getView();
    if (!tc.getText().isEmpty()) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setPaint(hint.getForeground());
      Insets i = tc.getInsets();
      Dimension d = hint.getPreferredSize();
      int x = tc.getWidth() - i.right - d.width - 2;
      int y = (tc.getHeight() - d.height) / 2;
      SwingUtilities.paintComponent(g2, hint, tc, x, y, d.width, d.height);
      g2.dispose();
    }
  }
}
 
Example 4
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void paint(Graphics g, JComponent c) {
  super.paint(g, c);
  if (c instanceof JLayer) {
    JTextComponent tc = (JTextComponent) ((JLayer<?>) c).getView();
    if (!tc.getText().isEmpty()) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setPaint(hint.getForeground());
      Insets i = tc.getInsets();
      Dimension d = hint.getPreferredSize();
      int x = tc.getWidth() - i.right - d.width - 2;
      int y = (tc.getHeight() - d.height) / 2;
      SwingUtilities.paintComponent(g2, hint, tc, x, y, d.width, d.height);
      g2.dispose();
    }
  }
}
 
Example 5
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void paint(Graphics g, JComponent c) {
  super.paint(g, c);
  if (c instanceof JLayer) {
    JLayer<?> jlayer = (JLayer<?>) c;
    JTextComponent tc = (JTextComponent) jlayer.getView();
    if (tc.getText().isEmpty() && !tc.hasFocus()) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setPaint(hint.getBackground());
      Insets i = tc.getInsets();
      Dimension d = hint.getPreferredSize();
      SwingUtilities.paintComponent(g2, hint, tc, i.left, i.top, d.width, d.height);
      // int baseline = tc.getBaseline(tc.getWidth(), tc.getHeight());
      // Font font = tc.getFont();
      // FontRenderContext frc = g2.getFontRenderContext();
      // TextLayout tl = new TextLayout(hintMessage, font, frc);
      // tl.draw(g2, i.left + 2, baseline);
      g2.dispose();
    }
  }
}
 
Example 6
Source File: Baseline.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the baseline for single line text components, like
 * <code>JTextField</code>.
 */
private static int getSingleLineTextBaseline(JTextComponent textComponent, int h) {
	View rootView = textComponent.getUI().getRootView(textComponent);
	if (rootView.getViewCount() > 0) {
		Insets insets = textComponent.getInsets();
		int height = h - insets.top - insets.bottom;
		int y = insets.top;
		View fieldView = rootView.getView(0);
		int vspan = (int) fieldView.getPreferredSpan(View.Y_AXIS);
		if (height != vspan) {
			int slop = height - vspan;
			y += slop / 2;
		}
		FontMetrics fm = textComponent.getFontMetrics(textComponent.getFont());
		y += fm.getAscent();
		return y;
	}
	return -1;
}
 
Example 7
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Rectangle getVisibleEditorRect(JTextComponent tc) {
Rectangle alloc = tc.getBounds();
if ((alloc.width > 0) && (alloc.height > 0)) {
    alloc.x = alloc.y = 0;
    Insets insets = tc.getInsets();
    alloc.x += insets.left;
    alloc.y += insets.top;
    alloc.width -= insets.left + insets.right;
    alloc.height -= insets.top + insets.bottom;
    return alloc;
}
return null;
   }
 
Example 8
Source File: HintTextFieldUI.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
protected void paintSafely(Graphics g) {
    super.paintSafely(g);
    JTextComponent component = getComponent();
    if (hintText != null && component.getText().length() == 0 && !component.hasFocus()) {
        g.setColor(JBColor.GRAY);
        final int fontSize = component.getFont().getSize();
        final int padding = (component.getHeight() - fontSize) / 2;
        final int x = component.getInsets().left;
        final int y = component.getHeight() - padding - 1;
        g.drawString(hintText, x, y);
    }
}
 
Example 9
Source File: DarculaTextFieldUI.java    From Darcula with Apache License 2.0 5 votes vote down vote up
protected Rectangle getDrawingRect() {
  final JTextComponent c = getComponent();
  final Insets i = c.getInsets();
  final int x = i.right - 4 - 16;
  final int y = i.top - 3;
  final int w = c.getWidth() - i.left - i.right + 16*2 +7*2  - 5;
  int h = c.getBounds().height - i.top - i.bottom + 4*2 - 3;
  if (h%2==1) h++;
  return new Rectangle(x, y, w, h);
}
 
Example 10
Source File: DarculaTextFieldUI.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected Rectangle getDrawingRect() {
  final JTextComponent c = myTextField;
  final Insets i = c.getInsets();
  final int x = i.right - JBUI.scale(4) - JBUI.scale(16);
  final int y = i.top - JBUI.scale(3);
  final int w = c.getWidth() - (i.right + i.left) + JBUI.scale(16 * 2) + JBUI.scale(7 * 2) - JBUI.scale(5);
  int h = c.getBounds().height - (i.top + i.bottom) + JBUI.scale(4 * 2) - JBUI.scale(3);
  if (h % 2 == 1) h += JBUI.scale(1);
  return new Rectangle(x, y, w, h);
}