Example usage for javax.swing.text StyleConstants setBackground

List of usage examples for javax.swing.text StyleConstants setBackground

Introduction

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

Prototype

public static void setBackground(MutableAttributeSet a, Color fg) 

Source Link

Document

Sets the background color.

Usage

From source file:Main.java

public static void main(String args[]) throws BadLocationException {
    JFrame jf = new JFrame("StyledText");
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = jf.getContentPane();

    JTextPane pane = new JTextPane();
    SimpleAttributeSet set = new SimpleAttributeSet();
    StyleConstants.setBold(set, true);

    // Set the attributes before adding text
    pane.setCharacterAttributes(set, true);
    pane.setText("java2s.com ");

    set = new SimpleAttributeSet();
    StyleConstants.setItalic(set, true);
    StyleConstants.setForeground(set, Color.red);
    StyleConstants.setBackground(set, Color.blue);

    Document doc = pane.getStyledDocument();
    doc.insertString(doc.getLength(), "Swing ", set);

    set = new SimpleAttributeSet();
    StyleConstants.setFontSize(set, 24);

    doc.insertString(doc.getLength(), "Tutorial", set);

    JScrollPane scrollPane = new JScrollPane(pane);
    cp.add(scrollPane, BorderLayout.CENTER);

    jf.setSize(400, 300);/*from w w  w . ja  v  a  2  s  . c o  m*/
    jf.setVisible(true);
}

From source file:de.codesourcery.gittimelapse.MyFrame.java

protected final static SimpleAttributeSet createStyle(Color color) {
    SimpleAttributeSet result = new SimpleAttributeSet();
    StyleConstants.setBackground(result, color);
    return result;
}

From source file:com.sec.ose.osi.ui.frm.main.identification.codematch.JPanCodeMatchMain.java

private void markMySourceSnippet() {
    MutableAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setBackground(attr, new Color(255, 255, 180));

    startMySourceMatchPos.clear();/*from ww w.  j a va2  s  .  c  o m*/
    endMySourceMatchPos.clear();

    String projectName = IdentifyMediator.getInstance().getSelectedProjectName();
    File mySourceCode = new File(protexSDK.getSourceLocation(projectName) + File.separator + currentFileName);
    if (mySourceCode.exists()) {
        FileReader fr = null;
        try {
            fr = new FileReader(mySourceCode);
            BufferedReader br = new BufferedReader(fr);
            String tmpStr = null;
            int curLine = 1;
            int curTextSize = 0;
            while ((tmpStr = br.readLine()) != null) {
                setMySourceMatchLine(curLine, curTextSize);
                curTextSize += tmpStr.length() + 1;
                curLine++;
            }
            totalLine = curLine - 1;
            setMySourceMatchLine(curLine, curTextSize);
        } catch (Exception e) {
            log.warn(e);
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (Exception e) {
                log.warn(e);
            }
        }

        getJPanMatchedSourceViewLeft().clearStyle();

        if (startMySourceMatchPos.size() > endMySourceMatchPos.size())
            endMySourceMatchPos
                    .add(getJPanMatchedSourceViewLeft().getStyledDocumentForSourcePane().getLength());

        int snippetCnt = startMySourceMatchPos.size();
        if (snippetCnt > 0) {
            for (int i = 0; i < snippetCnt; i++) {
                int offset = startMySourceMatchPos.get(i);
                int length = endMySourceMatchPos.get(i) - offset;
                getJPanMatchedSourceViewLeft().getStyledDocumentForSourcePane().setCharacterAttributes(offset,
                        length, attr, true);
            }
            getJPanMatchedSourceViewLeft().getJTextPaneSourceView()
                    .setCaretPosition(startMySourceMatchPos.get(0));
        }
    }
}

From source file:org.nuxeo.launcher.gui.ColoredTextPane.java

/**
 * Append text at the end of document, choosing foreground and background colors, and bold attribute.
 *
 * @param text Text to append/*from  w w w . j a v a  2 s. co m*/
 * @param color Foreground color
 * @param bgColor Background color
 * @param isBold Is the text bold ?
 */
public void append(String text, Color color, Color bgColor, boolean isBold) {
    StyleConstants.setForeground(style, color);
    StyleConstants.setBackground(style, bgColor);
    StyleConstants.setBold(style, isBold);
    int len = doc.getLength();
    try {
        doc.insertString(len, text + "\n", style);
        if (maxSize > 0 && len > maxSize) {
            doc.remove(0, len - maxSize);
        }
    } catch (BadLocationException e) {
        log.error(e);
    }
    if (follow) {
        setCaretPosition(doc.getLength());
    }
}

From source file:org.spoutcraft.launcher.skin.ConsoleFrame.java

/**
 * Construct the frame./*from  w w  w.  j  a  v a  2 s.  c o  m*/
 * 
 * @param numLines number of lines to show at a time
 * @param colorEnabled true to enable a colored console
 * @param trackProc process to track
 * @param killProcess true to kill the process on console close
 */
