Greetings,
I am trying to have Slick an Swing work together.
The upper part of my JFrame is a
CanvasGameContainer(
gameContainer), the bottom a
JTextField(
inputField).
The Problem: After starting the Application i can write in the JTextField and get a line printed when hitting 'Enter'.
But: After i click on the CanvasGameContainer an back on the JTextField i cannot type anymore.
I know this has something to do with setting/revoking focus. But after hours of search for a simple input field i am exhausted and need help

(i have to get this working with Swing, no TWL or alike)
Any help is very appreciated. As a thank you i'll promise i'll finish the tutorial on "MultiClient-Server with Slick"

Here is the relevant Code (working):
Code:
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import org.newdawn.slick.*;
public class Main extends JFrame {
private CanvasGameContainer gameContainer;
private JTextField inputField = new JTextField(15);
public Main() throws Exception {
setSize(new Dimension(800,600));
setTitle("Test");
final TextFieldListener tfListener = new TextFieldListener();
inputField.addActionListener(tfListener);
inputField.setSize(100, 30);
inputField.setFocusable(true);
gameContainer = new CanvasGameContainer(new BasicGame("Title") {
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
g.drawString("Test String", 100, 100);
}
@Override
public void init(GameContainer gc) throws SlickException {
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
if(gc.hasFocus()) {
Input input = gc.getInput();
if(input.isMousePressed(0)) {
System.out.println("Click");
}
} else {
inputField.addActionListener(tfListener);
inputField.enableInputMethods(true);
}
}
});
gameContainer.setSize(800,450);
JPanel p = new JPanel();
p.add(gameContainer);
p.add(inputField);
add(p);
}
public static void main(String args[]) throws Exception {
Main main = new Main();
main.setVisible(true);
main.startGame();
}
private void startGame() throws Exception {
gameContainer.getContainer().setAlwaysRender(true);
gameContainer.requestFocus();
gameContainer.start();
}
private class TextFieldListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String inputString = inputField.getText();
System.out.println(inputString);
inputField.setText("");
}
}
}