Forcing Focus-Switching

Hey, so I've got a nice chat-box which activates the text-editor when you hit enter. The chat-box does not, however, steal focus from the game, so if you hit enter and start typing, nothing happens.

I want it to have ORPG behaviour–enter opens chat-editor and you can type, then you hit enter again and focus returns to the game.

So far I haven't found any method regarding setting focus on anything, but maybe I'm not searching in the right place. If anybody could give me a pointer (preferably not a Null one) that would be great.

Use requestFocus()?

not available in any class that I need to use it in

What GUI library are you using and what object is representing the chat-box's text field?

EDIT: Oh… FengGUI right?


So when it's closed, is there any way for the game to request focus again? So the player can start running around?

EDIT: Found out. Just do guiDisp.setFocusedWidget(null) and it works.

Hey Trussel,

I'm having the same issue, except I'm using Swing elements for my chat box.

Essentially, when I click my mouse to the text area to type a chat message that first click makes my avatar move…which I obviously don't want. After the first click, focus is established and I have no issue typing messages in (i.e. avatar does not move within the world).

skyuzo said:

Use requestFocus()?

I believe that was a Swing solution.... What classes are you using?

The chat window is a TextArea and a TextField. The TextArea is placed in a JScrollFrame. The scroll frame and the TextField are then put into JInternalFrame.

Here's the code for the chat window:

    * Create a chat window
    * **********************************************************
   public void createSwingChatWindow(final JDesktopPane desktopPane,
         final String title) {

      final JInternalFrame chatWindowInternalFrame = new JInternalFrame(title);
      if (title == null) {
               "JInternalFrame.isPalette", Boolean.TRUE);
      chatWindowInternalFrame.setLocation(0, height - 250);
            new BoxLayout(chatWindowInternalFrame.getContentPane(),

      final JPanel listPane = new JPanel();
      listPane.setLayout(new BoxLayout(listPane, BoxLayout.LINE_AXIS));

      incomming = new JTextArea(10, 25);
      final JScrollPane scrollPane = new JScrollPane(incomming);

      outgoing = new JTextField(20);
      outgoing.addActionListener(new SendListener());




In my mouse-click-to-move code, I have something like this:

if ((button == 0) && (InGameUI.isFocus() == false) && (pressed == true))

<insert mouse-click-to-move-logic-here>

The behavior I'm getting is that it always takes one mouse click for the focus to switch from the game to the UI.

But if you force the focus with requestFocus(), shouldn't it not even require 1 click? Why use swing instead of Feng, btw?

I don't know Trussel re: requestFocus()…I'm still in the learning phase of things, but I will give it a try :wink:

Why am I using Swing instead of Feng? First, I'm not married to Swing right now, it's just what I'm trying it out at the moment to see how it integrates into my project. Also, Swing seems to be better supported then Feng, i.e. Java has a very large tutorial website to draw off of.

…is there any huge reason to choose Feng over Swing?

Feng is a kitty.

Swing is a turtle.

…as in Feng is cute and cudly and purrs when you scratch its tummy kind of kitty, or Feng meows constantly at midnight and throw-up in your shoes kind of kitty?

Considering how hard I had to work to get it running, I would go with the latter… but at least Feng can make it to your shoes in a single update(), where Mr. Swing-the-Turtle would take 45 updates to even get there.

Are there any good tutorials for Feng that would stop it from throwing up in my shoe?

Yeah there are a few Wiki's which might tell you how to coax it.

Hey Trussel,

I'm giving Fengui a try, and I got some buttons and a window up there now…just to fool around with it.

Question: do you have to guiDisp.setFocusedWidget() on every element? Or is there some global way of watching for focus when the mouse is focused on any Fenggui element?

I use the FengJMEInputListener in the Wiki, and I add it to my listeners by doing input.addToAttachedListeners(guiInput);

I only use guiDisp.setFocusedWidget() when I am focusing / unfocusing the chat-box (via the enter key) at the moment.

Trussell said:

I use the FengJMEInputListener in the Wiki, and I add it to my listeners by doing input.addToAttachedListeners(guiInput);

I only use guiDisp.setFocusedWidget() when I am focusing / unfocusing the chat-box (via the enter key) at the moment.

Quick question, I'm working on a similar thing right now and I'm new to FengGui and JME. I'm creating a class for a window that will be a seach query box and has a button and a text area. I'm using a similar approach to this, but I don't undersand what guiInput is set to before you attached it to the handler. I set the variable input to be of type FengJMEInputListener, but not quite sure what type is guiInput.


guiInput is the variable's name in my program specifically… guiInput = new FengJMEInputListener(guiDisp) is what I call, where guiDisp is my org.fenggui.Display. The FengJMEInputListener watches all events happening on the org.fenggui.Display that it was created with.