Passing a Value between Two Separate Classes

Posted on 2005-04-17
Last Modified: 2010-03-31
Hi, i'd like to create a dialogBox that has a Textfield and a button. When the button is clicked, whatever is entered in the TextField gets passed into a string variable where i can use for processing.
However, the following code seems to always return null????
The first class below creates the dialog box, applies the actionlistener and should pass the value.
The second class is the actionlistener class.
1) I would like my returnTextDialog method to continue as is being a static method.
2) I do not want to use inner classes.

My first class is:
public class proccessingDialog extends Applet  {
    private static String Result;
    private static TextField resultBox;
  public static String returnTextDialog (Frame parent, String question)
    Dialog d = new Dialog(parent, true);
    d.add("Center", new Label(question));
    Panel p = new Panel();
    d.setLayout(new FlowLayout());
    resultBox = new TextField(30);
     Button ok = new Button("ok");
    ResultAction ra = new ResultAction(parent,Result,resultBox);
    d.add("South", p);
    d.setLocation(100, 200);
    return Result;

My second class is:
public class ResultAction implements ActionListener
    Window Frame;
    private String Result;
    private TextField resultBox;
public ResultAction (Window Frame,String Result,TextField resultBox)
    this.Frame = Frame;
    this.Result = Result;
    this.resultBox = resultBox;
public void actionPerformed(ActionEvent e)
      Result = resultBox.getText();
When i go ahead and type in another class something like:
System.out.println(parent, "Enter Name:");
the result is always null.
Question by:fuzzyfluid
    LVL 25

    Expert Comment

    Use this:

    String response = JOptionPane.showInputDialog( null, "Enter your name:" );

    Then you just process the "response" string.  :-)

    Author Comment

    isn't that a swing approach?

    is there an "awt" way to do it?
    LVL 16

    Expert Comment


    Let me check that out... :)

    LVL 30

    Expert Comment

    The proccessingDialog class need not extend Applet.
    LVL 30

    Expert Comment

    >> return Result;

    That's probably the reason for the error. The value of Result is still null.

    >> public void actionPerformed(ActionEvent e)
    >> {
    >>       Result = resultBox.getText();

    You were probably thinking of doing: proccessingDialog.Result = resultBox.getText () ; ?

    Author Comment

    hmm, if i used processingDialog.Result in the ResultAction class (implements ActionListener), could that possibly solve the problem?

    However, what would i change in the processingDialog class?
    LVL 30

    Accepted Solution

    Modified your code a little bit. This works:

    import java.awt.* ;
    import java.awt.event.* ;

    public class FrameTest
          public static void main ( String[] args )
                Frame f = new Frame () ;
                f.setSize ( 800, 600 ) ;
                f.setVisible ( true ) ;
                String result = ProcessingDialog.showDialog ( f, "Enter your message" ) ;
                System.out.println ( result ) ;



    class ProcessingDialog
          public static String result ;

          public static String showDialog ( Frame f, String message )
                Dialog d = new Dialog ( f, true ) ;
                TextField tf = new TextField ( 50 ) ;
                Button b = new Button ( "OK" ) ;
                d.setLayout ( new FlowLayout () ) ;
                d.add ( tf ) ;
                d.add ( b ) ;
                b.addActionListener ( new ResultAction ( tf, d ) ) ;
                d.setSize ( 400, 400 ) ;
                d.setVisible ( true ) ;
                return result ;



    class ResultAction implements ActionListener
          private TextField tf ;
          private Dialog d ;

          public ResultAction ( TextField tf, Dialog d )
       = tf ;
                this.d = d ;


          public void actionPerformed ( ActionEvent ae )
                ProcessingDialog.result = tf.getText () ;
                d.dispose () ;


    LVL 30

    Expert Comment

    >> System.out.println ( result ) ;

    - seems to print whatever you enter in the text-field.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now