• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GUI interaction

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic