2006-12-04

Bidi text input in Swing is enabled by default

I helped someone out last week when they had trouble with a text field in a Java application. The symptom was that they were using their application's find functionality, entering a search string, and the text had "suddenly" become right-justified in the field. At the same time, the caret had grown a little triangular ear.

It was obvious (to me; the user was mystified and convinced it was a problem with the application) that they'd accidentally switched to right-to-left text entry, but it wasn't obvious how to switch back. It was probably a toggle, but if they couldn't remember what they'd pressed in the first place – and they couldn't – that was of no help.

I tried searching the web, but didn't find anything explaining how to toggle between left-to-right and right-to-left text entry in a Java Swing application. This post is so that next time I need this information, I don't have to read the JDK source again.

It had to be part of the DefaultEditorKit, so I started looking there. Sure enough, there's a ToggleComponentOrientationAction there. It's in the collection of default actions, but its name isn't public, for some reason. (There are a few other actions whose names aren't public too.) Anyway, searching for the value of the constant, "toggle-componentOrientation", it turns out to be bound in all of the standard LAFs to "control shift o".

So if you're using a Java application and your text entry is suddenly right-justified and caret looks slightly strange, hit control-shift o and things will go back to normal.