Example usage for javax.swing.text Utilities getTabbedTextWidth

List of usage examples for javax.swing.text Utilities getTabbedTextWidth

Introduction

In this page you can find the example usage for javax.swing.text Utilities getTabbedTextWidth.

Prototype

public static final float getTabbedTextWidth(Segment s, FontMetrics metrics, float x, TabExpander e,
        int startOffset) 

Source Link

Document

Determines the width of the given segment of text taking tabs into consideration.

Usage

From source file:Main.java

public Main() {
    textArea.getDocument().addDocumentListener(new DocumentListener() {

        @Override//from  www  .j av a  2  s  .c  o  m
        public void changedUpdate(DocumentEvent e) {
            update(e);
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            update(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            update(e);
        }

        private void update(DocumentEvent e) {
            List<String> lines = getLines(textArea);
            String lastLine = lines.get(lines.size() - 1);
            int tabbedTextWidth = Utilities.getTabbedTextWidth(
                    new Segment(lastLine.toCharArray(), 0, lastLine.length()),
                    textArea.getFontMetrics(textArea.getFont()), 0, null, 0);
            int lineHeight = getLineHeight(textArea);
            if (lines.size() * lineHeight > textArea.getHeight() || tabbedTextWidth > textArea.getWidth()) {
                System.out.println("Too many lines!");
            }
        }
    });

    getContentPane().add(textArea);
}

From source file:FormattedTextFieldExample.java

public float getPreferredSpan(int axis) {
    int widthFormat;
    int widthContent;

    if (formatSize == 0 || axis == View.Y_AXIS) {
        return super.getPreferredSpan(axis);
    }//from   www  .j  ava  2 s. c o  m

    widthFormat = Utilities.getTabbedTextWidth(measureBuff, getFontMetrics(), 0, this, 0);
    widthContent = Utilities.getTabbedTextWidth(contentBuff, getFontMetrics(), 0, this, 0);

    return Math.max(widthFormat, widthContent);
}

From source file:FormattedTextFieldExample.java

public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
    a = adjustAllocation(a);//from   ww  w. j a va2  s.c  om
    Rectangle r = new Rectangle(a.getBounds());
    FontMetrics fm = getFontMetrics();
    r.height = fm.getHeight();

    int oldCount = contentBuff.count;

    if (pos < offsets.length) {
        contentBuff.count = offsets[pos];
    } else {
        // Beyond the end: point to the location
        // after the last model position.
        contentBuff.count = offsets[offsets.length - 1] + 1;
    }

    int offset = Utilities.getTabbedTextWidth(contentBuff, metrics, 0, this, element.getStartOffset());
    contentBuff.count = oldCount;

    r.x += offset;
    r.width = 1;

    return r;
}