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

The following examples show how to use javax.swing.JComponent#getFont() . 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: bug8132119.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void testStringWidth() {

        String str = "12345678910\u036F";
        JComponent comp = createComponent(str);
        Font font = comp.getFont();
        FontMetrics fontMetrics = comp.getFontMetrics(font);
        float stringWidth = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);

        if (stringWidth == fontMetrics.stringWidth(str)) {
            throw new RuntimeException("Numeric shaper is not used!");
        }

        if (stringWidth != getLayoutWidth(str, font, NUMERIC_SHAPER)) {
            throw new RuntimeException("Wrong text width!");
        }
    }
 
Example 2
Source File: MultiLineToolTipUI.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
  Font font = c.getFont();
  String tipText = ((JToolTip) c).getTipText();
  JToolTip mtt = (JToolTip) c;
  FontMetrics fontMetrics = mtt.getFontMetrics(font);
  int fontHeight = fontMetrics.getHeight();

  if (tipText == null)
    return new Dimension(0, 0);

  String lines[] = tipText.split("\n");
  int num_lines = lines.length;

  int width, height, onewidth;
  height = num_lines * fontHeight;
  width = 0;
  for (int i = 0; i < num_lines; i++) {
    onewidth = fontMetrics.stringWidth(lines[i]);
    width = Math.max(width, onewidth);
  }
  return new Dimension(width + inset * 2, height + inset * 2);
}
 
Example 3
Source File: MultiLineToolTipUI.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
public void paint(Graphics g, JComponent c) {
  Font font = c.getFont();
  JToolTip mtt = (JToolTip) c;
  FontMetrics fontMetrics = mtt.getFontMetrics(font);
  Dimension dimension = c.getSize();
  int fontHeight = fontMetrics.getHeight();
  int fontAscent = fontMetrics.getAscent();
  String tipText = ((JToolTip) c).getTipText();
  if (tipText == null)
    return;
  String lines[] = tipText.split("\n");
  int num_lines = lines.length;
  int height;
  int i;

  g.setColor(c.getBackground());
  g.fillRect(0, 0, dimension.width, dimension.height);
  g.setColor(c.getForeground());
  for (i = 0, height = 2 + fontAscent; i < num_lines; i++, height += fontHeight) {
    g.drawString(lines[i], inset, height);
  }
}
 
Example 4
Source File: DataTable.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
  // value is column name
  String name = (value==null) ? "" : value.toString(); //$NON-NLS-1$
  textLine.setText(name);
  if (OSPRuntime.isMac()) {
  	name = TeXParser.removeSubscripting(name);
  }
  Component c = renderer.getTableCellRendererComponent(table, name, isSelected, hasFocus, row, col);
  if (!(c instanceof JComponent)) {
    return c;
  }
  JComponent comp = (JComponent) c;
  int sortCol = decorator.getSortedColumn();
  Font font = comp.getFont();
  if (OSPRuntime.isMac()) {
  	// textline doesn't work on OSX
    comp.setFont((sortCol!=convertColumnIndexToModel(col))? 
    		font.deriveFont(Font.PLAIN) : 
    		font.deriveFont(Font.BOLD));
    if (comp instanceof JLabel) {
    	((JLabel)comp).setHorizontalAlignment(SwingConstants.CENTER);
    }
    return comp;
  }
  java.awt.Dimension dim = comp.getPreferredSize();
  dim.height += 1;
  panel.setPreferredSize(dim);
  javax.swing.border.Border border = comp.getBorder();
  if (border instanceof javax.swing.border.EmptyBorder) {
    border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
  }
  panel.setBorder(border);
  // set font: bold if sorted column
  textLine.setFont((sortCol!=convertColumnIndexToModel(col)) ? font : font.deriveFont(Font.BOLD));
  textLine.setColor(comp.getForeground());
  textLine.setBackground(comp.getBackground());
  panel.setBackground(comp.getBackground());
  return panel;
}
 
Example 5
Source File: Coloring.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Apply this coloring to component colors/font.
* The underline and strikeThrough line colors have no effect here.
*/
public void apply(JComponent c) {
    // Possibly change font
    if (font != null) {
        if (fontMode == FONT_MODE_DEFAULT) {
            c.setFont(font);

        } else { // non-default font-mode
            Font origFont = c.getFont();
            if (origFont != null) {
                synchronized (cacheLock) {
                    Font f = (Font)fontAndForeColorCache.get(origFont);
                    if (f == null) {
                        f = modifyFont(origFont);
                        fontAndForeColorCache.put(origFont, f);
                    }
                    c.setFont(f);
                }
            }
        }
    }

    // Possibly change fore-color
    if (foreColor != null) {
        if (!hasAlpha(foreColor)) {
            c.setForeground(foreColor);

        } else { // non-default fore color-mode
            Color origForeColor = c.getForeground();
            if (origForeColor != null) {
                synchronized (cacheLock) {
                    Color fc = (Color)fontAndForeColorCache.get(origForeColor);
                    if (fc == null) {
                        fc = modifyForeColor(origForeColor);
                        fontAndForeColorCache.put(origForeColor, fc);
                    }
                    c.setForeground(fc);
                }
            }
        }
    }

    // Possibly change back-color
    if (backColor != null) {
        if (!hasAlpha(backColor)) {
            c.setBackground(backColor);

        } else { // non-default back color-mode
            Color origBackColor = c.getBackground();
            if (origBackColor != null) {
                synchronized (cacheLock) {
                    Color bc = (Color)backColorCache.get(origBackColor);
                    if (bc == null) {
                        bc = modifyBackColor(origBackColor);
                        backColorCache.put(origBackColor, bc);
                    }
                    c.setBackground(bc);
                }
            }
        }
    }
}
 
Example 6
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 7
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 8
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)));
    }
}
 
Example 9
Source File: ViewHelper.java    From FastCopy with Apache License 2.0 4 votes vote down vote up
public static void setFontSizeForComponent(JComponent component, int newFontSize)  {
	Font original = component.getFont();
	Font newFont = original.deriveFont(Float.valueOf(newFontSize));
	component.setFont(newFont);
}