Enabling Floating TIP for Java Swing Apps on Tablet PCs (Part 2)

In part 1, I explained why Windows Tablet PC users don’t have a very user friendly experience when inputting text into Java Swing applications. I presented a prototype solution to the issue that had some problems. Since then, I’ve:

  • worked out some bugs
  • added support for JTextAreas
  • come up with a way to enable floating TIP on all JTextComponents in a Swing application
  • updated the demo application
  • decided to release some source code in case anyone want’s to critically review it

Adding JTextArea Support

In part 1,
the test applet only had text fields. I avoided text areas because they are often wrapped inside JScrollPanes/JViewports and this complicates things. I generalize the solution in this part so the demo and sources show that this is supported.

Use of the JFloatingPane

In my first attempt, I added the native TextField to the parent of the JTextField that had focus. This was necessary to ensure that the TextField was considered visible and would behave like a real TextField. But it had the bad side effect of potentially messing up the layout of the Container holding the JTextFields.

I remedied the problem by searching for the JRootPane parent of the JTextComponent, adding the native TextField to the back of the JLayeredPane.DEFAULT_LAYER layer so it will be visible but not interfere with the layout of the Swing text component’s Container.

Enabling for all JTextComponents in An App

In my first cut at the application, I explicitly installed FocusListeners on all Swing text components for which I wanted to enable the floating TIP. That is manageable in a small application, but fails to take care of text components in common dialogs like the JFileDialog.

I toyed with the idea of using byte code modification to, at runtime, inject code into the look and feel or into the Swing JTextComponent class itself to register the the FocusListener. But with a little research I found another [simpler] way be listen for all focus events on JTextComponents.

If your code is running in a JVM without a restrictive SecurityManager, you can add an AWTEventListener to the AWT event dispatcher that will receive all events of interest. The apps I’m working on are delivered with signed code and aren’t restricted. Further, since I ultimately plan to implement this Swing-Tablet workaround as a signed JRE extension to install only on Tablet PCs JREs, I’m content not to address the problem of running in a locked down applet.

I’m using the following code to listen for focus gained/lost events on all JTextComponents.

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusEvent;
import javax.swing.text.JTextComponent;

...

Toolkit.getDefaultToolkit().addAWTEventListener(
   new AWTEventListener()
   {
      public void eventDispatched(AWTEvent event)
      {
         FocusEvent fe = (FocusEvent)event;
         if(fe.getComponent() instanceof JTextComponent)
         {
            if(event.getID() == FocusEvent.FOCUS_GAINED)
               /*tell swing-tablet code to start workaround on JTextComponent that just got focus*/;
            else if(event.getID() == FocusEvent.FOCUS_LOST)
               /*tell swing-tablet code to stop workaround on JTextComponent that just lost focus*/;
         }
      }
   },
   AWTEvent.FOCUS_EVENT_MASK);

Since this code will only be run when running on a Windows system that has the floating TIP, it shouldn’t affect performance on other systems. Further, this approach does not seem to have any perceived performance impact on the applications I’ve tested it in.

The Updated Demo

The demo can be run in two ways.

1. Java Web Start Application or 2.Executable Jar

If you have Java Web Start installed, just click the “Launch” icon below.

Start

If your browser is not Java WebStart enabled, get it here.

  Download the following file, save it, then double click it.

   SOTPCDemo-2007-04-25.jar

If it does not start when you double click it, then open a command prompt and run

   java -jar SOTPCDemo-2007-04-25.jar

from the directory where you saved it.

You should expect to see an application that looks like this.

When putting focus on swing text fields or areas the floating TIP button should appear near the cursor and move with it as you type with a keyboard if you have one attached.

If you have trouble running this demo or if it doesn’t work as advertised, please let me know.

The Source

You can download the sources (bundled with the compiled jar) here:

  SOTPCDemo-2007-04-25.zip

The code is provided as is via the The MIT License. I plan to maintain it, so please work with me if you have suggestions or enhancements.

Coming up in Part 3

  • Probably some bug fixes as I get feedback
  • Providing Swing analog to Window’s right-click context menu that provides Copy/Cut/Paste/Select All/Delete menu items
  • Installing it as a JRE extension so that all Swing apps on your Tablet PC will become floating TIP enabled. I’m not sure exactly how I’ll achieve this, but I’ll figure something out.

This entry was posted in UMPC. Bookmark the permalink.

4 Responses to Enabling Floating TIP for Java Swing Apps on Tablet PCs (Part 2)

  1. Jeff Jackson says:

    Sadly, here it is almost 4 years later and still no Java support for the TIP, let alone multitouch. 🙁

  2. Jeff says:

    Hi

    I’ve been trying to get this code working with windows 7, but the native Text Field on ever seems to get the last letter in the string as a “KeyTyped” when you use the hand writing panel in TabTip.

    Have you managed to get this code working on windows 7?

    Thanks

Leave a Reply