Example usage for javax.swing JTextPane getFontMetrics

List of usage examples for javax.swing JTextPane getFontMetrics

Introduction

In this page you can find the example usage for javax.swing JTextPane getFontMetrics.

Prototype

public FontMetrics getFontMetrics(Font font) 

Source Link

Document

Gets the FontMetrics for the specified Font.

Usage

From source file:Main.java

public static void main(String[] args) {
    final JTextPane textPane = new JTextPane();
    final JScrollPane scrollPane = new JScrollPane(textPane);

    String text = "Lorem ipsum dolor sit amet, " + "consectetur adipiscing elit."
            + "Fusce nec sapien id diam consequat adipiscing.";
    textPane.setText(text);/*from  ww  w. ja  v a2  s. c  om*/

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(scrollPane);

    frame.setSize(new Dimension(200, 200));
    frame.setVisible(true);

    FontMetrics metrics = textPane.getFontMetrics(textPane.getFont());
    textPane.setMargin(new Insets(scrollPane.getViewport().getHeight() - metrics.getHeight(), 0, 0, 0));
}

From source file:Main.java

public static void setTabs(JTextPane textPane, int charactersPerTab) {
    FontMetrics fm = textPane.getFontMetrics(textPane.getFont());
    int charWidth = fm.charWidth('w');
    int tabWidth = charWidth * charactersPerTab;

    TabStop[] tabs = new TabStop[5];

    for (int i = 0; i < tabs.length; i++) {
        int tab = i + 1;
        tabs[i] = new TabStop(tab * tabWidth);
    }//from  w  ww . ja  va2s  .  c om

    TabSet tabSet = new TabSet(tabs);
    SimpleAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setTabSet(attributes, tabSet);
    int length = textPane.getDocument().getLength();
    textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
}

From source file:org.colombbus.tangara.EditorFrame.java

/**
 * This method changes the standard size of a TAB for a given JTextPane.
 *
 * @param the//  w  ww  . j  av a  2  s  .co  m
 *            JTextPane where to apply this method.
 */
public void setTabSizeOf(JTextPane textPane) {
    int spacesPerTab = tabSize;
    FontMetrics fm = textPane.getFontMetrics(textPane.getFont());
    int charWidth = fm.charWidth(' ');
    int tabWidth = charWidth * spacesPerTab;

    TabStop[] tabStops = new TabStop[200];

    for (int j = 0; j < tabStops.length; j++) {
        int tab = j + 1;
        tabStops[j] = new TabStop(tab * tabWidth);
    }

    TabSet tabSet = new TabSet(tabStops);

    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabSet);
    textPane.setLogicalStyle(style);
}

From source file:org.geopublishing.atlasViewer.GpCoreUtil.java

public static void setTabs(final JTextPane textPane, final int charactersPerTab) {
    final FontMetrics fm = textPane.getFontMetrics(textPane.getFont());
    final int charWidth = fm.charWidth('w');
    final int tabWidth = charWidth * charactersPerTab;

    final TabStop[] tabs = new TabStop[10];

    for (int j = 0; j < tabs.length; j++) {
        final int tab = j + 1;
        tabs[j] = new TabStop(tab * tabWidth);
    }/*from w  w w .j a  v  a  2s.co  m*/

    final TabSet tabSet = new TabSet(tabs);
    final SimpleAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setTabSet(attributes, tabSet);
    final int length = textPane.getDocument().getLength();
    textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
}