Reputation:
I have many .java files within my project. From FTall.java i want to access {text field} t1 ('main' jFrame -> jPanel2) of the FormTTS.java
I am right now getting errors due to that only, because it cannot find symbol t1.
It is private and i cant change it to public
Edit: I am using this code already to open up FTall from the FormTTS.java: In a button in FormTTS
FTall forma = new FTall();
JFrame frame = forma.getFrame();
forma.setVisible(true);
and this in FTall
public JFrame getFrame() {
return jFrame1;
}
Upvotes: 2
Views: 2351
Reputation: 347334
Because of the way your code is structure, you need to supply some way for FormTTS.t1
In FormTTS
, provide a method to exposes t1
, something like getMainTextField
for example...
public JTextField getMainTextField() {
return t1;
}
You're next problem is FTall
is going to need a reference to an instance of FormTTS
. Probably the easiest way would be to pass a reference to the constructor of FTall
private FormTTS mainForm;
public FTall(FormTTS mainForm) {
this.mainForm= mainForm;
}
This will allow you to access t1
by simply using the mainForm
reference...
JTextField field = mainForm.getMainTextField();
Personally, I would prefer not to expose the text field as it gives too much access to callers, instead I'd prefer to return
the text and if required provide a means to change it...
So in FormTTS
, I might do something like...
public String getMainText() {
return t1.getText();
}
// Do this only if you need to have write access
public void setMainText(String text) {
t1.setText(text);
}
But that's just me...
To obtain the value, you would use a similar approach as above (to getting the text field)
String text = mainForm.getMainText();
Upvotes: 2
Reputation: 1299
if i am understanding your question its simple first ensure that your text field come in to scope before access and once it come in, then use a setter to set its refrence in required class then you can access it.
Upvotes: 0