• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
  • Frits Walraven
  • fred rosenberger
  • salvin francis


Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone
I need help with ActionListener, I have a JFrame which has some JLabels, some TextField, four JButtons, and a JTextArea. I want to add on the JTextArea what i write in the JTextField, so when i click the button Add the text goes to the textarea.

Thanks in advance


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class AddPersonsData extends JFrame {
public AddPersonsData(){
super("Add Persons Data");
Container contentPane = getContentPane();
JPanel panel1 = new JPanel(new FlowLayout());

JButton button1 = new JButton("Load");
JButton button2 = new JButton("Save");
JButton button3 = new JButton("Clear");
JButton button4 = new JButton("Exit");
contentPane.add("South", panel1);

JPanel panel2 = new JPanel(new GridLayout(7, 2));

panel2.add(new JLabel("First Name", JLabel.RIGHT));
panel2.add(new JTextField(20));
panel2.add(new JLabel("Middle Name",JLabel.RIGHT));
panel2.add(new JTextField(20));
panel2.add(new JLabel("Last Name", JLabel.RIGHT));
panel2.add(new JTextField(20));
panel2.add(new JLabel("Gender", JLabel.RIGHT));
panel2.add(new JTextField(20));
panel2.add(new JLabel("Birth Year", JLabel.RIGHT));
panel2.add(new JTextField(20));
panel2.add(new JLabel("Birth Month", JLabel.RIGHT));
panel2.add(new JTextField(20));
panel2.add(new JLabel("Birth Day", JLabel.RIGHT));
panel2.add(new JTextField(20));
contentPane.add("Center", panel2);

JPanel panel3 = new JPanel();
panel3.add(new JScrollPane(new JTextArea(11, 20)));
contentPane.add("West", panel3);

} //end constructor method
public static void main(String[] args) {
// perform any initialization
AddPersonsData apd=new AddPersonsData();
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

click on the link, the author has linked it to java's api docs
type in the first couple of letters of JTextArea
select JTextArea
you will now be looking at the api docs for JTextArea
scroll down to the Methods section - the method you want will be in this section,
if you want to add onto existing text.

if you want to replace what is currently in the textArea, scroll down a bit further
to the methods inherited from...
the method you want is one of the set...() methods - it will stand out

the api docs are handy to have on your pc, you can download them from here

Posts: 73255
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the question. Do you mean you want to know how to write an ActionListener?

You are adding new JTextFields to your panel2; when you use the anonymous object syntax like that, you can never get access to those objects again. You will heve to give those JTextFields names, either as private fields or final local variables, before you can get your hands on them.

I have my own ideas about ActionListeners, and have written recently here. In my opinion the crucial question is:

Are you going to have several ActionListeners doing the same thing or similar things?

If yes, then write a class which implements the java.awt.event.ActionListener interface, and look in the API for its method signature. You must use exactly the same signature. Put whatever you want to do in its method body. Example:-

The alternative is to use the anonymous inner class, which you do in stages, unless you can persuade an IDE to put it together for you.

1: put in the addActionListener() method

It won't work until you finish the whole procedure.

2: break the line with two enter keystrokes in the ().

3: Enter the ActionListener; remember it is anonymous.

4: Add {} to make the class body for your Listener

5: Enter the method, whose signature you will have found in the API.

6: Check you have imported the two classes. At this point your code will compile, but nothing will happen because the method body is empty.

7: Put something in the method, something more useful than this:

and that's how you do it.

them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic