List of utility methods to do JTextPane
Color | getForegroundColor(JTextPane textPane) Returns the current foreground color. Color color = (Color) (textPane.getInputAttributes().getAttribute(StyleConstants.Foreground)); if (color == null) { color = new Color(0, 0, 0); return color; |
SimpleAttributeSet | 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) insert Styled String if (attrSet == null) { attrSet = new SimpleAttributeSet(); if (background != null) StyleConstants.setBackground(attrSet, background); if (foreground != null) StyleConstants.setForeground(attrSet, foreground); if (fontFamily != null) ... |
boolean | isSelectionBold(JTextPane textPane) Returns true if all of the selection is bold, otherwise false. 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; |
void | overrideStyle(String styleName, JTextPane textPane, Style newStyle) override Style Style style = textPane.getStyle(styleName);
if (style == null) {
style = styleContext.addStyle(styleName, null);
textPane.addStyle(styleName, style);
overrideStyle(style, newStyle);
|
void | saveFile(JTextPane txt) save File FileWriter fw; try { fw = new FileWriter(getDiretorio().getAbsoluteFile(), false); txt.write(fw); fw.close(); } catch (IOException e) { e.printStackTrace(); |
void | scrollTo(JTextPane textPane, int position) Forces a portion of the document to become visible. Rectangle r = textPane.modelToView(position);
if (r != null) {
textPane.scrollRectToVisible(r);
|
void | setDefaultTextPaneProperties(JTextPane textPane) set Default Text Pane Properties textPane.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, "\n");
|
void | setJTextPaneFont(JTextPane jtp, Font font, Color c) Utility method for setting the font and color of a JTextPane. MutableAttributeSet attrs = jtp.getInputAttributes(); StyleConstants.setFontFamily(attrs, font.getFamily()); StyleConstants.setFontSize(attrs, font.getSize()); StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0); StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0); StyleConstants.setForeground(attrs, c); StyledDocument doc = jtp.getStyledDocument(); doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false); ... |
void | setSelectionFontSize(JTextPane textPane, int fontSize) Sets the font size of the selected text. String FONTFAMILY = "fontSize." + fontSize; Style fontSizeStyle = new StyleContext().addStyle(FONTFAMILY, null); fontSizeStyle.addAttribute(StyleConstants.FontSize, new Integer(fontSize)); textPane.setCharacterAttributes(fontSizeStyle, false); |
void | setSelectionForeground(JTextPane textPane, Color color) Sets the foreground color of the selection. if (color != null) { String FOREGROUND = "foregroundColor." + color.toString(); Style foregroundStyle = new StyleContext().addStyle(FOREGROUND, null); foregroundStyle.addAttribute(StyleConstants.Foreground, color); textPane.setCharacterAttributes(foregroundStyle, false); |