Results 1 to 3 of 3
- 06-07-2012, 12:54 AM #1Member
- Join Date
- Apr 2012
- Rep Power
scanner.next() equivalent for JTextArea
I'm attempting to create a text-based RPG that displays in a swing window using a JTextArea for output and a JTextBox for input.
Without the GUI elements, I've made a similar project that functions well. The problem comes with trying to get input. In a console, I can force a pause to wait for input using scanner.next(), however I'm not sure how to cause a similar event using a JTextBox. Is it possible to use a scanner for this purpose? If not, what else could I do? Any ideas would be greatly appreciated.
- 06-07-2012, 02:29 AM #2Moderator
- Join Date
- Feb 2009
- New Zealand
- Rep Power
Re: scanner.next() equivalent for JTextArea
In a GUI you typically don't wait for user input: instead you respond to input when it happens. If you're unfamiliar with the approach it would be worth while stepping back from the program you want to write and having a look at a basic Swing tutorial. (Knowledge of how to write modest GUI applications should inform your design rather than letting it be driven by fixed ideas developed in another context).
That said your JTextField could form the basis for a scanner. One way would be to note that a Scanner can be constructed from (ie "wrap") any Readable or InputStream source. So one could try implementing Readable or extending InputStream in such a way that the data comes from the JTextField.
Such an approach is *not* trivial and will require some familiarity with both Swing and working with threads. So you are driven back to understanding Swing and, from that perspective, it makes more sense to write a "standard" GUI where text entered in the text box "triggers" subsequent actions (update of the state of the game, and output to the text area).
Last edited by pbrockway2; 06-07-2012 at 10:17 AM. Reason: see following...
- 06-07-2012, 05:55 AM #3
- By diamonddragon in forum New To JavaReplies: 5Last Post: 01-15-2012, 10:16 AM
- By shomid in forum Advanced JavaReplies: 0Last Post: 10-27-2011, 09:43 AM
- By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 08:51 PM
- By bullstreetboy in forum New To JavaReplies: 3Last Post: 04-06-2009, 06:31 PM
- By heb in forum Advanced JavaReplies: 1Last Post: 11-21-2007, 01:18 PM