Mouse Input in JMEDesktop

Hi all.

I've based my GUI implementation on the example found in HelloJMEDesktop.java, together with my BaseGame. The example worked fine, but i'm now having trouble receiving events from my swing components (in the example below, "clicked!" never happens).

I've done quite alot of swing programming before (non jME), but this implementation is giving me problems.

I think it boils down to something pretty simple, like me doing something wrong with the InputHandler(), but i can't seem to get it right…

The initialization itself is almost straight from, HelloJMEDesktop.java, i even left the "button" in there for debug purposes.


public GUIFrame(DisplaySystem display, final InputHandler input)
    {
        setRenderQueueMode( Renderer.QUEUE_ORTHO );
        // create the desktop Quad
        bottomPanel = new UnsafeJMEDesktop( "bottom panel", display.getWidth()-128, 128, guiInput);

        // and attach it to the gui node
        attachChild(bottomPanel);

        guiInput = new InputHandler();
        guiInput.setEnabled(true);

        bottomPanel.getLocalTranslation().set( bottomPanel.getWidth() / 2, 0, 0);
       
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                bottomPanel.getJDesktop().setBackground( new Color( 0.3f, 0.3f, 0.3f, 1f ) );

                // create a swing button
                final JButton button = new JButton( "click me" );
                // and put it directly on the desktop
                bottomPanel.getJDesktop().add( button );
                // desktop has no layout - we layout ourselfes (could assign a layout to desktop here instead)
                button.setLocation( 20, 0 );
                button.setSize( button.getPreferredSize() );
                // add some actions
                // standard swing action:
                button.addActionListener( new ActionListener() {
                    public void actionPerformed( ActionEvent e ) {
                        // this gets executed in swing thread
                        // alter swing components ony in swing thread!
                        button.setLocation( FastMath.rand.nextInt( 400 ), FastMath.rand.nextInt( 300 ) );
                        System.out.println( "clicked!" );
                    }
                } );
                // action that gets executed in the update thread:
                button.addActionListener( new JMEAction( "my action", guiInput ) {
                    public void performAction( InputActionEvent evt ) {
                        // this gets executed in jme thread
                        // do 3d system calls in jme thread only!
                        updateRenderState(); // this call has no effect but should be done in jme thread :)
                    }
                });
                guiInput.addAction(new InputAction() {
                public void performAction(InputActionEvent evt) {
                    System.out.println(evt.getTriggerName());
                }
            }, InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_ALL,
                    InputHandler.AXIS_NONE, false);
            }
        } );



I then attach the gui node to my PassManager, and do an update every frame since i have some counters and things there that need to be updated.
Here's the update method:

public void update(final float tpf)
    {
        SwingUtilities.invokeLater( new Runnable() {
        public void run()
        {
            guiInput.update(tpf);
            updateGeometricState(tpf, true);
            getBottomPanel().getJDesktop().repaint();
           
            updateRenderState();       
        }});
    }



If anyone has any ideas or could point me to a thread that would help, i would be most grateful.

Thanks!

I noticed i created guiInput after i used it in bottomPanel().

Doing it before rewarded me with the following exception:


Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
        at java.awt.EventQueue.invokeAndWait(EventQueue.java:980)
        at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1323)
        at com.jmex.awt.swingui.JMEDesktop.onMove(JMEDesktop.java:490)
        at com.jmex.awt.swingui.JMEDesktop$XUpdateAction.performAction(JMEDesktop.java:1194)
        at com.jme.input.ActionTrigger.performAction(ActionTrigger.java:264)
        at com.jme.input.InputHandler.processTriggers(InputHandler.java:425)
        at com.jme.input.InputHandler.update(InputHandler.java:410)



Which i guess i related to this thread:

http://jmonkeyengine.com/forum/index.php?topic=11573.0

So i'll go on reading there.

Turns out it was StrategicHandler hijacking the mouse cursor.



Lex says in this thread: http://www.jmonkeyengine.com/forum/index.php?topic=5365.15



Re: Input handler for strategy games