This site requires JavaScript, please enable it in your browser!
Greenfoot back
Velociraptor06
Velociraptor06 wrote ...

2013/5/5

Making a textbox

Velociraptor06 Velociraptor06

2013/5/5

#
Hi everyone, I want to make a text box, can someone show me how please? And is it possible to check what I've typed in the text box using a boolean statement? Thank you!
Gevater_Tod4711 Gevater_Tod4711

2013/5/5

#
You can use this textbox demo. Well but it's not possible to check what you have typed in with a boolean therefore you need a string. With a boolean you just could check whether there is anything in the textfield.
danpost danpost

2013/5/5

#
Do you want the textbox built right into your scenario? or can it be one that 'pops up' for user input? For a 'pop-up' textbox input frame, you can use an instance of the 'JOptionPane' class. Add the following line in your code to see how it works. First, you will need to add 'import javax.swing.JOptionPane;' to your list of imports at the top of the class code.
1
String inputValue = JOptionPane.showInputDialog("Please input a value");
After the inputDialog is finished, your 'inputValue' variable will contain 'null' or a String value depending on what the user does while the inputDialog is showing. You will probably need more than a Boolean statement to check the input (more like a method); but yes, it is possible to check the input String for what was typed (if anything).
Velociraptor06 Velociraptor06

2013/5/5

#
oh i want the text box built right into my scenario
danpost danpost

2013/5/5

#
What triggers the input into the box (does the user choose when to enter data or is it determined by some scenario event)?
danpost danpost

2013/5/5

#
If the user chooses when to enter data into the input box, then you should use an actor to create the box image. This way, the box can detect a user click on it to begin editing text in the box. If the user does not choose when to enter data into the input box, you can create the image of the box directly on the background of the world. Either way, when text is determined to be entered into the 'box' area, create a cursor object (which you will have to create a sub-class of Actor for) that will display the text and the cursor image and accept keystrokes to adjust its image. Text will need to be continuously updated in an instance field of the cursor as the keystrokes come in. Since you are writing the input code, you can limit the range of characters that can be accepted into the input String. Once the user is done with the input box, the input will either be accepted or cancelled (by pressing 'enter' or 'escape' if you allow). Whatever the final input is, it will need to be saved in a 'static' class field before removing the cursor object (use null or an empty String for the default value of this field). In the 'act' method of the world class continuously check for any non-default value in the 'static' field of the cursor class by calling a method that returns its value (copy the value to a local String field, set the 'static' field back to its default value, and return the value now in the local field). When calling the method from the world, immediately save it again in a local field. You can then use that local field to check for various input values and/or act upon its value as you see fit.
bourne bourne

2013/5/5

#
You can check out how I did my Textbox at: http://www.greenfoot.org/scenarios/7578 And feel free to use it.
Velociraptor06 Velociraptor06

2013/5/14

#
@bourne thank you very much. I'm having trouble with it though. I can't seem to add a text box into my world. and is there any way to check what the player has typed into the text box?
Velociraptor06 Velociraptor06

2013/5/14

#
@bourne never mind my previous post. thank you again. Just a couple questions: again is there any way (perhaps a boolean?) to check what the player has typed into the text box and how can i change the font (both font size and font) Thank you!
bourne bourne

2013/5/14

#
You can use getText() to get the contents of the TextBox, while setFont(Font) will allow you to change the Font of the text. For example: import java.awt.Font; ... myTextBox.setFont(new Font("Courier New", Font.PLAIN, 13));
Velociraptor06 Velociraptor06

2013/5/17

#
Thank you sorted that out already. Actually, how does the button work? I tried using Greenfoot.mousClicked() to instantiate methods in my button class, but then when i do click the button the game stops. what i'm trying to do is to click the button, which will then instantiate the getText() method. Then I have this textbox.getText.equals("") (whatever is in the text box) then the world will do its own stuff. That's not working though, can someone please help? Thanks!
bourne bourne

2013/5/17

#
Are you talking about the Buttons from my scenario? If you are, then you should use its boolean wasClicked() method, to determine if it was clicked on. (which once it returns true, it will then return false until it has been clicked again). I'm not sure by what you are trying to do with: textbox.getText().equals("") This will return true or false - Whether the text from the text box is equal to the empty quotes. I need more explanation of what the goal around the getText() is, and how it is not working.
JFish JFish

2016/2/12

#
Sorry for opening this thread again but i still don´t get how to return the written text and check it with a boolean statement. I want the textbox to appear when an actor touches a button.
1
2
3
4
5
6
Robo j = new Robo();
      j = (Robo) getOneObjectAtOffset(0,0,Robo.class);
    if (j != null)
        {
         showInputBox();
        }
According to this the textbox should show a math task which has to be solved by the player. so far it´s done.
1
2
3
4
5
6
7
public void showInputBox()
    {
     int a = Greenfoot.getRandomNumber(11);
     int b = Greenfoot.getRandomNumber(11);  
         
     String inputValue = JOptionPane.showInputDialog((int) a + "+" + (int) b);
    }
But now i wanna have a boolean statement if the result of the task and the input are equal. How can i check whether it´s equal or not? I just need a boolean statement that says: true when the written number is equal with the result and false when it´s not. If it´s equal the pop-up window and the button should disappear. Thank you in advance for your replies
danpost danpost

2016/2/14

#
@JFish, I think you mean something like:
1
if ((""+(a+b)).equals(inputValue))
You need to login to post a reply.