styleddocument

How to clear all styling from StyledDocument?

扶醉桌前 提交于 2021-01-27 07:22:41
问题 StyledDocument contains various methods to set styles. Like setCharacterAttributes. But I can't see any methods to remove styles. Is there any? 回答1: It is impossible to "clear" styles. One should obtain a "default" style with the following technique: Style defaultStyle = StyleContext. getDefaultStyleContext(). getStyle(StyleContext.DEFAULT_STYLE); Then apply it with: sampleDocument.setCharacterAttributes(0, sampleDocument.getLength(), defaultStyle, true); 回答2: StyledDocument has a removeStyle

Font of a StyledDocument associated with a JTextPane

烂漫一生 提交于 2020-01-04 02:42:24
问题 What font does the StyledDocument associated with a JTextPane use? By default, does it use the same font as the JTextPane? In particular, I'm wondering about the font size. 回答1: StyledDocument is just interface. Interface doesn't have any font. If you take a look at the DefaultStyledDocument class (implementing the interface). public Font getFont(AttributeSet attr) { StyleContext styles = (StyleContext) getAttributeContext(); return styles.getFont(attr); } Then in the StyleContext's sources

ColorPane - grab strings of different character possible?

喜你入骨 提交于 2019-12-29 02:08:37
问题 I'm currently working on a old project that was given to me, it currently use java swing and has a basic gui. It has a ColorPane that extends Jtextpane to change colors of selected text. It uses this methond public void changeSelectedColor(Color c) { changeTextAtPosition(c, this.getSelectionStart(), this.getSelectionEnd()); } Say that string = "Hello World!" Hello color is green World is black. How do I grab Hello out base on its color from the Jtextpane. I've tried the clunky way which is

How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

巧了我就是萌 提交于 2019-12-17 13:05:53
问题 How exactly is word-wrapping implemented in JTextPane? I'm trying to understand exactly how it works so that I can modify the behavior. Right now, if I have a standard JTextPane inside a JScrollPane, it will break text at spaces, but not inside long words - if there is a string of text without spaces that is wider than the window, it won't wrap/break and a horizontal scrollbar will appear. As the text width increases, the width of the ParagraphView (via getWidth()) increases to hold the text.

Multi-color text selection in JTextPane (Swing)

删除回忆录丶 提交于 2019-12-12 12:31:37
问题 I have a JTextPane, with styledDocuent. I've inserted programmatically the text: "Hello World". Word "Hello" is red, and word "World" is green. Is there any way I can select the two words, and the selection rectangle becomes half red half green(or whatever color the selected character is)? By select, I mean, select text at runtime, not programmatically... I believe here Changing color of selected text in jTextPane , StanislavL tells how this can be achieved, by I don't know how to implement

Which is the right regular expression to use for Numbers and Strings?

蓝咒 提交于 2019-12-10 13:17:32
问题 I am trying to create simple IDE and coloring my JTextPane based on Strings (" ") Comments (// and /* */) Keywords (public, int ...) Numbers (integers like 69 and floats like 1.5) The way i color my source code is by overwritting the insertString and removeString methods inside the StyledDocument. After much testing, i have completed comments and keywords. Q1: As for my Strings coloring, I color my strings based on this regular expression: Pattern strings = Pattern.compile("\"[^\"]*\"");

How to output using StyledDocument with HTML?

大憨熊 提交于 2019-12-10 10:38:40
问题 I have a JTextPane , and I would like to output text on it using StyledDocument . Here is my StyledDocument object StyledDocument dox = (StyledDocument) textArea.getDocument(); Style style = dox.addStyle("StyleName", null); StyleConstants.setFontFamily(style, Font.SANS_SERIF); StyleConstants.setFontSize(style, 8); dox.insertString(dox.getLength(), "<b>Some Text</b>", null); The problem right now is if I edit the text with html code, it does not display the way I want. I want the text to be

Change specific String attributes in StyledDocument

爷,独闯天下 提交于 2019-12-07 08:16:53
问题 I am trying to create a text editor. I am using a JTextPane with a StyledDocument. What I am trying to implement is a way to change the attributes of selected text. This works in the following way : the user inputs the desired text. Afterwards, he can change any String's attributes (Font family, font size, whether it is bold/italic or not) by selecting it and pressing a button, where by means of checkboxes and dropdown lists would select the desired changes. Is it possible for me to change

How to output using StyledDocument with HTML?

↘锁芯ラ 提交于 2019-12-06 03:33:06
I have a JTextPane , and I would like to output text on it using StyledDocument . Here is my StyledDocument object StyledDocument dox = (StyledDocument) textArea.getDocument(); Style style = dox.addStyle("StyleName", null); StyleConstants.setFontFamily(style, Font.SANS_SERIF); StyleConstants.setFontSize(style, 8); dox.insertString(dox.getLength(), "<b>Some Text</b>", null); The problem right now is if I edit the text with html code, it does not display the way I want. I want the text to be displayed as bolded instead of literally " Some Text ". Is there a way to do this? livelaughlove I did

Java Setting Indent Size on JTextPane

徘徊边缘 提交于 2019-12-02 06:20:43
问题 I want to set the size of a tab character, \t, in a JTextPane to be 4 spaces wide. After Googling quite a bit I found some things that I will include here for what I have tried and maybe why they failed. How do you set the tab size in a JEditorPane? JTextPane is not a plain document. Java JTextpane Tab Size Eclipse raised some errors: Type mismatch: cannot convert from javax.swing.text.AttributeSet to javax.print.attribute.AttributeSet and The method setParagraphAttributes(javax.swing.text