Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE); Style comment = styleContext.addStyle(COMMENT, defaultStyle); StyleConstants.setForeground(comment, COMMENT_COLOR); StyleConstants.setItalic(comment, true); Style quotes = styleContext.addStyle(QUOTES, defaultStyle); StyleConstants.setForeground(quotes, Color.MAGENTA.darker().darker()); Style charQuotes = styleContext.addStyle(SINGLE_QUOTES, defaultStyle); StyleConstants.setForeground(charQuotes, Color.GREEN.darker().darker()); Style slashyQuotes = styleContext.addStyle(SLASHY_QUOTES, defaultStyle); StyleConstants.setForeground(slashyQuotes, Color.ORANGE.darker()); Style digit = styleContext.addStyle(DIGIT, defaultStyle); StyleConstants.setForeground(digit, Color.RED.darker()); Style operation = styleContext.addStyle(OPERATION, defaultStyle); StyleConstants.setBold(operation, true); Style ident = styleContext.addStyle(IDENT, defaultStyle); Style reservedWords = styleContext.addStyle(RESERVED_WORD, defaultStyle); StyleConstants.setBold(reservedWords, true); StyleConstants.setForeground(reservedWords, Color.BLUE.darker().darker()); Style leftParens = styleContext.addStyle(IDENT, defaultStyle);
DefaultStyledDocument document = new DefaultStyledDocument(); JTextPane textpane = new JTextPane(document); StyleContext context = new StyleContext(); // build a style Style style = context.addStyle("test", null); // set some style properties StyleConstants.setForeground(style, Color.BLUE); // add some data to the document document.insertString(0, "", style);
protected Collection<MessageFragmentStyle> findExceptionNameAndMessage(final Style style, String subTextFragment) { final ArrayList<MessageFragmentStyle> result = new ArrayList<>(); Matcher matcherMessage = exceptionNameAndMessage.matcher(subTextFragment); while (matcherMessage.find()) { final int beginIndex = matcherMessage.start(1); final int endIndex = matcherMessage.end(1); final String msg = subTextFragment.substring(beginIndex, endIndex).replaceFirst("Caused by: ", ""); final Style style1 = styleContext.addStyle("exceptionMessage-" + msg, style); style1.addAttribute(STYLE_ATTRIBUTE_EXCEPTION_MSG, msg); result.add(new MessageFragmentStyle(beginIndex, endIndex - beginIndex, style1, false)); System.out.println("Setting style with exceptionMessage " + style1); } return result; }
String text = jTextPane.getSelectedText(); int cursorPosition = jTextPane.getCaretPosition(); StyleContext context = new StyleContext(); Style style; jTextPane.replaceSelection(""); style = context.addStyle("mystyle", null); style.addAttribute(StyleConstants.FontSize, new Integer(16)); jTextPane.getStyledDocument().insertString(cursorPosition - text.length(), text, style);
StyleContext context = new StyleContext(); Style style = context.addStyle("mystyle", null); style.addAttribute(StyleConstants.FontSize, new Integer(16));; jTextPane.setCharacterAttributes(style , true);
Document doc = yourEditorPane.getDocument(); StyleContext sc = new StyleContext(); Style style = sc.addStyle("yourStyle", null); Font font = new Font("Arial", Font.BOLD, 18); StyleConstants.setForeground(style, Color.RED); StyleConstants.setFontFamily(style, font.getFamily()); StyleConstants.setBold(style, true); doc.insertString(doc.getLength(), "Hello World!", style);
DefaultStyledDocument doc = new DefaultStyledDocument(); StyleContext sc = new StyleContext(); Style style = sc.addStyle("strikethru", null); StyleConstants.setStrikeThrough (style,true); doc.insertString (0, "Hello ", null); doc.insertString (6, "strike through ", style); JTextPane pane = new JTextPane(doc);
private static StyleContext getDefaultStyleContext() { StyleContext result = new StyleContext(); Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE ); Style baseStyle = result.addStyle( "base", defaultStyle ); Style regularStyle = result.addStyle( STYLE_REGULAR, baseStyle ); StyleConstants.setFontFamily( regularStyle, Font.MONOSPACED ); StyleConstants.setFontSize( regularStyle, 12 ); Style hyperStyle = result.addStyle( STYLE_HYPERLINK, regularStyle ); StyleConstants.setForeground( hyperStyle, COLOR_HYPER ); StyleConstants.setUnderline( hyperStyle, true ); Style titleStyle = result.addStyle( STYLE_TITLE, baseStyle ); StyleConstants.setFontFamily( titleStyle, Font.SANS_SERIF ); StyleConstants.setFontSize( titleStyle, 24 ); StyleConstants.setBold( titleStyle, true ); return result; } }
private void initStyles() { StyleContext sc = new StyleContext(); Style parent = sc.getStyle(StyleContext.DEFAULT_STYLE); StyleConstants.setFontFamily(parent, "courier"); StyleConstants.setFontSize(parent, 13); styleElementName = sc.addStyle("elementName", parent); StyleConstants.setForeground(styleElementName, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_ELEMENT_NAME)); styleAttributeName = sc.addStyle("attributeName", parent); StyleConstants.setForeground(styleAttributeName, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_ATTRIBUTE_NAME)); styleAttributeValue = sc.addStyle("attributeValue", parent); StyleConstants.setForeground(styleAttributeValue, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_ATTRIBUTE_VALUE)); styleContent = sc.addStyle("content", parent); StyleConstants.setBackground(styleContent, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CONTENT_BACKGROUND)); StyleConstants.setForeground(styleContent, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CONTENT_FOREGROUND)); styleOperator = sc.addStyle("operator", parent); StyleConstants.setForeground(styleOperator, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_OPERATOR)); StyleConstants.setBold(styleOperator, true); styleComments = sc.addStyle("comments", parent); StyleConstants.setForeground(styleComments, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_COMMENTS)); styleCData = sc.addStyle("cdata", parent); StyleConstants.setForeground(styleCData, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CDATA_FOREGROUND)); StyleConstants.setBackground(styleCData, theme.getColor(ThemeKey.LOG_DETAILS_SOAP_CDATA_BACKGROUND)); styleDOCTYPE = sc.addStyle("doctype", sc.addStyle("doctype", parent)); }
DefaultStyledDocument document = new DefaultStyledDocument(); JTextPane textpane = new JTextPane(document); StyleContext context = new StyleContext(); // build a style Style style = context.addStyle("test", null); // set some style color StyleConstants.setForeground(style, Color.RED); // add some data to the document document.insertString(0, "", style); OR JTextPane pane = new JTextPane(); SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setForeground(set, Color.red); Document doc = pane.getStyledDocument(); doc.insertString(doc.getLength(), "Kleine ", set);
mainStyle = sc.addStyle("MainStyle", defaultStyle); StyleConstants.setFontSize(mainStyle, 12); StyleConstants.setForeground(mainStyle, Color.BLACK); licenceStyle = sc.addStyle("classMethod", null); StyleConstants.setFontFamily(licenceStyle, "monospaced"); StyleConstants.setForeground(licenceStyle, Color.BLUE); titleStyle = sc.addStyle("note", mainStyle); StyleConstants.setFontFamily(titleStyle, "arial"); StyleConstants.setBold(titleStyle, true);
public Style createStyle(String _name, Color _fg, boolean _bold, boolean _italic) { Style style = ((StyleContext) getAttributeContext()).addStyle(_name, null); if (_fg != null) style.addAttribute(StyleConstants.Foreground, _fg); if (_bold) style.addAttribute(StyleConstants.Bold, Boolean.TRUE); if (_italic) style.addAttribute(StyleConstants.Italic, Boolean.TRUE); return style; }
@Override public Collection<MessageFragmentStyle> colorize(String textToColorize) { Collection<MessageFragmentStyle> list = new ArrayList<>(); if (StringUtils.isEmpty(searchString)) { return list; } StyleContext sc = new StyleContext(); Style searchStyle = sc.addStyle("searchResult", sc.getStyle(StyleContext.DEFAULT_STYLE)); final Color color = theme.getColor(ThemeKey.SEARCH_RESULT); 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; }
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; public class SQLEditor extends JFrame { private static final long serialVersionUID = 1L; public SQLEditor() { StyleContext styleContext = new StyleContext(); Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE); Style cwStyle = styleContext.addStyle("ConstantWidth", null); StyleConstants.setForeground(cwStyle, Color.BLUE); StyleConstants.setBold(cwStyle, true); final JTextPane pane = new JTextPane(new KeywordStyledDocument(defaultStyle, cwStyle)); pane.setFont(new Font("Courier New", Font.PLAIN, 12)); JScrollPane scrollPane = new JScrollPane(pane); getContentPane().add(scrollPane, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(375, 400); } public static void main(String[] args) throws BadLocationException { SQLEditor app = new SQLEditor(); app.setVisible(true); } }
/** Adds style to the document */ public Style addStyle(String styleName, Style parent) { String layerName = (String)stylesToLayers.get(styleName); if (layerName == null) { layerName = styleName; // same layer name as style name addStyleToLayerMapping(styleName, layerName); } Style style = styles.addStyle(styleName, parent); if (findLayer(layerName) == null) { // not created by default try { extWriteLock(); addStyledLayer(layerName, style); } finally { extWriteUnlock(); } } return style; }
protected Collection<MessageFragmentStyle> colorizeStackTraceRegex(final Style style, String text, Pattern regex, int group) { ArrayList<MessageFragmentStyle> list = new ArrayList<>(); Matcher matcher = regex.matcher(text); Style styleToUse = style; while (matcher.find()) { LocationInfo locationInfo = LocationInfo.parse(matcher.group(0)); if (locationInfo != null) { String name = styleToUse.getName(); Style newStyle = styleContext.addStyle(name + "-" + locationInfo.toString(), styleToUse); newStyle.addAttribute(STYLE_ATTRIBUTE_LOCATION_INFO, locationInfo); StyleConstants.setForeground(newStyle, StyleConstants.getForeground(styleToUse)); StyleConstants.setBold(newStyle, StyleConstants.isBold(styleToUse)); StyleConstants.setItalic(newStyle, StyleConstants.isItalic(styleToUse)); styleToUse = newStyle; } int start = matcher.start(group); int end = matcher.end(group); if (end - start > 0) { MessageFragmentStyle messageFragmentStyle = new MessageFragmentStyle(start, end - start, styleToUse, false); list.add(messageFragmentStyle); } } return list; }
protected void initStyles() { styleContext = new StyleContext(); Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE); StyleConstants.setFontFamily(defaultStyle, "courier"); styleStackTrace = styleContext.addStyle("stackTrace", defaultStyle); StyleConstants.setBackground(styleStackTrace, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_BACKGROUND)); StyleConstants.setForeground(styleStackTrace, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_FOREGROUND)); stylePackage = styleContext.addStyle("stylePackage", styleStackTrace); styleClass = styleContext.addStyle("styleClass", stylePackage); StyleConstants.setForeground(styleClass, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_CLASS)); StyleConstants.setBold(styleClass, true); styleMethod = styleContext.addStyle("styleMethod", styleStackTrace); StyleConstants.setForeground(styleMethod, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_METHOD)); StyleConstants.setItalic(styleMethod, true); StyleConstants.setBold(styleMethod, true); styleFile = styleContext.addStyle("styleFile", styleStackTrace); StyleConstants.setForeground(styleFile, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_FLE)); StyleConstants.setUnderline(styleFile, true); styleCodeComment = styleContext.addStyle("styleCodeComment", defaultStyle); StyleConstants.setForeground(styleCodeComment, theme.getColor(ThemeKey.LOG_DETAILS_STACKTRACE_COMMENT)); StyleConstants.setItalic(styleCodeComment, true); }
/** Adds style to the document */ public Style addStyle(String styleName, Style parent) { String layerName = (String) stylesToLayers.get(styleName); if (layerName == null) { layerName = styleName; // same layer name as style name addStyleToLayerMapping(styleName, layerName); } Style style = styles.addStyle(styleName, parent); if (findLayer(layerName) == null) { // not created by default try { extWriteLock(); addStyledLayer(layerName, style); } finally { extWriteUnlock(); } } return style; }
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()); } return style; }
/** * Set the style identified by the name. * * @param token * the style token * @param foreground * the foreground color * @param fontStyle * the font style Plain, Italic or Bold */ public void setStyle(String token, Color foreground, int fontStyle) { Style s = context.getStyle(token); if (s == null) { s = context.addStyle(token, context.new NamedStyle()); } StyleConstants.setItalic(s, (fontStyle & Font.ITALIC) > 0); StyleConstants.setBold(s, (fontStyle & Font.BOLD) > 0); StyleConstants.setForeground(s, foreground); }