User Inputted Text

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

JTextField/JTextArea/JTextPane add any of these swing components and then use the .getText() method.


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

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 textBox. :slight_smile:

That’s…what…I just said. No need to repeat :slight_smile:

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 :smiley:

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.

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

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.


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;
	}

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

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;
	   }