944,127 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4347
  • Java RSS
Sep 7th, 2006
0

Spacings And Carriage Returns

Expand Post »
Hi everyone,

I currently have a JTextPane using a html document and i am trying to serialize the html document contained in the JTextPane to the disk but i have run into some problems.

One problem lets say for example if i were to press the space bar five times and then type some text into the JTextPane and serialize it, everything seems alright except that when i read back the html document the space where i had pressed for five times has dissappeared and the text that i typed in is just left justified.

Another problem i have lets say for example if i were to press the carriage return five times and then type some text into the JTextPane and serialize it, everything seems alright except that when i read back the html document the carriage returns where i had pressed for five times has dissappeared and the text that i typed in is just placed at the top of the document in the JTextPane.

Why this is happening i am not sure but i am providing a compilable example for you guys to see what actually the problems that i am currently facing.

Here is the compilable example.

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import javax.swing.text.*;
  8. import javax.swing.text.html.*;
  9.  
  10. public class JHTML implements ActionListener
  11.  
  12. {
  13.  
  14. JFrame fr = new JFrame ("Frame");
  15.  
  16. JLabel Label1 = new JLabel("Label1 ", SwingConstants.RIGHT);
  17.  
  18. JButton Button1 = new JButton("Load");
  19. JButton Button2 = new JButton("Save As");
  20.  
  21. JFileChooser FileChooser1 = new JFileChooser();
  22. JFileChooser FileChooser2 = new JFileChooser();
  23.  
  24. JTextPane TextPane1 = new JTextPane();
  25.  
  26. StyleSheet ss = new StyleSheet();
  27.  
  28. HTMLDocument htmldoc = new HTMLDocument(ss);
  29.  
  30. JScrollPane ScrollPane1 = new JScrollPane(TextPane1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  31. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  32.  
  33. HTMLEditorKit htmlkit = new HTMLEditorKit();
  34.  
  35. Dimension Size1 = new Dimension();
  36.  
  37. String SF = "";
  38.  
  39. public void initialize ()
  40. {
  41. Container pane = fr.getContentPane();
  42. pane.setLayout(new FlowLayout());
  43. fr.setSize(250,300);
  44. fr.setLocation(300,300);
  45. fr.setBackground(Color.lightGray);
  46. TextPane1.setEditorKit(htmlkit);
  47. htmldoc = (HTMLDocument)htmlkit.createDefaultDocument();
  48. ss = htmldoc.getStyleSheet();
  49. TextPane1.setDocument(htmldoc);
  50.  
  51. Size1.width = 500;
  52. Size1.height = 300;
  53. ScrollPane1.setPreferredSize(Size1);
  54. pane.add(ScrollPane1);
  55. pane.add(Button1);
  56. pane.add(Button2);
  57. pane.add(Label1);
  58.  
  59. fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60. Button1.addActionListener(this);
  61. Button2.addActionListener(this);
  62. fr.pack();
  63. fr.setVisible(true);
  64. }
  65.  
  66. public void open ()
  67. {
  68.  
  69.  
  70. if(FileChooser1.showOpenDialog(fr) != JFileChooser.APPROVE_OPTION)
  71. {
  72. return;
  73. }
  74.  
  75. try
  76. {
  77. File file1 = FileChooser1.getSelectedFile();
  78. SF = file1.toString();
  79. FileInputStream in = new FileInputStream(SF);
  80. htmldoc = (HTMLDocument)htmlkit.createDefaultDocument();
  81. htmlkit.read(in, htmldoc, 0);
  82. ss = htmldoc.getStyleSheet();
  83. TextPane1.setDocument(htmldoc);
  84. in.close();
  85. }
  86.  
  87. catch (Exception e)
  88. {
  89. Label1.setText("A document reading error has occured");
  90. }
  91.  
  92. }
  93.  
  94. public void saveas ()
  95. {
  96. if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
  97. {
  98. return;
  99. }
  100.  
  101. try
  102. {
  103. File file2 = FileChooser1.getSelectedFile();
  104. SF = (file2.toString() + ".html");
  105. FileOutputStream out = new FileOutputStream(SF);
  106. htmlkit.write(out, htmldoc, 0, htmldoc.getLength());
  107. out.close();
  108. }
  109.  
  110. catch (Exception e)
  111. {
  112. Label1.setText("A document writing error has occured");
  113. }
  114.  
  115. }
  116.  
  117. public void actionPerformed(ActionEvent event)
  118. {
  119. JComponent b = (JComponent)event.getSource();
  120.  
  121. if(b == Button1)
  122. {
  123. open();
  124. }
  125.  
  126. else if(b == Button2)
  127. {
  128. saveas();
  129. }
  130.  
  131.  
  132. }
  133. public static void main(String args[])
  134. {
  135. JHTML a = new JHTML();
  136. a.initialize();
  137. }
  138. }

My only idea i guess is that i must be serializing the document wrongly, but i tried to yahoo but came up with nothing.

I hope someone knows why this is happening as already i am out of ideas.

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Sep 7th, 2006
0

Re: Spacings And Carriage Returns

when parsing html browsers will remove leading and trailing whitespace. That's part of the required behaviour for them.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I need help immediately
Next Thread in Java Forum Timeline: New to CVS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC