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 / General / September 2006

Tip: Looking for answers? Try searching our database.

Spacings And Carriage Returns

Thread view: 
freesoft_2000 - 07 Sep 2006 07:30 GMT
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
Tor Iver Wilhelmsen - 07 Sep 2006 15:48 GMT
>                      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.

Not necessary: What you are experiencing is a feature of HTML where
whitespace (spaces, tabs, linefeeds etc.) is compacted.

You need to tell the HTML renderer to respect whitespace and line
shifts, either using a <pre> element or through CSS properties.
freesoft_2000 - 07 Sep 2006 17:22 GMT
Hi everyone,

             Tor Iver Wilhelmsen, you said
-You need to tell the HTML renderer to respect
-whitespace and line
-shifts, either using a <pre> element.

How do i do this, because it seems that the default java html kit uses
<p>, </p> as its default for everything even carriage returns.

How do i tell the HTML renderer to respect these conditions.

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Tor Iver Wilhelmsen - 08 Sep 2006 15:38 GMT
> How do i do this, because it seems that the default java html kit uses
> <p>, </p> as its default for everything even carriage returns.

You need to add a <pre> HTML element to the underlying document under
a given condition, e.g. a GUI event from a button or menu item. Then
you could pick up the contents and the selection, add "<pre>" before
the selection start and "</pre>" after selection end, then set that
back as the document contents.


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.