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.addActionListene r(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(ActionEven t 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.
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.addActionListene
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(ActionEven
{
Result = resultBox.getText();
Frame.dispose();
Frame.hide();
}
}
When i go ahead and type in another class something like:
System.out.println(parent,
the result is always null.
ASKER
isn't that a swing approach?
is there an "awt" way to do it?
is there an "awt" way to do it?
Hi,
Let me check that out... :)
Regards
Dave
Let me check that out... :)
Regards
Dave
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(ActionEven t e)
>> {
>> Result = resultBox.getText();
You were probably thinking of doing: proccessingDialog.Result = resultBox.getText () ; ?
>> return Result;
That's probably the reason for the error. The value of Result is still null.
>> public void actionPerformed(ActionEven
>> {
>> Result = resultBox.getText();
You were probably thinking of doing: proccessingDialog.Result = resultBox.getText () ; ?
ASKER
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?
However, what would i change in the processingDialog class?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> System.out.println ( result ) ;
- seems to print whatever you enter in the text-field.
- seems to print whatever you enter in the text-field.
String response = JOptionPane.showInputDialo
Then you just process the "response" string. :-)