So I'm trying to have some form of input in my game and I understand all the keyPresses and whatnot but the only issue is I have no idea of adding a textfield to the game. JTextfield doesnt seem to appear so I started taking keypresses and adding them to a string but this proves to ether be too slow or too fast. Is there any way of adding a textfield to the game?

I'm using just plain java and swing.

Here is a code snippet:

JFrame frame = new JFrame("Game");
    //Sorts out JFrame variables and settings.
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            gameRunning = false;
    int columns = 25;
    TextField localTextField = new TextField("Text", columns);
    Insets insets = frame.getInsets();
    Dimension size = localTextField.getPreferredSize();
    localTextField.setBounds(25 + insets.left, 5 + insets.top,
                 size.width, size.height);
    //Double Buffer set up.
    strategy = getBufferStrategy();

Ok, so I pressume the only way of doing this is the way in which I'm currently doing it.While the text box is open it adds all keypesses together with a slight delay between each key press. I just thought that there might have been an easy method of doing this.

  • \$\begingroup\$ Could you be more specific? There is no way to know what you are trying to do. What engine or graphics library are you using? Are you using plain java and swing? Can you post some source code? \$\endgroup\$ Commented Jul 25, 2012 at 20:42
  • \$\begingroup\$ What do you use to draw your game, are you drawing directly to a canvas? In that case simply add a textfield outside the canvas, using layout to separate the textfield from the canvas. If you try to add the textfield directly to the same canvas your drawing the graphics on your going to get problems. Also, is it really text you want from the user (such as "go north") or do you want to know which buttons the user is holding down (such as WASD, space, etc). \$\endgroup\$ Commented Jul 25, 2012 at 21:24
  • \$\begingroup\$ How do I separate the textfield from the canvas? \$\endgroup\$
    – Malii
    Commented Jul 25, 2012 at 22:11
  • \$\begingroup\$ what is "this" in your code snippet? I assume its the Canvas class? \$\endgroup\$
    – Aron_dc
    Commented Aug 1, 2012 at 12:54
  • \$\begingroup\$ Its the constructor for the canvas class which extends canvas. \$\endgroup\$
    – Malii
    Commented Aug 1, 2012 at 15:52

3 Answers 3


Please give some code snippets. Normally you would use a JFrame as your window and add up your elements eventually using some kind of LayoutManager.

edit after codesippet appeared:

You have 3 main problems that I can figure out from this snippet.

  1. Why do you set the layout of the frame to null? Just take the standard.
  2. You are missing frame.pack() before the frame.setVisible(true) this would pack all the Elements (e.g. the Textfield) according to the used layout
  3. You centralize all your code to one class (this) it is your Canvas, and also key-,action-, and mouse listener. You should put them in different classes or at least make a Listener Class that handles all the logic.
  • \$\begingroup\$ This should be a comment \$\endgroup\$ Commented Jul 29, 2012 at 14:13
  • \$\begingroup\$ You're right but I just can't find an "add comment" button benath the original authors post. Where do i find it? \$\endgroup\$
    – Aron_dc
    Commented Jul 29, 2012 at 14:17
  • \$\begingroup\$ Hmm, that's odd. It should be around the same area where you found the comment button for this post. \$\endgroup\$ Commented Jul 29, 2012 at 15:00
  • \$\begingroup\$ It really isnt there. There is only a button if i find this post useful [yes] [no]. Does the ability to comment the original post come with a higher rep? \$\endgroup\$
    – Aron_dc
    Commented Jul 29, 2012 at 22:02
  • 1
    \$\begingroup\$ Yes, you need a certain amount of reputation before you can comment, good luck getting it! \$\endgroup\$
    – Tom Prats
    Commented Jul 30, 2012 at 5:04

One possibility:

int columns = 25;
TextField localTextField = new TextField("my game text", columns);
  • \$\begingroup\$ Doesn't appear to make a difference. \$\endgroup\$
    – Malii
    Commented Jul 31, 2012 at 0:44

Please look at this visual guide to layouts, pick a suitable layout and add the textfield and canvas so that they do not overlap.

If you do not wish to have an actual textbox, then it sounds more like your interested in listening to the keypresses, to detect when the player holds down aswd-keys for instance. Implement a key listener and there record which keys are held down. Alternatively, if your interested in making a hang-man type game, you can record each keypress in a List or a StringBuilder.

Hiding a textbox will cause problems, pressing left key would move the caret and change the functionality for instance.

  • \$\begingroup\$ Thats what I am trying to achieve though, I want my textfield to appear as though it is integrated into the canvas. \$\endgroup\$
    – Malii
    Commented Jul 31, 2012 at 0:44
  • \$\begingroup\$ I edited my answer as the comment area is too small \$\endgroup\$ Commented Jul 31, 2012 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .