Skip to main content
gamedev.net
🔒 Locked

[java] swing help

Started by Javacell 1 reply 900+ views
Original Post
Javacell
Javacell
I have a JTextArea and a JTextField. When I enter text in the textfield and click a button it shows in the textarea. How can I read that text that I entered, have that text used in the next line, and have it display. Example: 1. I created an application that asks for your name and prints it out for you. 2. You run the application. 3. It asks for your name. 4. You enter your name. 5. It prints out your name on the next line. This is how it appears: Enter your name: DKN Your name is DKN Here is the code:

System.out.print("Enter your name: ");//ask for name
		name = in.readLine(); //get name
		System.out.println( "\n" + "Your name is  " + name  ); //tell what your name is



Now what I did with the textfield and area. 1. The text area says "Enter your name" 1. You enter your name in the textfield. 2. You click a button. 3. The button copies what you put in the textfield onto the textarea. 4. Your name appears next to "Enter your name: ". This is how it appears: Enter your name: DKN Question is how can I read that name and have it print out on the next line on the textarea like what I did with my application? What should I read about swing that can teach me this? My book doesn't cover beyond the basics of design of swing and not how to use your application in a swing.
CaptainJester
CaptainJester
You need to use an ActionListener on the button to catch when a user clicks it. You can then use getText()/setText() methods to read from and write to text components. Since there is no appendText, you will first need to read the text from the JTextArea then write it back with the JTextfield appended. Look at the API documentation for these classes to see how these methods work.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
garazdawi
garazdawi
Before starting to play with swing I highly recommend reading this tutorial trail. It helps you thorugh with all your swinging and such and its a great resource whenever you want to learn something about most components in java.
garazdawi - 'I put the laughter back in slaughter'

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.