This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Java Swing Objects Unreachable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java Swing Objects Unreachable" Watch "Java Swing Objects Unreachable" New topic
Author

Java Swing Objects Unreachable

Lokesh Poovaragan
Greenhorn

Joined: Aug 26, 2011
Posts: 21
I am a beginner in Java Swing: and I want to know how to code GUI. My program has a JMenuItem so I had to make an actionListener for it, hence I follow the object oriented way of writing my program. Basically what I am trying to say is that I did not write all the GUI start up code in my main method, I wrote a seperate class and called the code from there
let me show you a glimpse of how I have written my program:



I soon solved this problem by myself, not knowing what to do, I thought that if I moved the JFrame and all the gui Object declarations outside, it would benefit me, hence I moved it outside, like this:



with this I (think) I solved one problem, but I started another, as Font threw an exception, I cannot get it to compile, hence after further head banging, I came accross something called as a Constructor, I thought it was worth a shot, and I tried that method too: like so:





I wish that all methods can easily access the object declarations

Hence I am stuck at this, I cannot come up with any other way to twist and turn this, so I wanted to ask here,
I really hope I have typed all this out clearly...

for now, can you please tell me, which method was correct and where I went wrong? or if all three of my methods were wrong and I have to declare the objects somewhere else where it can be accessed by everything?

-Lokesh




Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
all 3 methods would work, except the JTextArea needs to be a class field (not a local variable),
as it needs to be referenced by show()

something like this
Lokesh Poovaragan
Greenhorn

Joined: Aug 26, 2011
Posts: 21
would it be ok, if I declared only the "Font f" outside as a class field, and the rest of it "f = createFont" indside the method? would that be good coding structure? also how is it done professionally? I may have 3 methods of doing it, but which one is most widely and/or the recommended way of doing it?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> would it be ok, if I declared only the "Font f" outside as a class field, and the rest of it "f = createFont" indside the method?

yes, that's a common way.

> would that be good coding structure? also how is it done professionally?

the professional way is what the boss wants :-)

> I may have 3 methods of doing it, but which one is most widely and/or the recommended way of doing it?

all the sun/oracle tutorials were coded with a createAndShowGUI() (they used to, haven't read them for a while).

possibly one of the main considerations is code re-usability e.g. if you have everything, including a JFrame,
tied up in a constructor, you can't add that class to, say, an applet, or anything else, it's just a stand-alone.

here's an example of the createAndShowGUI() where the panel will display in a JFrame

now create another .java file (keep all this in the same folder)

finally, copy this into Notepad and save as [anyNameYouWant].html

double-click the html, and your browser will open, now run MyPanel.java,
and a Jframe will display the same thing
Lokesh Poovaragan
Greenhorn

Joined: Aug 26, 2011
Posts: 21
Okay Michael Dunn, I think I get it completely! I went with Declaring all my Object Declarations outside and then inside the block, I entered "f = createFont()" so that did the trick, I also managed to get the other function to run properly this way! and so to speak, all my problems have been solved, hence a BIG THANKS to you for helping me out, and clearly making me understand step by step!

also to set up and run the GUI components inside a Swing Thread was a bit of an overkill right now, I can say I have not understood Java in that much depth to understand how to use Threads yet, but I will soon get there, till then a single main Thread should suffice for whatever simple thing I am trying to do.

so at last a very big thanks for helping me step by step!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> also to set up and run the GUI components inside a Swing Thread was a bit of an overkill right now

read this for examples of what can happen if not started on the EDT

http://bitguru.wordpress.com/2007/03/21/will-the-real-swing-single-threading-rule-please-stand-up/
 
Consider Paul's rocket mass heater.
 
subject: Java Swing Objects Unreachable
 
Similar Threads
Jar file Problem
importing a .ttf file
java Font???
Why is this code taking too long to launch
can't I take a single array for JTextField and JTextArea as both are derived from JTextComponent