File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes GUI interaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI interaction " Watch "GUI interaction " New topic
Author

GUI interaction

Ben David
Ranch Hand

Joined: Oct 01, 2010
Posts: 67
I am a total Java newb looking for some guidance.

Background
I am making an App for a course I am taking. It has a gui that allows some input of payment and minutes spent for a tutoring session. Then it will also display a report of sessions, etc.

Anyway -- I made a class (GUI) and built the GUI.

And I made a class (EarningsBucket) with a 2d array to hold the minutes and payment amounts. I made a method to record a session.

Then I have a Main class with my Main method, that I use to create an earnings bucket object, and then create and display the GUI.

Question
So I was trying to update the actionlistenter for the "Enter Session" button, that will take the values from the input fields for minutes and payment, and run the method to enter the session into the EarningsBucket object.

But the actionlistener (as I have it now anyway based on an example GUI from my book) is an internal class in the GUI JPanel Class where I build the GUI, and I cant seem to access the earningsbucket object from in there. (Even after making the variable that holds it in the Main Class public)

Anyway -- any input would be helpful. I may be approaching the problem all wrong -- in which case, please point me in the right direction.

PLEASE DO NOT RESPOND WITH SPECIFIC CODE. Though examples/skeleton code to convey a point or approach is fine.


In case anyone wants to see -- here is what I have so far

Main


Earnings Bucket





GUI
John Storta Jr.
Greenhorn

Joined: Jul 26, 2009
Posts: 29
I think the problem you are having has to do with trying to use a non-final local variable within your inner classes and where those inner classes are declared.

Here is a write-up on inner classes and local variables that I think will help. http://www.javaranch.com/campfire/StoryInner.jsp

There are a couple ways you can go to make this work, but the key is where you have your inner classes declared and the local 'data' variable that you are trying to use.

Let me know if this helps. I was able to get this to work by making a couple small changes to your code.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Welcome to the Ranch

I think this thread would sit better on our GUIs forum, so I shall move it there.
Ben David
Ranch Hand

Joined: Oct 01, 2010
Posts: 67

Thanks. I got this working! I just declared an instance variable, and set it with the variable passed in the constructor call.

At this point though I am curious -- particularly since you mentioned WHERE I was declaring the inner class. While what I have works, I am wondering if it is considered better form to do it differently. (or really, if there is anything I did, that would be considered a sloppy way to do it)




Earnings Bucket



GUI
John Storta Jr.
Greenhorn

Joined: Jul 26, 2009
Posts: 29
I am probably not the best source to determine if something is sloppy or bad form as I am still learning myself.

However, you have declared your inner classes within the constructor for the class. Therefore, their scope is local to the constructor. That was why you could not reference the local data object. As soon as the constructor finished your inner classes lost visibility to it.

As I worked through the code, I moved those declarations out of the constructor and into the class declaration itself so that their scope would be the lifespan of the object.

I would need someone else to weigh in on the pros and cons of each method.
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
agree with John,

what's the benefit of constructing the inner class inside of the constructor of that class anyway?
in terms of outside accessing, it wouldn't be reached.
Ben David
Ranch Hand

Joined: Oct 01, 2010
Posts: 67
J. Insi wrote: agree with John,

what's the benefit of constructing the inner class inside of the constructor of that class anyway?
in terms of outside accessing, it wouldn't be reached.


In Ch 18 of Big Java 3rd edition they create a sample GUI and provide code. This is where I saw the set up like this. It does not seem to explain why they did it that way.

Anyway -- I was able to make it work by creating an instance variable and assigning it from the local variable passed in the constructor. And then using the instance variables in the inner class. This worked even with the inner class inside the constructor.

However -- I ended up changing this because I found a method that I like better. I create the inner class anonymously in the call to the addActionListener method. Then in the actionPerformed method I just call a method I defined in the outer class, and that is where I put what should be done.

So here is how it looks to create the anonymous action listener object. (no need to have a variable to hold it)


And the method defined in the outer class


It just makes more sense to me this way.


Here is what the whole thing looks like now, with a nice and clean looking constructor.



But yea -- if anyone can tell us newbs, from an expereinced stand point, pro's and con's, etc -- that would be awesome


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Cross-posted: java-forums.org/gui-interaction

Ben, please read: BeForthrightWhenCrossPostingToOtherSites

It will explain why this is an important issue here and in all other programming fora.

Luck.
Ben David
Ranch Hand

Joined: Oct 01, 2010
Posts: 67
pete stein wrote:Cross-posted: java-forums.org/gui-interaction

Ben, please read: BeForthrightWhenCrossPostingToOtherSites

It will explain why this is an important issue here and in all other programming fora.

Luck.


My bad. I will make sure to clearly state it next time



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI interaction