2

I have following problem:

I display an HTML-Document with an JTextPane.

In my HTML-Text there are ­ (shy at w3.org) to make a soft-hyphenation. My problem is, that no hyphenation appears. Is there some kind of flag, which I don't know, to use these option?

Following Programm will show the problem:

package com.dvelop.ckue.swing;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;

public class SwingGui extends JFrame {

    public static void main(String[] args) {
        SwingGui sg = new SwingGui();
        sg.setSize(new Dimension(200, 800));
        sg.setPreferredSize(new Dimension(200, 800));
        sg.pack();
        sg.setVisible(true);
        sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private SwingGui() {
        super();
        setLayout(new FlowLayout());

        // No "-" appears, but a linebreak
        add(createField("<html>longlong<br>longlong<br>longlonglonglonglonglonglonglonglonglongWord"));
        // No linebreak, but the hyphenationsymbol
        add(createField("<html>longlong&shy;longlong&shy;longlonglonglonglonglonglonglonglonglongWord"));
        // Linebreak, but not where expected and no symbol            
        add(createField("<html>longlong&#8203;longlong&#8203;longlonglonglonglonglonglonglonglonglongWord"));
        // No linebreak, no symbol
        add(createField("<html>longlonglonglonglonglonglonglonglonglonglonglonglonglongWord"));
    }

    private JTextPane createField(String content) {
        JTextPane field1 = new JTextPane();
        field1.setPreferredSize(new Dimension(100, 200));
        field1.setAutoscrolls(true);
        field1.setEditorKit(new HTMLEditorKit());
        field1.setText(content);
        return field1;
    }

}

My expected behavior is, that my text will be broken to the next line:

longlong- 
longlong-
longlonglonglongWord

As seem by the first block, but with an hyphenation-sign.

EDIT: It will work in most browsers, but I don't use a webbrowser here.

EDIT 2: I use a JTextPane, I don't know, if Java will use some installes HTML rendering-engine internally.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79

2 Answers2

3

Many browsers do not handle this character. It is preferable to use the name of the entity (&shy;) instead of the ISO numeric entity (&#173;).

But this entity is pretty bad handled by most browsers.

&#8203; is just a zero width space character.

The simpler is: (but the hyphen is always visible...)

<p>longlonglong-&#8203;longlonglong</p>

And you can even try this (but I don't think that you see your hyphen...):

<p>longlonglong&shy;&#8203;longlonglong</p>

But I don't understand why you insert plain-text just after an <html> node, this should not ease the task of the browser, isn't it?

Otherwise, on what browser(s) do you test your code? Because this work perfectly on the lastest versions of Chrome and Firefox:

<p>longlonglong&shy;longlonglong</p>

Anyway you'll may be interested by the <wbr> tag, and this article dedicated to the soft hyphen problem...

Community
  • 1
  • 1
Pascal Qyy
  • 4,442
  • 4
  • 31
  • 46
  • Well, I don't display this within a browser. In fact it is the display of my compancys licence information during the installation of an application. The installer is a Java-Swing application. The licenceinformation is a wellformed HTML-document (it to long to show it here) – Christian Kuetbach Nov 12 '10 at 14:30
  • Ok, so, what is the html render engine (Cobra, Webkit, Gecko, Trident...), and what DTD do you use to validate your well formed HTML? – Pascal Qyy Nov 12 '10 at 20:54
  • The rendering engine is the build-in engine `java.swing.JTextPane`. I'm unsure which DTD is used, I'll take a look at monday. – Christian Kuetbach Nov 14 '10 at 14:39
1

http://java-sl.com/Hyphenation_In_JEditorPane.html That's an example of custom hyphnation. You can use the same approach and change the HTMLEditorKit to use your hyphens.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • With your answer I would be able to do, what I wanted to do. But I changed somethin within my buildprocess, so I don't need to change the hyphenation. (In fact I removed all `­`) – Christian Kuetbach Nov 18 '10 at 09:00