Hey guys, I'm having a bit of a problem. I have an application that is using a main frame1 and a second frame2 that is called from frame1. I need to set a textBox in Frame1 based on input from Frame2 but i can't figure out how to do it. any help would be appreciated.
4/7/2011 1:30:47 PM
Is VariableA declared to be public, or does it have a public "getter" method (the better design pattern)?If it is, you should be able to access it, ideally by calling that "getter" method:
public class Frame1 extends javax.swing.JFrame implements Observer{ //stuff Frame2 myFrame2 = new Frame2(); //more stuff private void methodNeedingVariableA(){ //stuff a = myFrame2.getA(); //more stuff } //still more stuff}//...public class Frame2 extends javax.swing.JFrame{ //stuff private string variableA; //more stuff public string getA(){return variableA;} //still more stuff}
4/7/2011 1:44:43 PM
The problem I'm having is that I want to get the variable from Frame2 only if the config button is hit (which also disposes of Frame2). In Frame1 I have this code:
private void findVIPBtnActionPerformed(java.awt.event.ActionEvent evt) { Frame2 myFrame2=new Frame2();this.vipAddressField.setText(myFrame2.returnVariableA().toString());..
4/7/2011 2:12:39 PM
It's been a few years since I've used swing so there are probably better answers but here goes.
Frame2 myFrame2=new Frame2();this.vipAddressField.setText(myFrame2.returnVariableA().toString());
class Frame1{ private variable_set_by_frame2; JFrame myFrame2 = new Frame2(this); public set_function_used_by_frame2(someVar){ variable_set_by_frame2 = someVar; }}class Frame2{ public Frame2(JFrame parentFrame){ myParentFrame = parentFrame; } private void configBtnActionPerformed(java.awt.event.ActionEvent evt) { myParentFrame.set_function_used_by_frame2(VariableA); this.dispose(); }}
4/8/2011 12:04:58 AM