2

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 SimpleAttributeSet set = new SimpleAttributeSet();

Code for making input text bold(same of adding other styles) :

bold.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = textPane.getStyledDocument();
                if (StyleConstants.isBold(set)) {
                    StyleConstants.setBold(set, false);
                    bold.setSelected(false);
                } else {
                    StyleConstants.setBold(set, true);
                    bold.setSelected(true);
                }
                textPane.setCharacterAttributes(set, true);
            }
        });

code for moving text from the input pane to the display pane :

getInput.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String input = textPane.getText();
                textPane.setText("");
                if(!input.endsWith("\n")){
                    input+="\n";
                }
                StyledDocument doc = displayPane.getStyledDocument();
                int offset = displayPane.getCaretPosition();
                try {
                    doc.insertString(offset, input, set);
                } catch (BadLocationException ex) {
                    Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
Giannis
  • 5,286
  • 15
  • 58
  • 113
  • please what did you return Document, continuation of the http://stackoverflow.com/questions/9022366/keeping-the-correct-style-on-text-retrieval – mKorbel Jan 27 '12 at 21:36
  • Yes I have improved the question so the problem and what i have done is clearer. – Giannis Jan 27 '12 at 21:37
  • (-: that nothing against your person:-) for potential answer – mKorbel Jan 27 '12 at 21:39
  • my question why (simplyfied the things) did you wan the copy Document with Attributes, setDocument/Attributes/padding in the Action or from source that came from – mKorbel Jan 27 '12 at 22:16
  • You mean why I am using the Document to copy the text ? This is because I do not want the messages already displayed on the displayPane to be removed (as it would happen with setText()) – Giannis Jan 27 '12 at 22:22
  • chat is two directional communications, on the one you wrote, on send you copy those words to the common area, where you can see your actions and theit reply from another channel, isn't it – mKorbel Jan 27 '12 at 22:27
  • Once this part is working and I know how to correctly copy the text, I will simply send it over the network so it will be displayed on other members displayPanes as well. – Giannis Jan 27 '12 at 22:32
  • @latusaki : If you may, post the pictures of what exactly is happening and then tell what you were expecting, will might add weight to your question. Regards – nIcE cOw Jan 28 '12 at 05:45
  • Sorry for late reply I wasn't able to log sooner. As I described on question , the style of the last char of the text was applied to the whole string when moved to the displayPane. Although the solution shown on @StanislavL answer is working. – Giannis Jan 29 '12 at 17:47

1 Answers1

4

Use the example to merge both Documents http://java-sl.com/tip_merge_documents.html

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks for this ! I guess since i need to send messages ill just send my document to other clients and the merge will happen there. – Giannis Jan 29 '12 at 17:40