jtextcomponent

Writing multiline JTextArea content into file

旧时模样 提交于 2019-11-26 17:23:00
问题 I have to write the content of textarea into a file with line breaks. I got the output like, it is written as one string in the file. public void actionPerformed(ActionEvent ev) { text.setVisible(true); String str= text.getText(); System.out.println(str); try { BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt")); fileOut.write(str); fileOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } Example Output should be: I am King. but it is showing: IamKing.

How can I set each character to a different color/background color in a JTextPane?

喜欢而已 提交于 2019-11-26 16:54:34
问题 I've been searching for this for a while and so far all I've been able to come up with is how to create a style and apply it to a character like so: StyledDocument doc = (StyledDocument) new DefaultStyledDocument(); JTextPane textpane = new JTextPane(doc); textpane.setText("Test"); javax.swing.text.Style style = textpane.addStyle("Red", null); StyleConstants.setForeground(style, Color.RED); doc.setCharacterAttributes(0, 1, textpane.getStyle("Red"), true); This is useful if you have only a few

java change the document in DocumentListener

自古美人都是妖i 提交于 2019-11-26 16:44:16
I use a DocumentListener to handle any change in a JTextPane document. while the user types i want to delete the contents of JTextPane and insert a customized text instead. it is not possible to change the document in the DocumentListener ,instead a solution is said here: java.lang.IllegalStateException while using Document Listener in TextArea, Java ,but i don't understand that, at least i don't know what to do in my case? DocumentListener is really only good for notification of changes and should never be used to modify a text field/document. Instead, use a DocumentFilter Check here for

Using DocumentFilter.FilterBypass

你说的曾经没有我的故事 提交于 2019-11-26 14:49:15
问题 I want to have a method like this on my DocumentFilter public void replaceUpdate(int offset, int length, String text) { try { super.replace(byPass, offset, length, text, null); } catch (BadLocationException ex) { //error } } Currently in order to get an instance of FilterBypass (byPass on method above) , I need to get from the overridden method insertString : private FilterBypass byPass; @Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet

How to use TextAction

坚强是说给别人听的谎言 提交于 2019-11-26 11:38:55
问题 What are the purposes of using TextAction from AbstractAction? and how to use it for the following: Caret Selection in the JTextComponents KeyBindings 回答1: While composing this answer, I recalled a venerable HTMLDocumentEditor by Charles Bell that illustrates the typical usage of the subclasses found in javax.swing.text.TextAction. That editor is listed among the credits of Metaphase Editor. This related example showing actions found in StyledEditorKit follows the same approach. All such

java change the document in DocumentListener

女生的网名这么多〃 提交于 2019-11-26 04:55:33
问题 I use a DocumentListener to handle any change in a JTextPane document. while the user types i want to delete the contents of JTextPane and insert a customized text instead. it is not possible to change the document in the DocumentListener ,instead a solution is said here: java.lang.IllegalStateException while using Document Listener in TextArea, Java ,but i don\'t understand that, at least i don\'t know what to do in my case? 回答1: DocumentListener is really only good for notification of