Java JComponent Color buildTextPane(String text, Color foreground)

Here you can find the source of buildTextPane(String text, Color foreground)

Description

Formats the text and displays it in a JTextPane .

License

Open Source License

Parameter

Parameter Description
text The text to display.
foreground The foreground color.

Return

See above.

Declaration

public static JTextPane buildTextPane(String text, Color foreground) 

Method Source Code

//package com.java2s;
/*/*from www.jav  a 2  s. c  o m*/
 * org.openmicroscopy.shoola.util.ui.UIUtilities
 *
 *------------------------------------------------------------------------------
 *  Copyright (C) 2006-2015 University of Dundee. All rights reserved.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *------------------------------------------------------------------------------
 */

import java.awt.Color;

import javax.swing.JTextPane;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class Main {
    /**
     * Formats the text and displays it in a {@link JTextPane}.
     * 
     * @param text The text to display.
     * @return See above.
     */
    public static JTextPane buildTextPane(String text) {
        return buildTextPane(text, null);
    }

    /**
     * Formats the text and displays it in a {@link JTextPane}.
     * 
     * @param text          The text to display.
     * @param foreground   The foreground color.
     * @return See above.
     */
    public static JTextPane buildTextPane(String text, Color foreground) {
        if (text == null)
            text = "";
        StyleContext context = new StyleContext();
        StyledDocument document = new DefaultStyledDocument(context);

        Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
        StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
        if (foreground != null)
            StyleConstants.setForeground(style, foreground);
        try {
            document.insertString(document.getLength(), text, style);
        } catch (BadLocationException e) {
        }

        JTextPane textPane = new JTextPane(document);
        textPane.setOpaque(false);
        textPane.setEditable(false);
        textPane.setFocusable(false);
        return textPane;
    }
}

Related

  1. applyBackgroundToSubComponents(Container container, Color color)
  2. color(String string, AttributeSet set)
  3. convertHtmlColorNameToColor(String htmlColorName)
  4. createColorChooser(Component component)
  5. createDisabledTextComponent (String text, Color color, boolean textField)