Using & shy; in a Java component that supports HTML HTML

I have the following problem:

I am showing an HTML document with JTextPane.

My HTML text has ­ (shy on w3.org) make a soft carry. My problem is that the transfer does not occur. Is there any flag that I don't know to use this option?

The following program 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; } } 

I expect my text to be broken into the following line:

 longlong- longlong- longlonglonglongWord 

It looks like the first block, but with a sign of transfer.

EDIT: It will work in most browsers, but here I do not use a web browser.

EDIT 2: I am using JTextPane, I do not know if Java will use some HTML rendering engine settings inside.

+2
source share
2 answers

http://java-sl.com/Hyphenation_In_JEditorPane.html This is an example of custom hyphnation. You can use the same approach and modify the HTMLEditorKit to use your hyphens.

+1
source

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

But this object is pretty poorly handled by most browsers.

&#8203; is just a space character wide.

The easiest way: (but the hyphen is always visible ...)

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

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

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

But I don’t understand why you are inserting plain text right after the <html> node, this should not make the browser task easier, right?

Otherwise, in which browser (s) do you check your code? Because it works fine on the latest versions of Chrome and Firefox:

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

In any case, you might be interested in the <wbr> tag , but in this article on soft hyphen ...

+3
source

All Articles