• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Passing a Value between Two Separate Classes

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.
 
Thanks
 

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);
    p.add(resultBox);
 
     Button ok = new Button("ok");
     p.add(ok);
 
    ResultAction ra = new ResultAction(parent,Result,resultBox);
   
 resultBox.addActionListener(ra);
 ok.addActionListener(ra);
 
    d.add("South", p);
    d.setLocation(100, 200);
    d.pack();
    d.show();
 
    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();
      Frame.dispose();
      Frame.hide();
}
}
 
When i go ahead and type in another class something like:
 
System.out.println(parent, "Enter Name:");
 
the result is always null.
0
fuzzyfluid
Asked:
fuzzyfluid
1 Solution
 
InteractiveMindCommented:
Use this:

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

Then you just process the "response" string.  :-)
0
 
fuzzyfluidAuthor Commented:
isn't that a swing approach?

is there an "awt" way to do it?
0
 
suprapto45Commented:
Hi,

Let me check that out... :)

Regards
Dave
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Mayank SAssociate Director - Product EngineeringCommented:
The proccessingDialog class need not extend Applet.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> d.show();
>> 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 () ; ?
0
 
fuzzyfluidAuthor Commented:
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?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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 )
      {
            this.tf = tf ;
            this.d = d ;

      }

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

      }

}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> System.out.println ( result ) ;

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now