Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
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? –  Tomas Lazaro Jul 25 '12 at 20:42
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). –  Daniel Carlsson Jul 25 '12 at 21:24
How do I separate the textfield from the canvas? –  Smallbro Jul 25 '12 at 22:11
what is "this" in your code snippet? I assume its the Canvas class? –  Aron_dc Aug 1 '12 at 12:54
Its the constructor for the canvas class which extends canvas. –  Smallbro Aug 1 '12 at 15:52

3 Answers 3

up vote 0 down vote accepted

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.
share|improve this answer
This should be a comment –  Brendan Jul 29 '12 at 14:13
You're right but I just can't find an "add comment" button benath the original authors post. Where do i find it? –  Aron_dc Jul 29 '12 at 14:17
Hmm, that's odd. It should be around the same area where you found the comment button for this post. –  Brendan Jul 29 '12 at 15:00
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? –  Aron_dc Jul 29 '12 at 22:02
Yes, you need a certain amount of reputation before you can comment, good luck getting it! –  TMP Jul 30 '12 at 5:04

One possibility:

int columns = 25;
TextField localTextField = new TextField("my game text", columns);
share|improve this answer
Doesn't appear to make a difference. –  Smallbro Jul 31 '12 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.

share|improve this answer
Thats what I am trying to achieve though, I want my textfield to appear as though it is integrated into the canvas. –  Smallbro Jul 31 '12 at 0:44
I edited my answer as the comment area is too small –  Daniel Carlsson Jul 31 '12 at 9:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.