Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

append content to jTextPane

Posted on 2005-04-03
31
Medium Priority
?
1,570 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

578 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