Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does HTMLDocument ignore white spaces

I have a problem. It looks like that HTMLEditorKit just ignore white spaces. There is my sample code:

public class TestEditor extends JFrame {

public TestEditor(){
    createConnection();
    createGUI();
}
private void createGUI(){
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JScrollPane scroll1=new JScrollPane(text);
    JScrollPane scroll2=new JScrollPane(html);
    JSplitPane split=new JSplitPane();
    split.setLeftComponent(scroll1);
    split.setRightComponent(scroll2);
    split.setDividerLocation(0.5);
    split.setResizeWeight(0.5);
    getContentPane().add(split);
    setTitle("Test");
    setPreferredSize(new Dimension(600,300));
    pack();
}
private void createConnection(){
    text=new JTextPane();
    html=new JTextPane();
    html.setContentType("text/html");
    html.getStyledDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            update();
        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            update();
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            update();
        }
        private void update(){
            if(fromText) return;
            fromHtml=true;
            text.setText(html.getText());
            fromHtml=false;
        }
    });
    text.getStyledDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            update();
        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            update();
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            update();
        }
        private void update(){
            if(fromHtml) return;
            fromText=true;
            html.setText(text.getText());
            fromText=false;
        }
    });
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TestEditor().setVisible(true);
        }
    });
}
private JTextPane text;
private JTextPane html;
private boolean fromHtml, fromText;
}

This code creates a simple application containing two JTextPane text fields. User is allowed to write in the right editor and then he can see a HTML code on the left side. Additionally, he can edit the code and see the changes on the right side.

There is the problem. Let's assume that I wrote the text

"     aaaa"

in the right editor (five spaces before "aaaa"). This would present itself well. However if I add next 'a' in HTML editor (on the left side) I lose my 5 spaces and see only "aaaaa" on the right side.

Is there any way to fix this problem, so that my 5 spaces wouldn't be lost?

like image 975
bakala12 Avatar asked Aug 10 '26 11:08

bakala12


2 Answers

HTML strips all tabs, spaces and makes them in a single space.

The HTML code/entity for a space is   and for a tab 	

If you wish to preserve all spaces when double space is used you could consider using this:

String spaces = text.getText()
                .replace("  ","  ")
                .replace("\t","	");
html.setText(spaces);

This way you replace tabs for 	 HTML entity, and double spaces for two HTML spaces. Single spaces will be left alone as they will render properly.

like image 68
Tschallacka Avatar answered Aug 13 '26 02:08

Tschallacka


Probably because it does not recognize the character space, but sees it as empty spaces. You can change it to  , which is the HTML-code for a space.

like image 26
Robbenu Avatar answered Aug 13 '26 00:08

Robbenu