How to put properly a libgdx application inside swing application?

This is how I do it:

public class EditorApp extends JFrame {

    public EditorApp() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Container container = getContentPane();
        container.setLayout(new BorderLayout());

        LwjglAWTCanvas canvas = new LwjglAWTCanvas(new MyGame(), true);
        container.add(canvas.getCanvas(), BorderLayout.CENTER);

        pack();
        setVisible(true);
        setSize(800, 600);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EditorApp();
            }
        });
    }
}

I also have some API on my game class making it easy to work with from my editor.


You have to use SwingUtilites.invokelater because postRunnable posts to the game loop which is not running. I would try to get a Component from MyGame and add this. If you return a Component you don't have a dep. to the LwjglCanvas. It's not that nice because now the MyGame Interface has a dep. to swing but it's worth a shot to see if its solves your problem.

Tags:

Java

Swing

Libgdx