jtextpane

Java JTextPane Change Font of Selected Text

瘦欲@ 提交于 2019-12-01 06:22:05
I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font? textpane.getSelectedText().setFont() won't work. (Even with font-family) You can change JTextPane's font only as a whole, it doesn't do rich text. There's a Document underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument() . You want to cast that to a StyledDocument if you can, and then you can do things like setCharacterAttributes to a given run of characters. There are some (hopefully) helpful examples in the Java tutorial

How to center text and a JComponent in a JTextPane vertically?

为君一笑 提交于 2019-12-01 04:38:52
问题 Currently it looks so What to do so that it looks so? Below is my code: JFrame f = new JFrame(); JTextPane textPane = new JTextPane(); JTextField component = new JTextField(" "); component.setMaximumSize(component.getPreferredSize()); textPane.setSelectionStart(textPane.getDocument().getLength()); textPane.setSelectionEnd(textPane.getDocument().getLength()); textPane.insertComponent(component); try { textPane.getDocument().insertString(textPane.getDocument().getLength(), "text", new

Java JTextPane Change Font of Selected Text

三世轮回 提交于 2019-12-01 04:37:59
问题 I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font? textpane.getSelectedText().setFont() won't work. (Even with font-family) 回答1: You can change JTextPane's font only as a whole, it doesn't do rich text. There's a Document underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument() . You want to cast that to a StyledDocument if you can, and then you can do things like

How to have a Scrollable JTextPane?

淺唱寂寞╮ 提交于 2019-11-30 22:59:26
问题 I would like to have a JTextPane that have scroll bar, how can I do so ? Thanks. 回答1: To insert a scroll bar on your new JTextPane, just use a JScrollPane: JTextPane txt = new JTextPane(); JScrollPane jsp = new JScrollPane(txt); JTextPane API: http://download.oracle.com/javase/6/docs/api/javax/swing/JTextPane.html JScrollPane API: http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html If you have some issues, take a look at this SO question : Java JTextPane JScrollPane

How to prevent memory leak in JTextPane.setCaretPosition(int)?

ぐ巨炮叔叔 提交于 2019-11-30 07:35:57
问题 I'm working on a Java application with Swing-based GUI. The application uses JTextPane to output log messages as follows: 1) truncate existing text to keep total text size under the limit; 2) append new text; 3) scroll to the end (actual logic is slightly different, but it's irrelevant here). I was using Eclipse with JVM Monitor to determine reasonable text size limit and found significant memory leak. I tried to remove UndoableEditListener s from the underlying document and disable automatic

How to make JTextPane scroll horizontally

只愿长相守 提交于 2019-11-29 17:38:29
I have a JTextPane , when there are too many lines, a vertical scroll bar appears, but when a line is too long, instead of appearing a horizontal scroll bar, the line breaks into two lines, how to make the horizontal bar appear instead of breaking into two lines, my jTextPane is added like this: JScrollPane jScrollPane1 = new JScrollPane(); jScrollPane1.setViewportView(jTextPane1); As presented here by our very own Rob Camick, you could try using something like... JTextPane tp = new JTextPane() { @Override public boolean getScrollableTracksViewportWidth() { return getUI().getPreferredSize(this

How to set the line spacing in a JtextPane?

落爺英雄遲暮 提交于 2019-11-29 16:49:57
First of all, i set the JTextPane like this: HTMLEditorKit editorKit = new HTMLEditorKit(); HTMLDocument document = (HTMLDocument) editorKit.createDefaultDocument(); JTextPane textPane = new JTextPane(); textPane.setContentType("text/html"); textPane.setDocument(document); and i want to set the line spacing in the JtextPane , this is my idea,but it can't work: SimpleAttributeSet aSet = new SimpleAttributeSet(); StyleConstants.setLineSpacing(aSet, 50); textPane.setParagraphAttributes(aSet, false); i was wrong? When you call textPane.setParagraphAttributes(aSet, false); it tries to apply line

How to set fixed width but dynamic height on JTextPane?

醉酒当歌 提交于 2019-11-29 15:55:28
I want to use a JTextpane with fixed width but dynamic height which should also allow wrapping. The height should change as the user adds or removes text. I would've used a JTextArea but the text has to be styled differently. Is there an easy way to go about this problem? Sharcoux I already wrote an answer for something similar. Check here . You have to understand that the preferredSize returned by the JTextPane is: if the width is not set, getPreferredSize returns Dimension(width,height) where width would be the width of the longest line if there where no wrapping, and height , the height

How to implement bullet points in a JTextPane?

流过昼夜 提交于 2019-11-29 15:10:50
I have a JTextPane with a StyledDocument and RTFEditorKit implemented. How can I add bullet points (preferrably multi-level ones) onto the JTextPane ? Well it does not have built in support for this, however here is a great link with tutorial on creating bulleted and numbered lists in JTextPane and JEditorPane s: Bullets and Numberings in the JEditorPane/JTextPane. Figured it out doing this: HTMLEditorKit.InsertHTMLTextAction bulletAction = new HTMLEditorKit.InsertHTMLTextAction("Bullet", "<li> </li>", HTML.Tag.BODY, HTML.Tag.UL); I know this is an old question, but what I have done is:

How to modify letter-spacing in a JTextPane?

跟風遠走 提交于 2019-11-29 15:06:00
I'm need to modify letter-spacing (font tracking) in a JTextPane, and I can't get it to work. When I'm using a JTextArea, I can just do: Font font = new Font("Courier New", Font.PLAIN, 10); HashMap <TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>(); attrs.put(TextAttribute.TRACKING, -0.1); font = font.deriveFont(attrs); textArea.setFont(font); but as I need to change line spacing, I need to use a JTextPane, and doing: textPane.setFont(font) as I did with the JTextArea doesn't work. another thing I tried was: MutableAttributeSet set = new SimpleAttributeSet(); StyleConstants