Link to home
Start Free TrialLog in
Avatar of fuzzyfluid
fuzzyfluid

asked on

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.
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Use this:

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

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

ASKER

isn't that a swing approach?

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

Let me check that out... :)

Regards
Dave
Avatar of Mayank S
The proccessingDialog class need not extend Applet.
>> 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 () ; ?
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?
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> System.out.println ( result ) ;

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