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();
}
}
}

