Spaces in JTextPane

Hi...

I have a issue using JTextPane.

When i given more than one space between characters and reseting the same HTML text, multiple spaces are converted to single spaces.

Steps to produce the above issue :-

1. Run the below.

2. Exter some text=>more than one space=>some text again.

3. Click "Show HTML" so that the above text area displays HTML.

4. Click "Rest Same HTML" button so that same HTML what is displayed in text area is set back to text pane. After setting to text pane multiple spaces is replaced by single space.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass HTMLPaneTestextends JFrame{

JTextPane editor =null;

JButton btnShowHTML =null;

JTextArea taHTMLDisplay =null;

public HTMLPaneTest()throws Exception{

editor =new JTextPane();

editor.setContentType("text/html");

editor.setEditable(true);

add(editor, BorderLayout.CENTER);

btnShowHTML =new JButton("Show HTML");

btnShowHTML.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

taHTMLDisplay.setText(editor.getText());

}

});

add(btnShowHTML, BorderLayout.SOUTH);

taHTMLDisplay =new JTextArea();

taHTMLDisplay.setBackground(Color.BLUE);

taHTMLDisplay.setForeground(Color.MAGENTA);

taHTMLDisplay.setEditable(false);

taHTMLDisplay.setPreferredSize(new Dimension(20, 20));

JScrollPane scroll =new JScrollPane(taHTMLDisplay);

scroll.setPreferredSize(new Dimension(100, 250));

add(scroll, BorderLayout.NORTH);

addWindowListener(new WindowAdapter(){

@Override

publicvoid windowActivated(WindowEvent e){

editor.requestFocusInWindow();

}

});

JButton btnResetHTML =new JButton("Rest Same HTML");

btnResetHTML.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

editor.setText(editor.getText());

}

});

add(btnResetHTML, BorderLayout.EAST);

setSize(500, 400);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setVisible(true);

}

publicstaticvoid main(String[] args){

try{

new HTMLPaneTest();

}catch (Exception e){

e.printStackTrace();

}

}

}

[4224 byte] By [Sarjaya] at [2007-11-27 11:53:36]
# 1

> When i given more than one space between characters

> and reseting the same HTML text, multiple spaces are

> converted to single spaces.

You have to parse editor.getText() and get all the text between the HTML tags and replace spaces with so that they will be retained. setText(textWithSpacesThatHaveBeenReplacedWith )

astigmatika at 2007-7-29 18:51:12 > top of Java-index,Desktop,Core GUI APIs...
# 2

astigmatik ... thats a good idea

is there anyway that i can have & nbsp; inserted when user presses "SPACE"/"TAB"?

Since & nbsp; retains the space after setting again.

Message was edited by:

Sarjay

Message was edited by:

Sarjay

Sarjaya at 2007-7-29 18:51:12 > top of Java-index,Desktop,Core GUI APIs...
# 3

> is there anyway that i can have & nbsp; inserted when user presses "SPACE"/"TAB"?

a) What if the user uses the "paste" functionality to add text to the text pane that contains multiple spaces?

b) I don't think you want to insert a & nbsp for every space only when you have multiple spaces.

Anyway the approach you would use would be to create a DocumentFilter so that you can parse and convert the text string before it is inserted into the Document.

Read the JTextPane API and follow the link to the Swing tutorial on "Text Component Features" for more information.

camickra at 2007-7-29 18:51:12 > top of Java-index,Desktop,Core GUI APIs...
# 4

camickr thank you for the information on DocumentFilter.

I tried with the above example. Overriding the replace(fb, offs, length, str, a) function of DocumentFilter

str = str.replaceAll(" ", " ");

super.replace(fb, offs, length, str, a);

Whats happening is that instead of space i am able to see & nbsp;.. Any suggestion?

> b) I don't think you want to insert a & nbsp for every space only when you have multiple spaces.

Can i know what is the best way of doing?

Sarjaya at 2007-7-29 18:51:12 > top of Java-index,Desktop,Core GUI APIs...
# 5

> Whats happening is that instead of space i am able to see & nbsp;.. Any suggestion?

I've never played with HTML Documents, so I don't know why its not working as expected.

camickra at 2007-7-29 18:51:12 > top of Java-index,Desktop,Core GUI APIs...