Example usage for javax.swing.text Segment Segment

List of usage examples for javax.swing.text Segment Segment

Introduction

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

Prototype

public Segment(char[] array, int offset, int count) 

Source Link

Document

Creates a new segment referring to an existing array.

Usage

From source file:Main.java

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

        @Override//from  ww w. j a v a  2  s .  c  om
        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);
}