public ConsoleFrame(int numLines, boolean colorEnabled, final Process trackProc, final boolean killProcess) {
    super("Console");
    this.numLines = numLines;
    this.colorEnabled = colorEnabled;
    this.trackProc = trackProc;

    this.highlightedAttributes = new SimpleAttributeSet();
    StyleConstants.setForeground(highlightedAttributes, Color.BLACK);
    StyleConstants.setBackground(highlightedAttributes, Color.YELLOW);

    this.errorAttributes = new SimpleAttributeSet();
    StyleConstants.setForeground(errorAttributes, new Color(200, 0, 0));
    this.infoAttributes = new SimpleAttributeSet();
    StyleConstants.setForeground(infoAttributes, new Color(200, 0, 0));
    this.debugAttributes = new SimpleAttributeSet();
    StyleConstants.setForeground(debugAttributes, Color.DARK_GRAY);

    setSize(new Dimension(650, 400));
    buildUI();

    Compatibility.setIconImage(this, Toolkit.getDefaultToolkit().getImage(LoginFrame.spoutcraftIcon));

    if (trackProc != null) {
        track(trackProc);
    }

    addMouseListener(this);

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent event) {
            if (trackProc != null && killProcess) {
                trackProc.destroy();
                if (loggerHandler != null) {
                    rootLogger.removeHandler(loggerHandler);
                }
                event.getWindow().dispose();
            }
        }
    });
}

From source file:pl.otros.logview.gui.message.pattern.StyleProperties.java

public static Style getStyle(StyleContext styleContext, DataConfiguration styleConfig, String styleName,
        int group) {
    Style style = styleContext.addStyle(styleName, styleContext.getStyle(StyleContext.DEFAULT_STYLE));

    String groupSuffix = "." + group;
    if (group <= 0) {
        groupSuffix = "";
    }/*  www  .j av  a  2  s.  c  o m*/

    String fontFamily = styleConfig.getString(PROP_FONT_FAMILY + groupSuffix, "");
    if (fontFamily.trim().length() > 0) {
        StyleConstants.setFontFamily(style, styleConfig.getString(PROP_FONT_FAMILY + groupSuffix));
    }

    if (styleConfig.getString(PROP_FONT_SIZE + groupSuffix, "").trim().length() > 0) {
        StyleConstants.setFontSize(style, styleConfig.getInt(PROP_FONT_SIZE + groupSuffix));
    }

    if (styleConfig.getString(PROP_FONT_BOLD + groupSuffix, "").trim().length() > 0) {
        StyleConstants.setBold(style, styleConfig.getBoolean(PROP_FONT_BOLD + groupSuffix));
    }

    if (styleConfig.getString(PROP_FONT_ITALIC + groupSuffix, "").trim().length() > 0) {
        StyleConstants.setItalic(style, styleConfig.getBoolean(PROP_FONT_ITALIC + groupSuffix));
    }

    if (styleConfig.getString(PROP_FONT_UNDERLINE + groupSuffix, "").trim().length() > 0) {
        StyleConstants.setUnderline(style, styleConfig.getBoolean(PROP_FONT_UNDERLINE + groupSuffix));
    }

    if (styleConfig.getString(PROP_BACKGROUND + groupSuffix, "").trim().length() > 0) {
        StyleConstants.setBackground(style, styleConfig.getColor(PROP_BACKGROUND + groupSuffix));
    }

    if (styleConfig.getString(PROP_FOREGROUND + groupSuffix, "").trim().length() > 0) {
        StyleConstants.setForeground(style, styleConfig.getColor(PROP_FOREGROUND + groupSuffix));
    }
    return style;
}

From source file:pl.otros.logview.gui.message.SearchResultColorizer.java

@Override
public Collection<MessageFragmentStyle> colorize(String textToColorize) throws BadLocationException {
    Collection<MessageFragmentStyle> list = new ArrayList<MessageFragmentStyle>();
    if (StringUtils.isEmpty(searchString)) {
        return list;
    }/* w  ww.j a va  2  s.c om*/
    StyleContext sc = new StyleContext();
    Style searchStyle = sc.addStyle("searchResult", sc.getStyle(StyleContext.DEFAULT_STYLE));
    StyleConstants.setBackground(searchStyle, color);
    if (searchMode.equals(SearchMode.STRING_CONTAINS)) {
        list.addAll(colorizeString(textToColorize, searchStyle, searchString));
    } else if (searchMode.equals(SearchMode.REGEX)) {
        list.addAll(MessageColorizerUtils.colorizeRegex(searchStyle, textToColorize,
                Pattern.compile(searchString, Pattern.CASE_INSENSITIVE), 0));
    }
    for (MessageFragmentStyle style : list) {
        style.setSearchResult(true);
    }
    return list;
}

From source file:pl.otros.logview.gui.message.update.LogDataFormatter.java

private Style getStyleForMarkerColor(MarkerColors markerColors) {
    String styleName = "MarkerColor" + markerColors.name();
    Style style = sc.getStyle(styleName);
    if (style == null) {
        style = sc.addStyle(styleName, mainStyle);
        StyleConstants.setForeground(style, markerColors.getForeground());
        StyleConstants.setBackground(style, markerColors.getBackground());
    }//from w w w.ja v  a2s . c  o m
    return style;
}