Slick Forums

Discuss the Slick 2D Library
It is currently Sun May 26, 2013 1:24 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Apr 23, 2012 11:25 pm 
Offline

Joined: Wed Oct 12, 2011 11:48 pm
Posts: 10
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("");
      }
   }

}


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 10:22 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
Using your code and the latest version of Slick (dev branch) I can't reproduce your problem.

A few things to keep in mind:
  • Exit the JVM after closing the window, otherwise the program will continue to run. You can do this with JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).
  • You are adding the action listener many times to the text field; the result is that actionPerformed will be called many times when the user hits the "enter" key. You only need to add it once.
  • You don't need to check to see if the game has focus. If it doesn't have focus, then BasicGame's keyPressed/keyReleased/mousePressed/etc won't be triggered, and likewise isMousePressed in update will return false.


Top
 Profile  
 
PostPosted: Wed Apr 25, 2012 5:25 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1173
Well - one think which looks wrong with your code is that you violate the Swing threading rules. All Swing code should execute on the EDT and not the main thread.
But mixing OpenGL and AWT/Swing is most likely to end with problems - you should really move to a 100% OpenGL GUI like TWL.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group