NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
veggieCoder
Joined: 06 Jan 2009 Posts: 2
|
Posted: Thu Dec 16, 2010 6:38 pm Post subject: JFrame out of scope? |
|
|
Hi Everyone,
I'm semi-new to both Java and Netbeans and have a question about creating JFrame forms using the GUI, then accessing them in code.
If I right-click my package, then click New then JFrame form, Netbeans creates a form and I can see in the properties that it's called "frame1." However, no-where in the code can I access frame1. It appears to be out of scope everywhere. So, if I'd like to make changes to the interface at runtime or based on user input I can't do so. What am I missing?
Thanks in advance. |
|
| Back to top |
|
 |
markwade
Joined: 04 Dec 2010 Posts: 140
|
Posted: Fri Dec 17, 2010 1:34 am Post subject: JFrame out of scope? |
|
|
On Dec 16, 2010, at 1:38 PM, veggieCoder wrote:
| Quote: | Hi Everyone,
I'm semi-new to both Java and Netbeans and have a question about
creating JFrame forms using the GUI, then accessing them in code.
If I right-click my package, then click New then JFrame form,
Netbeans creates a form and I can see in the properties that it's
called "frame1." However, no-where in the code can I access
frame1. It appears to be out of scope everywhere. So, if I'd like
to make changes to the interface at runtime or based on user input I
can't do so. What am I missing?
Thanks in advance.
|
From within the JFrame, in NewJFrame.java or whatever you have named
it, you refer to the frame as "this". If you have another class in
the project that you want to access the frame from you will have to
create a JFrame variable and either make it public or protected or
create a public or protected accessor for it.
import javax.swing.JFrame;
public class NewJFrame extends JFrame{
private JFrame _frame;
public .JFrame get_frame(){
if(_frame == null){
_frame = this;
}
return _frame;
}
}
Actually, you don't need a variable just:
public JFrame getFrame(){
return this;
}
Mark Wade
address-removed |
|
| Back to top |
|
 |
areeda
Joined: 28 Aug 2008 Posts: 469 Location: Los Angeles
|
Posted: Fri Dec 17, 2010 2:40 am Post subject: Re: JFrame out of scope? |
|
|
| markwade wrote: | From within the JFrame, in NewJFrame.java or whatever you have named
it, you refer to the frame as "this". If you have another class in
the project that you want to access the frame from you will have to
create a JFrame variable and either make it public or protected or
create a public or protected accessor for it.
Mark Wade
address-removed | Everything Mark said is true, but I wonder if your question is more basic than that:
If you create a Java Desktop Application a lot of this is done for you.
If you are building the GUI yourself what you want might be as simple as :
NewJFrame njf = new NewJFrame(this,true);
njf.setVisible(true);
The Class must be instantiated into an object somehow. Usually during that part either in your code of the framework there will be some way to get access to the object.
Joe |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|