Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Swing Objects Unreachable

 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic