Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Waiting for input with TextArea  (Read 1498 times)
0 Members and 1 Guest are viewing this topic.
Offline Pyrodragoon

Junior Member




Art calculated with java...


« Posted 2010-02-05 11:18:08 »

Hi.

I want to do the following, but don't know how:
I have a TextArea and I want to use it as a console.
1  
2  
3  
4  
5  
TextArea console;
void doSomething(){
  String input = getInputFromConsole();
  ...
}

What I want is, that the method doSomething() waits
for me to press ENTER in the TextArea
and then return the text which is in the TextArea
into the String input.
How can I do that ?
Offline pjt33
« Reply #1 - Posted 2010-02-05 13:18:47 »

AWT is event-driven. Your approach can be implemented by registering a listener with the text area which will push its contents to a blocking queue and then having the doSomething() method poll the queue, but you might want to consider whether restructuring your design to be more event-driven would be a better approach.

Have you gone through any of the official Java Tutorial series on AWT and Swing?
Offline zoto

Senior Member


Medals: 4



« Reply #2 - Posted 2010-02-05 13:57:43 »

The JTextArea class can implement this easier. The only way I have found to do it with awt.TextArea probably isn't that good.
1  
2  
3  
4  
   public String getInputFromConsole() {
      String[] lines = console.getText().split("\n");
      return lines[lines.length-1];
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2010-02-05 16:52:20 »

Just use a caret listener sort of like this:

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  
private int lastLength;

public void caretUpdate(CaretEvent e)
{
    String currentText = textArea.getText();

    //Make sure that the user typed something (the size of the text are changed)
   //before we do anything. Don't bother when they only select around.
   if (currentText.length() != lastLength)
    {
        lastLength = currentText.length();

        //See if the last character is a newline.
       if (currentText.charAt(currentText.length()-1) == '\n')
        {
            //Check commands, blah blah.
           //Add our response to the box.
           String responseString = "COMMAND EXECUTED.\n";

            //Match the new size that we will have when the response is added.
           lastLength += responseString.length();

            //The VERY IMPORTANT thing to note here is that this will immediately
           //call caretUpdate again! That is why changing the length first is important.
           lastLength.setText(lastLength.getText() + responseString);
        }
    }
}


I just typed that now, but I have dealt with caretListener before, and it's certainly a mixed bag. On the one hand it works quite well, on the other it can't detect the difference between the user typing directly and you adding in your own stuff. What I put above should probably be an acceptable solution, but other ways of doing it are using textArea.setEditable(false) while you are adding text in, then set setEditable(true) once you've finished. The problem with this is that it grays out the text and removes and selections (which may be just fine for your purposes). You can keep the text from graying if you call textArea.setDisabledTextColor(myNormalTextColor).

I've done a lot of dynamic text field / text area editing in various apps, and CaretListener seemed to be the best bet. One example I used this for was to fill in keywords as the user typed, and select the extra letters so they could delete it as they went. You know, the usual way of suggesting keywords. It was a surprising pain in the butt to get it working nicely. Another example is having the value in the text box saved every time its value changes. This causes more problems because you either do it with every caret change (which can cause a lot of problems when the user has put in improper input and you want to warn them), or when any character changes - which means storing not just a length but also a string that must be compared with every letter type.

Either way, it'll take some work, but you can make it work for you.

Oh yeah, and to get this working you need to do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class MyClass implements CaretListener
{
    public MyClass()
    {
        textArea = new JTextArea(/*blah blah*/);
        textArea.addCaretListener(this);
    }

    //This must be implemented in order to allow this to be a caret listener.
   public void caretUpdate(CaretEvent e)
    {
        //code from above
   }
}

See my work:
OTC Software
Offline Pyrodragoon

Junior Member




Art calculated with java...


« Reply #4 - Posted 2010-02-07 14:10:52 »

This is a really nice way to get the user input from a TextArea
and as I only used the simple ActionListener, KeyListener etc. until now,
it's good to know.
I also made a tool with keyword suggestion, but I didn't need it there
because I used a thread there which checked the input every 1/25 sec.
I used the StringBuffer.reverse() method to compare the actual input to
a list of keywords.
But here the thread-approach is not a secure solution as you can see.

What I really need though is something equivalent to the Dialog function
1  
setModal(true);

I need something that behaves like the java.util.Scanner for the default System.in-console
with my TextArea.
Probably the easiest way to describe this is:
I want to emulate a console in my game.

For example i have a method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
TextArea console;
void enterUserData(){
  console.append("Please enter your name\n");
  String name = getInputFromConsole();
  console.append("Which type of warrior do you want to be?\n");
  console.append("(1) Assassin\n");
  console.append("(2) Archer\n");
  console.append("(3) Berserker\n");
  String type = getInputFromConsole();
  ....
}


I should have written this before, didn't I ?  Undecided
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.

CogWheelz (14 views)
2014-08-01 22:53:16

CogWheelz (14 views)
2014-08-01 22:51:43

CopyableCougar4 (15 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!