Example usage for javax.swing.text Document getLength

List of usage examples for javax.swing.text Document getLength

Introduction

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

Prototype

public int getLength();

Source Link

Document

Returns number of characters of content currently in the document.

Usage

From source file:HTML.java

/**
 * Utility method to convert HTML to text.
 * @param html The string containing HTML.
 * @return a String containing the derived text .
 *//*w ww .java 2 s.c  o  m*/
public static final String html2text(String html) {
    EditorKit kit = new HTMLEditorKit();
    Document doc = kit.createDefaultDocument();
    doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
    try {
        Reader reader = new StringReader(html);
        kit.read(reader, doc, 0);
        return doc.getText(0, doc.getLength());
    } catch (Exception e) {

        return "";
    }
}

From source file:MainClass.java

protected void append(String s, AttributeSet attributes) {
    Document d = textPane.getDocument();
    try {/*from  www  . ja v  a  2 s  .  c  o  m*/
        d.insertString(d.getLength(), s, attributes);
    } catch (BadLocationException ble) {
    }
}

From source file:Main.java

public Main() {
    textField.setText("Hit Enter to Add Text to Text Pane");
    getContentPane().add(textField, BorderLayout.NORTH);
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Document doc = textPane.getDocument();
                doc.insertString(doc.getLength(), " " + textField.getText(), null);
                textField.setText("");//clear
                Dimension d = textPane.getPreferredSize();
                Rectangle r = textPane.modelToView(textPane.getDocument().getLength());
                d.height = r.y + r.height;
                textPane.setPreferredSize(d);
                getContentPane().validate();
            } catch (Exception e2) {
            }/*from  w ww.  ja  v a  2 s  . co  m*/
        }
    });

    JPanel south = new JPanel();
    textPane = new JTextPane();
    textPane.setText("Some \ntext");
    textPane.setEditable(false);
    textPane.setPreferredSize(new Dimension(120, 23));

    south.add(textPane);
    getContentPane().add(south, BorderLayout.SOUTH);
}

From source file:MainClass.java

public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {

    Document doc = fb.getDocument();
    int currentLength = doc.getLength();
    String currentContent = doc.getText(0, currentLength);
    String before = currentContent.substring(0, offset);
    String after = currentContent.substring(length + offset, currentLength);
    String newValue = before + after;
    currentValue = checkInput(newValue, offset);
    fb.remove(offset, length);/*from   w ww. ja  va  2  s  . c om*/
}

From source file:MainClass.java

public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
        throws BadLocationException {

    Document doc = fb.getDocument();
    int currentLength = doc.getLength();
    String currentContent = doc.getText(0, currentLength);
    String before = currentContent.substring(0, offset);
    String after = currentContent.substring(length + offset, currentLength);
    String newValue = before + (text == null ? "" : text) + after;
    currentValue = checkInput(newValue, offset);
    fb.replace(offset, length, text, attrs);
}

From source file:MainClass.java

public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr)
        throws BadLocationException {

    if (string == null) {
        return;// ww w .  j  a v a  2 s. c  o  m
    } else {
        String newValue;
        Document doc = fb.getDocument();
        int length = doc.getLength();
        if (length == 0) {
            newValue = string;
        } else {
            String currentContent = doc.getText(0, length);
            StringBuffer currentBuffer = new StringBuffer(currentContent);
            currentBuffer.insert(offset, string);
            newValue = currentBuffer.toString();
        }
        currentValue = checkInput(newValue, offset);
        fb.insertString(offset, string, attr);
    }
}

From source file:Main.java

public void highLight(JTextComponent component, String patteren) {
    try {//from   w  w  w. jav a 2s  .  c  om
        Document doc = component.getDocument();
        String text = component.getText(0, doc.getLength());
        int pos = component.getCaretPosition();
        if (pos == doc.getLength()) {
            pos = 0;
        }
        int index = text.toUpperCase().indexOf(patteren.toUpperCase(), pos);
        if (index >= 0) {
            component.setSelectionStart(index);
            component.setSelectionEnd(index + patteren.length());
            component.getCaret().setSelectionVisible(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public Main() {
    editorPane.setDocument(doc);/*from   ww  w. j  av  a2  s.c  om*/
    editorPane.setEditorKit(styledEditorKit);
    JScrollPane scrollpane = new JScrollPane(editorPane);
    scrollpane.setPreferredSize(new Dimension(500, 400));
    JPanel comboPanel = new JPanel();
    comboPanel.add(fontBox);

    setLayout(new BorderLayout());
    add(scrollpane, BorderLayout.CENTER);
    add(comboPanel, BorderLayout.SOUTH);

    Document doc = editorPane.getDocument();
    for (int i = 0; i < 20; i++) {
        int offset = doc.getLength();
        String str = "This is line number: " + i + "\n";
        try {
            doc.insertString(offset, str, null);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    fontBox.addActionListener(e -> {
        int size = (Integer) fontBox.getSelectedItem();
        Action fontAction = new StyledEditorKit.FontSizeAction(String.valueOf(size), size);
        fontAction.actionPerformed(e);
    });
}

From source file:AppendingTextPane.java

public void appendText(String text) {
    try {/*w w w.j  a v  a2s. c om*/
        Document doc = getDocument();

        // Move the insertion point to the end
        setCaretPosition(doc.getLength());

        // Insert the text
        replaceSelection(text);

        // Convert the new end location
        // to view co-ordinates
        Rectangle r = modelToView(doc.getLength());

        // Finally, scroll so that the new text is visible
        if (r != null) {
            scrollRectToVisible(r);
        }
    } catch (BadLocationException e) {
        System.out.println("Failed to append text: " + e);
    }
}

From source file:hr.fer.zemris.vhdllab.view.LogHistoryView.java

private void setupLogAppender(final JTextPane textPane) {
    Logger.getRootLogger().addAppender(new AppenderSkeleton() {
        @Override/* www.java 2s.co m*/
        protected void append(LoggingEvent event) {
            if (!event.getLevel().equals(Level.INFO)) {
                return;
            }

            StringBuilder sb = new StringBuilder();
            String time = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(event.timeStamp);
            sb.append(time).append("  ").append(event.getMessage());

            Document document = textPane.getDocument();
            int documentLength = document.getLength();
            try {
                document.insertString(documentLength, sb.toString() + "\n", null);
            } catch (BadLocationException e) {
                throw new IllegalStateException(e);
            }
            // scroll to end of document
            textPane.setCaretPosition(documentLength);
        }

        @Override
        public boolean requiresLayout() {
            return false;
        }

        @Override
        public void close() {
        }
    });
}