• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActonListener

 
Suela Smith
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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

COde:

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

public class AddPersonsData extends JFrame {
public AddPersonsData(){
super("Add Persons Data");
setSize(790,270);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
JPanel panel1 = new JPanel(new FlowLayout());

JButton button1 = new JButton("Load");
panel1.add(button1);
JButton button2 = new JButton("Save");
panel1.add(button2);
JButton button3 = new JButton("Clear");
panel1.add(button3);
JButton button4 = new JButton("Exit");
panel1.add(button4);
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();
apd.setVisible(true);
}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://jdk.representqueens.com:9090/s/jdk/

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

http://java.sun.com/j2se/1.5.0/download.jsp#docs
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.

CR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic