Example usage for javax.swing.text DefaultCaret NEVER_UPDATE

List of usage examples for javax.swing.text DefaultCaret NEVER_UPDATE

Introduction

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

Prototype

int NEVER_UPDATE

To view the source code for javax.swing.text DefaultCaret NEVER_UPDATE.

Click Source Link

Document

Indicates that the caret should remain at the same absolute position in the document regardless of any document updates, except when the document length becomes less than the current caret position due to removal.

Usage

From source file:org.zephyrsoft.sdb2.presenter.SongView.java

/**
 * Private constructor: only the builder may call it.
 *//*from  www. ja  va2 s  .co  m*/
private SongView(Builder builder) {
    song = builder.song;
    showTitle = builder.showTitle;
    showChords = builder.showChords;
    titleFont = builder.titleFont;
    lyricsFont = builder.lyricsFont;
    translationFont = builder.translationFont;
    copyrightFont = builder.copyrightFont;
    topMargin = builder.topMargin;
    leftMargin = builder.leftMargin;
    rightMargin = builder.rightMargin;
    bottomMargin = builder.bottomMargin;
    titleLyricsDistance = builder.titleLyricsDistance;
    lyricsCopyrightDistance = builder.lyricsCopyrightDistance;
    foregroundColor = builder.foregroundColor;
    backgroundColor = builder.backgroundColor;

    text = new JTextPane();
    ((DefaultCaret) text.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    text.setRequestFocusEnabled(false);
    text.setEditable(false);
    text.setEnabled(false);

    setLayout(new BorderLayout());
    add(text, BorderLayout.CENTER);
    setBackground(backgroundColor);
    setOpaque(true);
    text.setForeground(foregroundColor);
    text.setDisabledTextColor(foregroundColor);

    text.setBorder(BorderFactory.createEmptyBorder(topMargin, leftMargin, 0, rightMargin));

    render();

    // workaround for Nimbus L&F:
    text.setOpaque(false);
    text.setBackground(new Color(0, 0, 0, 0));
}

From source file:processing.app.EditorTab.java

/**
 * Replace the entire contents of this tab.
 *///  w  ww.  j  a  v  a2s.  c o  m
public void setText(String what) {
    // Remove all highlights, since these will all end up at the start of the
    // text otherwise. Preserving them is tricky, so better just remove them.
    textarea.removeAllLineHighlights();
    // Set the caret update policy to NEVER_UPDATE while completely replacing
    // the current text. Normally, the caret tracks inserts and deletions, but
    // replacing the entire text will always make the caret end up at the end,
    // which isn't really useful. With NEVER_UPDATE, the caret will just keep
    // its absolute position (number of characters from the start), which isn't
    // always perfect, but the best we can do without making a diff of the old
    // and new text and some guesswork.
    // Note that we cannot use textarea.setText() here, since that first removes
    // text and then inserts the new text. Even with NEVER_UPDATE, the caret
    // always makes sure to stay valid, so first removing all text makes it
    // reset to 0. Also note that simply saving and restoring the caret position
    // will work, but then the scroll position might change in response to the
    // caret position.
    DefaultCaret caret = (DefaultCaret) textarea.getCaret();
    int policy = caret.getUpdatePolicy();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    try {
        Document doc = textarea.getDocument();
        int oldLength = doc.getLength();
        // The undo manager already seems to group the insert and remove together
        // automatically, but better be explicit about it.
        textarea.beginAtomicEdit();
        try {
            doc.insertString(oldLength, what, null);
            doc.remove(0, oldLength);
        } catch (BadLocationException e) {
            System.err.println("Unexpected failure replacing text");
        } finally {
            textarea.endAtomicEdit();
        }
    } finally {
        caret.setUpdatePolicy(policy);
    }
}