Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (564)
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 974 times)
0 Members and 1 Guest are viewing this topic.
Offline Luminem

Senior Newbie





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

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

Simply Perfect
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 132
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2012-12-20 17: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 19: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

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 132
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2012-12-20 20: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 21: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

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-12-20 21: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 12: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 13: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 17: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Grunnt (20 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (107 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!