Skip to main content

Bidi anomaly mixing ltr and rtl text

2 replies [Last post]
Joined: 2003-07-17


I noticed that when mixing ltr text and rtl text, parenthesis may confuse bidi. Please see example below.

I read bug 6502834 but this problem seems different: my problem applies both to Arabic and Hebrew. 6502834 is documented to be a duplicate of bug 6449486 that describes a problem with Hindi text.

The demo code also demonstrates a work around, but that will work only on components that accept html text (like JLabel).



<br />
import java.awt.GridLayout;<br />
import java.text.MessageFormat;<br />
import java.util.Locale;</p>
<p>import javax.swing.JFrame;<br />
import javax.swing.JLabel;<br />
import javax.swing.JPanel;<br />
import javax.swing.SwingUtilities;<br />
import javax.swing.border.TitledBorder;</p>
<p>public class BidiAnomaly {</p>
<p>    private static final String plainFormat = "{0} {1}";</p>
<p>    private static final String htmlFormat = "{0}{1}";</p>
<p>    public static void main(String[] args) {<br />
	SwingUtilities.invokeLater(new Runnable() {<br />
	    @Override<br />
	    public void run() {<br />
		new BidiAnomaly().test();<br />
	    }<br />
	});<br />
<p>    private void createLocaleLabels(JPanel panel, Locale locale) {<br />
	String rootName = locale.getDisplayName(Locale.ROOT);<br />
	String localeName = locale.getDisplayName(locale);<br />
	JLabel label;<br />
	label = new JLabel(MessageFormat.format(plainFormat, rootName,<br />
		localeName), JLabel.CENTER);<br />
	label.setBorder(new TitledBorder("Plain formatting"));<br />
	panel.add(label);<br />
	label = new JLabel(MessageFormat.format(htmlFormat, rootName,<br />
		localeName), JLabel.CENTER);<br />
	label.setBorder(new TitledBorder("HTML table formatting"));<br />
	panel.add(label);<br />
<p>    private void test() {<br />
	JFrame frame = new JFrame(this.getClass().getSimpleName());<br />
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
	JPanel panel = new JPanel(new GridLayout(0, 1));<br />
	frame.add(panel);<br />
	createLocaleLabels(panel, new Locale("ar", "EG"));<br />
	createLocaleLabels(panel, new Locale("iw", "IL"));<br />
	createLocaleLabels(panel, new Locale("nl", "NL"));<br />
	frame.setSize(400, 400);<br />
	frame.setLocationRelativeTo(null);<br />
	frame.setVisible(true);<br />
    }<br />
}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-01-13

You might want to take a look at the recently-released W3G doc that discusses this exact problem:

Granted, this discusses the problem in HTML, but it manifests the same way. The article discusses handling mirrored parenthesis with the correct unicode characters. It also discusses control characters (RLO, LRO) you can use to override the direction for a block of text.

Also, if you haven't done so, you may want to check out the Java Bidi class and see if it can help you maintain directional integrity.

Joined: 2003-07-17

Hi markwoodman,

Thanks for the pointer.