How to manually set focus to swing component?

Hi, I want to set focus to JTextArea when enter is pressed. Like a input textbox to write stuff to other players.



I have…



public class MessageTypingAction extends KeyInputAction{
   boolean isTyping=false;
   
   @Override
   public void performAction(InputActionEvent evt) {
      IngameHUD.getInstance().infoTextArea.append("enter pressed");

      IngameHUD.getInstance().infoTextArea.requestFocus();
      if(isTyping){
         
      }
      else{
         
      }      
   }



which works when enter is pressed the output comes, but the focus is not gained to that textarea.

I built the textarea like this:


       final JMEDesktop desktop = new JMEDesktop( "desktop", DisplaySystem.getDisplaySystem().getWidth() / 2, DisplaySystem.getDisplaySystem().getHeight() / 4, GTASGame.getInstance().input );
        // and attach it to the gui node
        this.attachChild( desktop );
       
        desktop.getLocalTranslation().set( desktop.getWidth()/2, DisplaySystem.getDisplaySystem().getHeight()-desktop.getHeight()/2, 0);

        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                // make it transparent blue
                desktop.getJDesktop().setBackground( new Color( 0, 0, 0, 0.0f ) );
infoTextArea = new JTextArea(5,50);
      infoTextArea.setBackground(new Color(238,238,238));
      infoTextArea.setAutoscrolls(true);
      infoTextArea.setEditable(true);
                infoTextArea.setFocusable(true);
         
                infoTextArea.setSize(infoTextArea.getPreferredSize());
                JScrollPane scrollPane = new JScrollPane(infoTextArea);
                scrollPane.setOpaque(false);
                scrollPane.setLocation(10,10);
                scrollPane.setSize(scrollPane.getPreferredSize());
                desktop.getJDesktop().add(scrollPane);



Focus is gained though when i click on the textarea with mouse.
How can I make that component gain focus?


The, IngameHUD.getInstance().desktop.setFocusOwner(IngameHUD.getInstance().infoTextArea), gives me exception:



SEVERE: Exception in game loop
java.lang.IllegalStateException: not in swing thread!
   at com.jmex.awt.swingui.JMEDesktop.dispatchEvent(JMEDesktop.java:669)
   at com.jmex.awt.swingui.JMEDesktop.setFocusOwner(JMEDesktop.java:891)
   at gtas.player.actions.MessageTypingAction.performAction(MessageTypingAction.java:15)
   at com.jme.input.ActionTrigger.performAction(ActionTrigger.java:264)
   at com.jme.input.ActionTrigger$CommandTrigger.performAction(ActionTrigger.java:291)
   at com.jme.input.InputHandler.processTriggers(InputHandler.java:426)
   at com.jme.input.InputHandler.update(InputHandler.java:411)
   at gtas.PlayerInputHandler.update(PlayerInputHandler.java:41)
   at gtas.GTASGame.update(GTASGame.java:189)
   at com.jme.app.BaseGame.start(BaseGame.java:84)
   at gtas.GTASGameServer$1.run(GTASGameServer.java:36)



Can anybody help me?

Maybe use SwingUtilities.invokeLater() when you set the focus too ?

Tx for the reply but i tried it, program didnt crash but it didnt have any effect. Maybe anybody can point me to some jmetest for such functionality?



I'm gonna post my code just in case i did something wrong, the action is called when enter is pressed… . Currently I make the mouese visible to navigate to JTextArea.



public class MessageTypingAction extends KeyInputAction{
   boolean isTyping=false;
   String tempStr;
   
   @Override
   public void performAction(InputActionEvent evt) {   
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
               IngameHUD.getInstance().desktop.setFocusOwner(IngameHUD.getInstance().infoTextArea);
               tempStr = IngameHUD.getInstance().chatBox.getText();
            }
        });
      
      if(isTyping){      
         if(tempStr.length()>0){
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setText(tempStr);
            if(GTASGame.getInstance().client!=null) GTASGame.getInstance().client.broadcast(chatMessage);
            else
            if(GTASGame.getInstance().server!=null) GTASGame.getInstance().server.sendToAllExcept(chatMessage, chatMessage.getPlayerId());
            IngameHUD.getInstance().infoTextArea.setText(GTASGame.getInstance().player.getName()+">> "+tempStr+"n"+IngameHUD.getInstance().infoTextArea.getText());
            IngameHUD.getInstance().chatBox.setText("");
         }
         else{
            isTyping = false;
            MouseInput.get().setCursorVisible( false );      
         }
      }
      else{
         isTyping = true;
         MouseInput.get().setCursorVisible( true );
      }   
      
      KeyInput.get().clearKey(KeyInput.KEY_RETURN);
   }
}