User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,001 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,197 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1315 | Replies: 1
Reply
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Help Spacings And Carriage Returns

  #1  
Sep 7th, 2006
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.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class JHTML implements ActionListener

{

	JFrame fr = new JFrame ("Frame");

	JLabel Label1 = new JLabel("Label1                            ", SwingConstants.RIGHT);

	JButton Button1 = new JButton("Load");
	JButton Button2 = new JButton("Save As");

	JFileChooser FileChooser1 = new JFileChooser(); 
	JFileChooser FileChooser2 = new JFileChooser();

	JTextPane TextPane1 = new JTextPane();

	StyleSheet ss = new StyleSheet();

	HTMLDocument htmldoc = new HTMLDocument(ss);

	JScrollPane ScrollPane1 = new JScrollPane(TextPane1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
	ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

	HTMLEditorKit htmlkit = new HTMLEditorKit();     

	Dimension Size1 = new Dimension();

	String SF = "";

	public void initialize ()
	{ 
		Container pane = fr.getContentPane();
		pane.setLayout(new FlowLayout());
		fr.setSize(250,300);
		fr.setLocation(300,300);
		fr.setBackground(Color.lightGray);
		TextPane1.setEditorKit(htmlkit);
		htmldoc = (HTMLDocument)htmlkit.createDefaultDocument();
		ss = htmldoc.getStyleSheet();
		TextPane1.setDocument(htmldoc);

		Size1.width = 500;
		Size1.height = 300;
		ScrollPane1.setPreferredSize(Size1);
		pane.add(ScrollPane1);
		pane.add(Button1);
		pane.add(Button2);
		pane.add(Label1);

		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Button1.addActionListener(this);
		Button2.addActionListener(this);
		fr.pack();
		fr.setVisible(true);
	}

	public void open ()
	{


		if(FileChooser1.showOpenDialog(fr) != JFileChooser.APPROVE_OPTION)
		{
			return;
		}

		try
		{
			File file1 = FileChooser1.getSelectedFile();
			SF = file1.toString();
			FileInputStream in = new FileInputStream(SF);
			htmldoc = (HTMLDocument)htmlkit.createDefaultDocument(); 
			htmlkit.read(in, htmldoc, 0);
			ss = htmldoc.getStyleSheet();
			TextPane1.setDocument(htmldoc);
			in.close();
		}

		catch (Exception e)
		{
			Label1.setText("A document reading error has occured");
		}

	}

	public void saveas ()
	{
		if(FileChooser1.showSaveDialog(fr) != JFileChooser.APPROVE_OPTION)
		{
			return;
		}

		try
		{
			File file2 = FileChooser1.getSelectedFile();
			SF = (file2.toString() + ".html");
			FileOutputStream out = new FileOutputStream(SF);
			htmlkit.write(out, htmldoc, 0, htmldoc.getLength());
			out.close();
		}

		catch (Exception e)
		{
			Label1.setText("A document writing error has occured");
		}

	}

	public void actionPerformed(ActionEvent event)
	{
		JComponent b = (JComponent)event.getSource();

		if(b == Button1)
		{
			open();
		}

		else if(b == Button2)
		{
			saveas();
		}


	}
	public static void main(String args[])
	{
		JHTML a = new JHTML(); 
		a.initialize();
	}                   
}

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
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Spacings And Carriage Returns

  #2  
Sep 7th, 2006
when parsing html browsers will remove leading and trailing whitespace. That's part of the required behaviour for them.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC