Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  User Inputted Text  (Read 605 times)
0 Members and 1 Guest are viewing this topic.
Offline Luminem

Senior Newbie





« Posted 2012-12-20 17:42:05 »

hey guys :S now my question is... how can i read the user inputted text? But not at the console...

Simply Perfect
Online Jimmt
« Reply #1 - Posted 2012-12-20 18:56:34 »

JTextField/JTextArea/JTextPane add any of these swing components and then use the .getText() method.
1  
2  
3  
frame.add(panel);
panel.add(textField);
String input = textField.getText();

...obviously you have to instantiate the frame, panel and textField first, and you should have a solid understanding of swing. If not: http://www.javabeginner.com/java-swing/java-swing-tutorial
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #2 - Posted 2012-12-20 20:26:53 »

I agree with Jimmit, first create a JFrame, then a panel, add the TextField to it, and then: String input = textBox.getText(). The getText() method returns the user input into the textBoxSmiley
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Online Jimmt
« Reply #3 - Posted 2012-12-20 21:55:28 »

I agree with Jimmit, first create a JFrame, then a panel, add the TextField to it, and then: String input = textBox.getText(). The getText() method returns the user input into the textBoxSmiley
That's...what...I just said. No need to repeat Smiley
Offline Luminem

Senior Newbie





« Reply #4 - Posted 2012-12-20 22:14:45 »

No!, No!  Wink i wasn't referring to that. I wanted to say a "textfield" but inside the game. For example Minecraft when you make a new world you must write a name for it... There's a textfield when you can put the name. Or another example is Titan Attacks. You must write a user name in a textfield inside the game. So i was referring to that Cheesy

Simply Perfect
Offline sproingie
« Reply #5 - Posted 2012-12-20 22:17:18 »

Both of those games use custom gui toolkits of their own making, but each of them likely follows the same design where there's a method to get the string out of the text widget, the same as in Swing.  If you're using LWJGL, you're probably better off using something like TWL instead of rolling your own GUI.
Offline Luminem

Senior Newbie





« Reply #6 - Posted 2012-12-21 13:54:29 »

I think i will be able to make my own gui, only i need know what method i must use? There's a "scanner" as console input? The rest is only test

Simply Perfect
Offline Damocles
« Reply #7 - Posted 2012-12-21 14:08:14 »

Here a simple code snipplet, for entering a one-liner with editing
A "raw" solution without using any API functionality.

This grabs the raw Event data directly and translates all standard Latin characters to chars, added
to your textline.
Plus using the Back-Delete and Enter key.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
String chatstring="";

....

   //draw the String somewhere
  g.drawString(chatstring, 20, 20);

   //or a bit more funky with a cursor
  g.drawString(chatstring+(System.currentTimeMillis()%500>250?"|":""), 20, 50);
           

....
   public boolean handleEvent(Event paramEvent)
   {

      //determine keypress
     if(paramEvent.id==401)      
         {
         //pressed an  ASCII character
        if(paramEvent.key>=32 && paramEvent.key<=122) chatstring+=(char)paramEvent.key;
         //delete  key
        if(paramEvent.key==8 && chatstring.length() > 0) chatstring = chatstring.substring(0, chatstring.length() - 1);
         //enter and do something with the input
        if(paramEvent.key==10 && chatstring.length() > 0) {sendChat(chatstring); chatstring="";}
         }

     
      return true;
   }

Offline Luminem

Senior Newbie





« Reply #8 - Posted 2012-12-26 18:35:51 »

OMC, could you help me to use that ? :s I don't know how to use it... because it doesn't work on my game
here. my code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
public void drawDialog(Graphics g){
      g.setColor(Color.BLACK);
      g.setFont(font);
      int i;
      for(i = 0; i < dialogs[i].length; i++){
         if(dialogs[u][i] == null) text = "";
         else text = dialogs[u][i];
         if(text.equals("<name>")){
               g.drawString(name+(System.currentTimeMillis()%500>250?"|":""), 15, 25+i*15);
               handleEvent(Event paramEvent);
         }else{
            g.drawString(text,15,25+i*15);
         }
      }
      g.drawString("Press enter to continue...",600,25+i*15);
      enter = true;
   }
public boolean handleEvent(Event paramEvent)
      {
         //determine keypress
        if(paramEvent.id==401)      
            {
            //pressed an  ASCII character
           if(paramEvent.key>=32 && paramEvent.key<=122) name+=(char)paramEvent.key;
            //delete  key
           if(paramEvent.key==8 && name.length() > 0) name = name.substring(0, name.length() - 1);
            }

         
         return true;
      }

Simply Perfect
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (33 views)
2013-05-17 21:29:12

alaslipknot (42 views)
2013-05-16 21:24:48

gouessej (72 views)
2013-05-16 00:53:38

gouessej (71 views)
2013-05-16 00:17:58

theagentd (79 views)
2013-05-15 15:01:13

theagentd (74 views)
2013-05-15 15:00:54

StreetDoggy (114 views)
2013-05-14 15:56:26

kutucuk (138 views)
2013-05-12 17:10:36

kutucuk (138 views)
2013-05-12 15:36:09

UnluckyDevil (146 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.245 seconds with 21 queries.