• Post Reply Bookmark Topic Watch Topic
  • New Topic

preventing focus in swing gui  RSS feed

Bret Waldow
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm working out a handwriting recognition system for my Linux tablet. I'm finding out solutions to the various issues, and then I'll design it properly, once I know what's needed.

I've got much of it working, but I am having a problem with the gui. In order to work, it needs to not steal the focus from other windows - that way, java.awt.Robot can put keystrokes matching recognized characters into the target window, instead of into the recognizer itself.

I'm basing it on a JFrame, with some panels, buttons, etc. I mark all these .setFocusable( false ), and that keeps the focus off the gui, even though it has decorations and is re-sizable.

When the strokes are entered, the system gathers up groups (assuming they are words) and sends them off to the recognition engine, which for each group returns a list of likely candidates. I add each list as a JComboBox to a JTabbedPane for presentation to the user. The idea is that the tabs provide a simple horizontal list of results (looks like the sentence the user wrote), and the JComboBox allows choosing one of the alternate results if the user doesn't agree with the recognition engine's first choice.

Everything is fine up to this point - the focus is not stolen from the target (external) window, so keystrokes Robot emits would go to that target window. Clicking on any components with the mouse (stylus) does not set the focus on the recognition gui itself. Not drawing the strokes, not clicking the buttions, and not selecting the tabs with the results in them - to focus stays off.

Until the user actually activates one of the JComboBox listings, at which point the focus is given to the gui's frame. No good.

I instrumented the JComboxBox items while creating them:

...and I get this output:

But the focus grab doesn't occur at this point, it occurs when the user actually activates the JComboBox, so I presume some other Swing object is being created on the fly at that time.

How do I prevent these transient objects from being focusable, and grabbing the focus?

Another possibility is to use menus instead of the JTabbedPane, in the hope that is more controlable, but I haven't tried that yet.

Can anyone explain what is being created (at run time, presumably) and what I can do to keep it out of the focus loop entirely?

Thanks in advance,
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!