Java JTextPane isSelectionBold(JTextPane textPane)

Here you can find the source of isSelectionBold(JTextPane textPane)

Description

Returns true if all of the selection is bold, otherwise false.

License

Apache License

Declaration

public static boolean isSelectionBold(JTextPane textPane) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;

public class Main {
    /**//from ww w  . jav a 2  s. c  o m
     * Returns true if all of the selection is bold, otherwise false.
     */
    public static boolean isSelectionBold(JTextPane textPane) {
        int selectionStart = textPane.getSelectionStart();
        int selectionEnd = textPane.getSelectionEnd();
        StyledDocument doc = (StyledDocument) (textPane.getDocument());
        if (selectionStart == selectionEnd) {
            AttributeSet attr = textPane.getInputAttributes();
            if (!attr.containsAttribute(StyleConstants.Bold, new Boolean(
                    true))) {
                return false;
            }
        } else {
            for (int i = selectionStart; i < selectionEnd; i++) {
                AttributeSet attr = doc.getCharacterElement(i)
                        .getAttributes();
                if (!attr.containsAttribute(StyleConstants.Bold,
                        new Boolean(true))) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. createJTextPane(String text, Color backgroundColor)
  2. createTextPaneScrollPane(JTextPane textPane)
  3. getFontFamily(JTextPane textPane)
  4. getForegroundColor(JTextPane textPane)
  5. insertStyledString(JTextPane textPane, String text, SimpleAttributeSet attrSet, Color background, Color foreground, String fontFamily, int fontSize, boolean bold, boolean italic, boolean underline, boolean strikeThrough, Boolean superscript)
  6. overrideStyle(String styleName, JTextPane textPane, Style newStyle)
  7. saveFile(JTextPane txt)
  8. scrollTo(JTextPane textPane, int position)
  9. setDefaultTextPaneProperties(JTextPane textPane)