Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

JTextPane line wrapping problem

Thread view: 
fahruz@hotmail.com - 04 Feb 2005 11:29 GMT
Line wrapping should normally occur when inserting a document in a
JTextPane, right?
Well for some reason it doesn't happen when inserting an HTML document
read from a File. If you execute the code below, the content of the
HTML page is displayed in a single line. Embedding the JTextPane inside
a JScrollPane didn't change a thing.

public class TextComponentDemo extends JFrame {
   JTextPane textPane;

   public TextComponentDemo() {

       textPane = new JTextPane();
       try {
       HTMLEditorKit editorKit = new HTMLEditorKit();
       HTMLDocument HTMLDoc =
(HTMLDocument)editorKit.createDefaultDocument();
       FileReader in = new FileReader("simple.html"); // Simple HTML
file with normal English text
       editorKit.read(in, HTMLDoc, 0);
       textPane.setPreferredSize(new Dimension(200, 200));
       textPane.setMaximumSize(new Dimension(200, 200));
       textPane.setSize(new Dimension(200, 200)); // setting all those
size attributes didn't change anything :(
       textPane.setDocument(HTMLDoc);}
       catch(Exception e) {e.printStackTrace();}

       JScrollPane scrollPane = new JScrollPane(textPane);
       scrollPane.setPreferredSize(new Dimension(200, 200));
       getContentPane().add(scrollPane);
   }

   private static void createAndShowGUI() {

       final TextComponentDemo frame = new TextComponentDemo();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.pack();
       frame.setVisible(true);
   }

   public static void main(String[] args) {
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
           }
       });
   }
}

---

Any idea why this happens?
Roland - 04 Feb 2005 12:53 GMT
> Line wrapping should normally occur when inserting a document in a
> JTextPane, right?
[quoted text clipped - 48 lines]
>
> Any idea why this happens?

Use
    textPane.setContentType("text/html");
just before
    textPane.setDocument(HTMLDoc);

However, instead of creating a HTMLDocument yourself, it's probably
easier and more reliable to use setPage(URL). The try block would then
have to be
   try {
      textPane.setContentType("text/html");
      textPane.setPage(new File("simple.html").toURL());
   } catch (... etc ...)

I believe that you even don't need setContentType(...);
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

fahruz@hotmail.com - 07 Feb 2005 01:38 GMT
Great! With the setContentType("text/html") it worked fine. Thanks a
lot!
As for your other tip unfortunately I cannot use it within my
application because my program is in fact a tad more complicated (I
produced a sample code here with what I thought to be the core of the
problem, for the sake of explanation). Actually I have a Document that
is passed via a TransferHandler to the JTextPane. This Document can be
any subclass of Document, including but not limited to HTMLDocument.
However I guess it's ok if I include a "(if Document instanceof
HTMLDocument) textPane.setContentType("text/html");" in my class
without adding to much specificity.

Many thanks again.

F.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.