?
Solved

append content to jTextPane

Posted on 2005-04-03
31
Medium Priority
?
1,568 Views
Last Modified: 2008-02-01
I want to take the contents of a jTextPane, and add it to the end of another jTextPane.

It sounds easy enough, but I want to *bring along all of the formatting* as well.  Ideally what I would like to do is simply take the contents of

(StyledDocument)MyJTextPane.getDocument();

and add it to the endof the other jTextPane, but I have not found any methods to allow this.  I messed around with the getComponent addComponent methods of jTextPane, but I'm not sure they are what I need.  Thanks!
0
Comment
Question by:ARACK04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 14
  • 3
31 Comments
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693538
post your code and i'll fix it for you!
0
 

Author Comment

by:ARACK04
ID: 13693573
for(int i = 0, j = TextPane1.getComponentCount(); i < TextPane3.getComponentCount(); i++, j++)
     TextPane1.add(TextPane3.getComponent(i), j);

I want to add the entire content of TextPane3 to the end of TextPane1.  This is the closest thing I could find in the javadocs that looks like what I wanted.
       
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693593
first of all what is inside your 3rd JTextPane? just text?

and is your 1st JTextPane empty?


i meant post your full code by the way!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Expert Comment

by:ellandrd
ID: 13693602
p.s do you wnat tyo place the contents of the 3rd Jtextpane to the 1st jtextpane all the time or just at certian times?
0
 

Author Comment

by:ARACK04
ID: 13693612
There is no way I can post my full code, it's too big.  

1st jTextPane is NOT empty, and the third contains formatted text, color, etc.  If it were just text it would be EASY!!
0
 

Author Comment

by:ARACK04
ID: 13693617
Just at certain times.  I click a button, and I want to append the contents of the TextPane3 to the end of TextPane1
0
 

Author Comment

by:ARACK04
ID: 13693632
Just to be clear, I want to bring all formatting over too, so I can't just use the getText setText() methods/
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693636
ok on a button event, better, whole on
0
 

Author Comment

by:ARACK04
ID: 13693652
I don't need the event handler or anything like that.  Just how to grab the whole contents of a jTextPane and append it to another.  :)
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693698
here is an exmaple:

buttonLoad.addActionListener( new ActionListener()
{
         public void actionPerformed( ActionEvent e )
         {
                   // step 1 get the contents from the 3rd JTextPane
                   // step 2 assign it to some global variable
                   // step 3 set the 1st JTextPane equal the global variable
                   // remeber the global var contains the 3rd JTextPanes contents
                   // use getText() method to get the content          
                   // use setText() method to set the contents again
                   // clear the global variable...
         }
} ) ;
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693725
also try

try {
        // Get the text pane's document
        JTextPane textPane = new JTextPane();
        StyledDocument doc = (StyledDocument)textPane.getDocument();
   
        // Create a style object and then set the style attributes
        Style style = doc.addStyle("StyleName", null);
       
        // Font size
        StyleConstants.setFontSize(style, 30);
   
        // Background color
        StyleConstants.setBackground(style, Color.blue);
   
        // Foreground color
        StyleConstants.setForeground(style, Color.white);
   
        // Append to document
        doc.insertString(doc.getLength(), "Some Text", style);
    } catch (BadLocationException e) {
    }

where some text might be the contents fo teh 3rd JTextPane...
0
 

Author Comment

by:ARACK04
ID: 13693728
How do I get the contents of a JTextPane which includes the formatting.  getText will only get a string, without formatting.
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693749
look here;

http://javaalmanac.com/egs/javax.swing.text/tp_StyledText.html

as i siad before using StyledDocument doc = (StyledDocument)textPane.getDocument();
0
 

Author Comment

by:ARACK04
ID: 13693778
Oh, whoops, I missed that post.  Sorry!
0
 

Author Comment

by:ARACK04
ID: 13693796
I guess the problem I am having is that I do not know how get the set of styles in the original jTextPane.  I know how to put text in with a certain style, but I don't know how to take the formatted text out.
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693809
format as in HTML?
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693812
private JTextPane editor;
  private DefaultStyledDocument doc;
  private StyledEditorKit kit;
...
...
editor = new JTextPane();
    doc = new DefaultStyledDocument();
    kit = new StyledEditorKit();
    editor.setEditorKit(kit);
    editor.setDocument(doc);
...
0
 

Author Comment

by:ARACK04
ID: 13693817
No.  No html.  I'm inserting text like so:

doc = (StyledDocument)jTextPane1.getDocument();

Style style = .........

doc.insertString(doc.getLength(), str, style);


I want to just grab the whole contents of the jTextPane, and drop it somewhere else and have the formatting still show up in the NEW location.

0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693844
look here:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_10255513.html?query=get+format+in+JTextPane&clearTAFilter=true

if every time you are taken the contents from 1 JTextPane into another, is the format going to be the same?

if so read the contents out line by line.

then when reading the contents into another JTextPane, apply that same format again?

See what i mean?
0
 

Author Comment

by:ARACK04
ID: 13693892
It won't.  I really really really need to know

*how to pull the complete formatted contents of a jTextPane, and APPEND it to another*
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693898
dont think you can just grab the entire formatted content in JTextPane and place them into another JTextPane just like that?
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 13693911
ok brb whole on a minute
0
 
LVL 92

Expert Comment

by:objects
ID: 13693980
How complex is the styling of the document? If is was simply a different style each line or paragraph then you could copy it across one line/parapgraph at a time and apply the approproiate style.
0
 

Author Comment

by:ARACK04
ID: 13694083
There are different styles in each line.  
0
 

Author Comment

by:ARACK04
ID: 13694104
Is there any kind of method to append the contents of one StyledDocument onto the end of another?
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13694121
not that i'm aware of

you may need to do it character by character if each character may have a different style
0
 

Author Comment

by:ARACK04
ID: 13694386
hmmm, ok.  That should work.  I think this is small enough in scope such that it won't cost too much.  I think I remember seeing some methods that returned a style for a certain character, or caret position or something.  I'll take a look.
0
 

Author Comment

by:ARACK04
ID: 13694630
Sweet.  Looks like the methods I need are in the JTextPane class:

myTextPane3.setCaretPosition(NUM);
AttributeSet s = myTextPane3.getCharacterAttributes();
String str = myTextPane3.getText();
str = str.substring(JUNK, ++JUNK);
        try{
        doc.insertString(doc.getLength(), str,  s);

Oh well, it will be tedious program this, but it will work.  I should be able to find chunks of characters that have the same Style, and send them over all together.  Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question