Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Action Listeners for JTextFields

Thread view: 
George - 15 Oct 2007 14:21 GMT
Dear All,

I am trying to use JFrame and JTextField, so that whenever a button is
clicked a message box will come up with the contents of the textfield.
These are defined as follows:

class Test extends JFrame implements ActionListener {
Jpanel p1 = new JPanel();
JTextField jtf = new JTextField();
p1.add(jtf);
this.add(p1);
JButton jb = new JButton("Click Me!");
this.add(jb);
jb.addActionListener(this);
(of course these commands are within an initialisation method)

The action listener is implemented as follows:

public void actionPerformed(ActionEvent e){
System.out.println(jtf.getText());
}

However, this gives me the error message that jtf can not be resolved. Is
it because it is inside a panel and how can I reference the panel and then
the textfield inside?

I look forward to hearing from you soon.

Many thanks in advance.

George
Andrew Thompson - 15 Oct 2007 15:18 GMT
...
>...this gives me the error message that jtf can not be resolved.

Odd you should say that.  That is *not* the error I
am getting here, for *this* version of the code.

<sscce>
import java.awt.event.*;
import javax.swing.*;

class Test extends JFrame implements ActionListener {

 Test() {
   JPanel p1 = new JPanel();
   JTextField jtf = new JTextField();
   p1.add(jtf);
   this.add(p1);
   JButton jb = new JButton("Click Me!");
   this.add(jb);
   jb.addActionListener(this);
 }

 /** The action listener is implemented as follows */
 public void actionPerformed(ActionEvent e){
   System.out.println(jtf.getText());
 }
}
</sscce>

D:\Test.java:18: cannot find symbol
symbol  : variable jtf
location: class Test
       System.out.println(jtf.getText());

Why does it report "jtf can not be resolved", where
you are?

BTW.  Note that I posted an SSCCE*.  An SSCCE
helps to explain a problem to people, far better than
code snippets.  Please consider posting SSCCE's,
rather than code snippets, in future.

* <http://www.physci.org/codes/sscce.html>

>I look forward to hearing from you soon.

This is usenet.  It is not uncommon to wait 72 hours
before getting a reply - or no reply at all.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Andrew Thompson - 15 Oct 2007 16:02 GMT
Some other variants to consider..

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Test2 extends JFrame implements ActionListener {

 // scope class..
 JTextField jtf = new JTextField(20);

 Test2() {
   JPanel p1 = new JPanel();
   p1.add(jtf);
   // layout constraint
   this.add(p1, BorderLayout.NORTH);
   JButton jb = new JButton("Click Me!");
   // layout constraint
   this.add(jb, BorderLayout.CENTER);
   jb.addActionListener(this);
 }

 /** The action listener is implemented as follows */
 public void actionPerformed(ActionEvent e){
   System.out.println(jtf.getText());
 }

 /** Add a simple main to throw it on-screen */
 public static void main(String[] args) {
   Runnable r = new Runnable() {
     public void run() {
       Test2 test = new Test2();
       test.setDefaultCloseOperation(
         JFrame.EXIT_ON_CLOSE);
       test.pack();
       test.setVisible(true);
     }
   };
   SwingUtilities.invokeLater(r);
 }
}
</sscce>

..and..

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Test3 extends JFrame {

 Test3() {
   JPanel p1 = new JPanel();
   // scope local, must be final..
   final JTextField jtf = new JTextField(20);
   p1.add(jtf);
   // layout constraint
   this.add(p1, BorderLayout.NORTH);
   JButton jb = new JButton("Click Me!");
   // layout constraint
   this.add(jb, BorderLayout.CENTER);
   //jb.addActionListener(this);
   // implement actionlistener as inner class
   jb.addActionListener(
     new ActionListener(){
       /** The action listener is implemented as follows */
       public void actionPerformed(ActionEvent e){
         System.out.println(jtf.getText());
       }
     });
 }

 /** Add a simple main to throw it on-screen */
 public static void main(String[] args) {
   Runnable r = new Runnable() {
     public void run() {
       Test3 test = new Test3();
       test.setDefaultCloseOperation(
         JFrame.EXIT_ON_CLOSE);
       test.pack();
       test.setVisible(true);
     }
   };
   SwingUtilities.invokeLater(r);
 }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Roedy Green - 15 Oct 2007 16:07 GMT
>However, this gives me the error message that jtf can not be resolved. Is
>it because it is inside a panel and how can I reference the panel and then
>the textfield inside?

Normally you make jtf an instance variable then you don't have the
problem.  When they are local variables, they must be final to be seen
in the anonymous inner class. I kid you not.

See http://www.mindprod.com/jgloss/nestedclasses.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.