Jump to content
  • Advertisement
Sign in to follow this  
Javacell

[java] swing help

This topic is 5396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!