jtextpane

JTextPane - Make Caret Normal Size?

眉间皱痕 提交于 2019-12-11 05:23:06
问题 So I am trying to make the text in a JTextPane double spaced. Here's my code: MutableAttributeSet attrs = editor.getInputAttributes(); StyleConstants.setLineSpacing(attrs, 2); editor.getStyledDocument().setParagraphAttributes(0, doc.getLength() + 1, attrs, true); The problem with this, is, the curser (caret) is as big as three or four line spaces. How can I resize the caret to normal size? Here is a screen snip 回答1: Try this: editor.setCaret(new DefaultCaret() { public void paint(Graphics g)

Find the offset to a String in a JTextPane

假装没事ソ 提交于 2019-12-11 03:36:44
问题 I'm looking for a quick way to find a String in a JTextPane and change the style there, so it gets highlighted. What I currently have is something like this (tpOutput is the JTextPane in question, strSearch the String to be searched.. duh): int index = tpOutput.getText().indexOf(strSearch); StyledDocument doc = tpOutput.getStyledDocument(); doc.setCharacterAttributes(i, strSearch.length(), doc.getStyle("exampleStyle") , false); However, as beautiful as that would be if it worked, it counts

How do I limit the amount of characters in JTextPane as the user types (Java)

泪湿孤枕 提交于 2019-12-11 03:35:15
问题 I need to not allow any characters to be entered after X have been typed. I need to send a beep after X characters have been typed. I know how to do this after the user presses enter, but I need to do it before the user presses enter. The approach I found from Oracle's site is to add a DocumentSizeFilter to the JTextPane. I can't get this to notify the user when they have gone over (it doesn't work until they press enter). This is a sample of what I have. public class EndCycleTextAreaRenderer

KeyEvent issue on JTextArea

五迷三道 提交于 2019-12-11 03:26:24
问题 I am new to GUI programming. While practicing KeyEvent handling on Java Swing JTextarea I face one problem. The listener interface is implemented by text area itself. When I pressed VK_ENTER key in text area I get text from text area then I displayed that text into JTextPane . After that I set text as empty string on text area. Here I used keyPressed key event - it is creating one new line in text area but already I set text area row as 0 (zero). Actually I want one row in text area I don't

How do I stop my JTextPane swallowing keyboard shortcuts (accelerators)?

不羁岁月 提交于 2019-12-11 03:05:18
问题 My application's main JFrame contains a JTextPane. While it has focus, it consumes all keyboard shortcuts ("accelerators") instead of passing them on to the JFrame's JMenu. This means that while it has focus, eg ctrl-n for "New Document" doesn't work. Obviously it's useful that it handles ctrl-A/C/V/X correctly for select all/copy/paste/cut, but how do I convince it not to swallow the other shortcuts? 回答1: I think at the end of your key listener functions you should be able to say... this

Highlight and Bold text in JTextPane

﹥>﹥吖頭↗ 提交于 2019-12-11 00:54:28
问题 I am trying to add some code to myHighlighter class so that i can highlight and turn BOLD the part of the text i want.My first try was not successful.. Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red,Font.BOLD); class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter { public MyHighlightPainter(Color color, int Font) { super(color); } Also i am trying to avoid Graphics... 回答1: Have you tried setting the text to HTML? I believe the

How to copy styled text in JTextPane

孤街浪徒 提交于 2019-12-11 00:29:23
问题 I'm trying to create a WYSIWYG editor using JTextPane. I'm using DefaultEditorKit.CopyAction to copy text in the editor. But this method does not preserve the style of the text. Can someone tell me how to copy the text in JTextPane and preserve the style, please ? 回答1: http://java-sl.com/tip_merge_documents.html You can use this. If you need part of the document just select desired fragment of the source pane. 回答2: I have a class that uses the following code to copy all the text out of the

JTextPane does not go to a new line?

℡╲_俬逩灬. 提交于 2019-12-10 23:46:49
问题 I'm displaying RSS feed in my JTextPanel. Result displayed doesn't go to a new line. How can I insert \n in JTextPane? Thanks! writeNews class: public String writeNews() { String result = ""; try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); URL u = new URL("http://thestar.com.my.feedsportal.com/c/33048/f/534600/index.rss"); Document doc = builder.parse(u.openStream()); NodeList nodes = doc.getElementsByTagName("item"); for(int i=0;i<nodes.getLength()

getting JTextPane to scroll

别说谁变了你拦得住时间么 提交于 2019-12-10 22:33:11
问题 How do I get JTextPane to scroll? In this example, JScrollPane is employed but as running the code will reveal, the scroll bar can be displayed but it is not functional. import java.awt.BorderLayout; import javax.swing.*; import javax.swing.text.*; public class JTextPaneTester { JTextPane jtp = new JTextPane(); StyledDocument doc; Style style; JScrollPane jsp = new JScrollPane(jtp); JTextPaneTester() { doc = (StyledDocument)jtp.getDocument(); style = doc.addStyle("fancy", null); jsp

How to correctly print out a hard copy of a JTextPane with “text/rtf” content?

耗尽温柔 提交于 2019-12-10 20:16:46
问题 I'm trying to print out some simple RTF-formatted text to a laser printer using a JTextPane . The result looks fine on a software PDF printer (FreePDF XP), but the text doesn't have proper space between it's formatted parts when print to a real printer. Edit: I have uploaded an example output (The bottom is the scanned printout) Example http://ompldr.org/vNXo4Zg/output.png It seems to me that there is a problem with the Graphics object starting to paint the indiviual parts of the RTF code. As