jtextpane

Keeping the format on text retrieval

喜夏-厌秋 提交于 2019-12-06 11:08:18
I am making a network application that has a chat function. On the chat I have one JTextPane for displaying messages and one more for input. Then I have some buttons that allow to add style on the input text(bold,italic,font size,colour). The text is formatted correctly on input pane , although when moved to the display pane(once the correct JButton is pressed) it only has the format of last character. How can I move the text while keeping its original format?For example if I write "Hello Worl d " on the input , display shows "Hello Worl d" textPane is the input pane Where set : final

How to implement an auto-complete for a text editor written in java?

泄露秘密 提交于 2019-12-06 09:14:03
问题 I'm trying to create a text editor like program for coding in mips assembly using java , the point i got mixed up alittle was the part i was trying to provide a Control-Space feature like that of eclipse's.For example when the user enters add $s1 , then presses ctrl+Space , i would replace this string: "add $s1 , $s2 , $s3" with what he has typed! ( that's an example .. i know add can be of immediate type instructions :D ), I'm using a hashmap to bind key strokes to what will actually happen

How can I create an AutoComplete popup in a JTextPane in Java?

折月煮酒 提交于 2019-12-06 08:59:22
问题 I am creating a SQL editor. I am using JTextPane for the editor. I want to implement AutoCompletion for table name etc. like Eclipse. 回答1: I think the appropriate class for displaying info on top of another component is JPopupMenu , which already handles layering correctly to display itself. JPopupMenu has a show() method that takes its 'parent' component as an argument, and it will show itself in that component's coordinate space. Since you want to display a selection of terms for the user

How to get selection from JTextPane

删除回忆录丶 提交于 2019-12-06 04:18:47
I want to find out which part of JTextPanel text is selected. Tried to call JTextPane.getSelectionStart() and JTextPane.getSelectionEnd() , but they always return same value that is equal to current caret position. What is my problem with that? I would be thankful for any code exapmle that gets current selection. Have a look at JTextComponent#getSelectedText() . You'd simply call this method on the instance of your JTextPane and it will return the selected text of your JTextPane . Did a small example: import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent;

Why do I loose new line character when I load text from a java servlet to JTextPane?

安稳与你 提交于 2019-12-06 04:16:23
I try to load content of a text file that contains some text in multiple lines using java servlet. When I test servlet in browser it works fine. Text is loaded with new line chars. But when I load it to a string in my swing application and then use textpane.setText(text); new lines are gone. I tried many solutions I found int the net, but still can't get it right. Servlet Code: Reading text from file (simplified): File file = new File(path); StringBuilder data = new StringBuilder(); BufferedReader in = new BufferedReader(new FileReader(file)); String line; while ((line = in.readLine()) != null

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

Enabling word wrap in a JTextPane with HTMLDocument

大兔子大兔子 提交于 2019-12-06 03:02:37
问题 Everywhere I read answers of people finding ways of enabling word wrapping in a JTextPane , but none of them work for me. I'm using an HTMLDocument (to display "text/html" content) and nothing that I have found so far got it to work. The JTextPane always cause the JScrollPane to scroll horizontally. I need the JTextPane to be scrollable, but only vertically. Would anyone have a workable demo of a word wrapping JTextPane displaying HTML content? 回答1: Use this as example to implement custom

Java string replaceAll()

六眼飞鱼酱① 提交于 2019-12-06 01:59:49
I've been wondering if for example: JTextPane chatTextArea = new JTextPane(); s.replaceAll(":\\)", emoticon()); public String emoticon(){ chatTextArea.insertIcon(new ImageIcon(ChatFrame.class.getResource("/smile.png"))); return "`"; } can put a picture and a "`" everywhere ":)" is found. When I run it like this if s contains a ":)" then the whole s gets replaced just by the icon. Is there a way to do it? David Kroukamp Here is a small example I made (+1 to @StanislavL for the original), simply uses DocumentListener and checks when a matching sequence for an emoticon is entered and replaces it

Method that returns the line number for a given JTextPane position?

半世苍凉 提交于 2019-12-05 23:18:18
I'm looking for a method that computes the line number of a given text position in a JTextPane with wrapping enabled. Example: This a very very very very very very very very very very very very very very very very very very very very very very long line. This is another very very very very very very very very very very very very very very very very very very very very very very long line. | The cursor is on line number four, not two. Can someone provide me with the implementation of the method: int getLineNumber(JTextPane pane, int pos) { return ??? } Try this /** * Return an int containing

How to get the height of the entered text in a JTextPane?

♀尐吖头ヾ 提交于 2019-12-05 21:47:35
Is there a possible way to get the height of an entered text in a JTextPane ? Pixels would be great but any value that can represent it would do. For example I have entered in the JTextPane this: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with