• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Changing value of a outer class member via a inner class

 
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
Have hit a slight yet solid sturdy wall.

Trying to manipulate a outer class member via a inner class event handling method.

the gory details...

A class MyVolorChooser has a private member sliderColor. Default value is set to black in the constructor.

A inner class handles the construction of a new color via user input from JSLiders. I then set slideColor value to this new colour via a setMethod in the outer class.

Problem is a instance of the outerClass uses the outerClass method getColor which always returns the value from the constructor (black), and not the new value it has been assigned via inner event handier class?

Any ideas?

Cheers in advance
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciaran Mooney wrote:
A class MyVolorChooser has a private member sliderColor. Default value is set to black in the constructor.

A inner class handles the construction of a new color via user input from JSLiders. I then set slideColor value to this new colour via a setMethod in the outer class.

Problem is a instance of the outerClass uses the outerClass method getColor which always returns the value from the constructor (black), and not the new value it has been assigned via inner event handier class?

Any ideas?



An inner class instance should be able to access private members of it's outer class instance. What you are describing should work.

Something else is the issue -- and unfortunately, we don't have any details to speculate what is wrong.

Henry
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it seems like it should be working but got me stumped why its not......have posted soem of what i belive are the relevant bits f code:

NB Apologies in advance if code unsuitable as wasnt sure how much to insert....

Class MyColorChooser with which allows creation of color object via input fom JSliders handled by a inner class event hander:



Then Class ShapePanel which is a menu for which user can select various attributes of a shape to be drawn including colour. Color determined thrught slection via a JComobobox of an array of clolor objects. The colour from MyColorCHooser4 is acquired via an instance of teh class invoking the its getColor method:

declaration and intilisation of the above ...note DrawPanel



Event handler of class ShapePane which allows selection of colour....



The instance draw of class DrawPanel sets the colour the shape shoudl be drawn in.....



Hopefully this is somewhat useful......
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't use code tags correctly so I have corrected them for you. May I suggest you read UseCodeTags (← click) to see how to use them.

The posted code doesn't match the descriptions given in your first post, which together with code snippets of various classes without an explanation of how it all hangs together is a little confusing. I suggest you start to do some debugging by adding some print statements to the inner class to print out the colour the code is generating and also to the setSlideColor() method to see what color is actually being set (you may for instance find it is being set correctly and then another bit of code is setting it back to black).
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@tony, yes cheers for advice, i have debugged and noted that colour is the colour it should be in the inner class and also the setColour method...so im suspecting its been changed Back to the default constructor value...just not sure where or by what...ill just keep debugging
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try the following:
1. Search your code for everywhere you set the colour variable and add a print statement stating the method name and the colour you are setting it to.
2. In the inner class after setting the colour immediately call getColor() and print out the returned value to make sure you really have set the colour and are returning the correct variable etc.
 
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ciaran, try to log which thread accessing the setter:

It might be that some other thread (event-dispatching thread or smthn) is modifying your color.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other thing to check is that the instance of the colour chooser panel that you are displaying is the same instance as the one you are using to call the getColour() method on. ie you should only be creating one instance of MyColorChooser4 and you add this instance to the GUI panel and pass this same instance to whichever code needs to call getColor().
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
OK I think i have identified the problem....

So as suggested I controlled obj creation using a singleton class of MyColorChooser4 to ensure two objects are sharing the same colour....

Object 1 is created in the DrawInternalFrame and rendered onto the JInternalFrame.
Object 2 is in Class ShapePanel where shape type and color is selected.

Each object is created with MyColorChooser4.getInstance() and method getColor is called MyColorChooser4.getInstance.getColor()......

.....and I now clearly see that when the instance is created in ShapePanel of MyColorChosser4 it contains all the DEFAULT values of this class ie color=RED.

But when I look in the class itself I see the color has changed according to the JSlider input......so somehow a instance of the class has none of the values that the class has in its current state.....


PS @Surlac how does one log the threads accessing the setter?



 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So as suggested I controlled obj creation using a singleton class of MyColorChooser4 to ensure two objects are sharing the same colour....

Object 1 is created in the DrawInternalFrame and rendered onto the JInternalFrame.
Object 2 is in Class ShapePanel where shape type and color is selected.


That's not a singleton then. The singleton pattern ensures there can only be one instance of a class.

But when I look in the class itself I see the color has changed according to the JSlider input......so somehow a instance of the class has none of the values that the class has in its current state.....


You appear to be getting confused between classes and objects. Class variables (ie those declared as static) are shared by all instances of the class whereas instance variables are unique to each instance of the class. Therefore, if you create 2 instances of your class there will be two separate sets of instance variables, one for each instance. If you then modify the value of an instance variable in one instance the change will not be reflected in the other instance.

The solution to your problem is to only have one instance of the class. There are a few ways of achieving this, such as:
1. Use the singleton pattern to make sure you can only have one instance and have a static getInstance() method so your other objects can access it.
2. Create a single instance and pass the reference to that instance to the various objects that need to access it.
3. Implement a Event Listener in the class and have objects that need to be notified of the state changes listen for events.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony.
1. I thought i had created the one instance that coould be used in different classes:



..but ended up with same problem where they appeared to be actually separate instances as opposed to the same instance used in separate classes.

2. If I were to create a single instance of a class and pass the ref of the instance to objects that need it, where would one create the instance so as it could be used in a number of different classes?

3. Will attempt later today

Hope that makes sense.

Thanks in advance.
C
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciaran Mooney wrote:
PS @Surlac how does one log the threads accessing the setter?



See my answer above:

surlac surlacovich wrote:...




Tony, are you sure Swing/AWT doesn't create instances by itself? Dunno, but it looks like.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. I thought i had created the one instance that coould be used in different classes:


You spoke about creating different objects and didn't show any code so I didn't know exactly what you had done.

BTW That code is not a safe way to implement singleton - correctly implementing singleton is not as straightforward as you would think and there are number of articles on the pitfalls. To start with I suggest you do the following:

..but ended up with same problem where they appeared to be actually separate instances as opposed to the same instance used in separate classes.


Whilst that code wasn't ideal it was unlikely to create more than one instance. How do you know you had two different instances?

2. If I were to create a single instance of a class and pass the ref of the instance to objects that need it, where would one create the instance so as it could be used in a number of different classes?


It's difficult to say without knowing the application design but you would need to create it at a point where the reference could either be passed to both objects that need it or if one of the objects creates the other one then create this instance in the first object and pass it's reference to the second object.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

surlac surlacovich wrote:
Tony, are you sure Swing/AWT doesn't create instances by itself?


Yes I'm sure. Just think what the consequences would be if that was the case. How could you ever interact with the GUI if when you created an instance of a class and added it to the GUI, Swing suddenly created one or more instances of that same class to use instead of/as well as the one you passed in.

surlac surlacovich wrote:
Dunno, but it looks like.


Debugging is a difficult process and it is all to easy to jump to a conclusion and then try to make the facts fit. Debugging is an iterative process you need to gather lots of evidence, look at that evidence and think of what circumstances might cause those results. Then you gather more evidence to prove/disprove those ideas and so on.
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

surlac surlacovich wrote:
Dunno, but it looks like.


Debugging is a difficult process and it is all to easy to jump to a conclusion and then try to make the facts fit. Debugging is an iterative process you need to gather lots of evidence, look at that evidence and think of what circumstances might cause those results. Then you gather more evidence to prove/disprove those ideas and so on.


Thanks, I really would like to see the whole project and try to run it to gain some evidence.
Ciaran, can you post a link with the project archived.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure will do, in the middle of new tatic which seems to be working, ill email both versions next coupla days

What is the best way to upload, as java files or a jar file?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciaran Mooney wrote:What is the best way to upload, as java files or a jar file?


The best thing to do is to produce an SSCCE and post the code here.
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ciaran Mooney wrote:What is the best way to upload, as java files or a jar file?


upload it to some file-server (mediafire.com, http://dfiles.ru/, etc).

Tony Docherty wrote:
The best thing to do is to produce an SSCCE and post the code here.


That's true, but sometimes to see all the files involved in the project more helpful than spelling it's contents via separate posts in the forum.
Either topic-starter posts the whole project or narrow down the whole source code (truncating all unnecessary code) till the problem exists but code-base is minimal.
Also, I personally don't know how to source code posted in this thread and what dependencies I will need to add to make it even compile. So the project files will be very helpful.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

surlac surlacovich wrote:
Either topic-starter posts the whole project or narrow down the whole source code (truncating all unnecessary code) till the problem exists but code-base is minimal.


Narrowing down the source code until the problem exists but code-base is minimal is what an SSCCE is ie Short, Self Contained, Correct Example.

surlac surlacovich wrote:Also, I personally don't know how to source code posted in this thread and what dependencies I will need to add to make it even compile.


Posting code is easy. You post source code by cutting and pasting the code into the editor, highlighting it and clicking on the 'code' button.
There are no dependencies to worry about in an SSCCE as it is self contained.

surlac surlacovich wrote:So the project files will be very helpful.


It is rarely, if ever, true that seeing the whole project is better than seeing an SSCCE as when looking at the whole project you have to spend an amount of time working out which code is not relevant before you can even look for the problem.
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:
Posting code is easy. You post source code by cutting and pasting the code into the editor, highlighting it and clicking on the 'code' button.
There are no dependencies to worry about in an SSCCE as it is self contained.


Thanks, I meant it's not that clear how to build the Swing/AWT app from the source provided, not everyone on the forum has built any Swing/AWT app before, so if topic-started unable to simplify the code, he better to post the whole thing as archive.

Tony Docherty wrote:

surlac surlacovich wrote:So the project files will be very helpful.


It is rarely, if ever, true that seeing the whole project is better than seeing an SSCCE as when looking at the whole project you have to spend an amount of time working out which code is not relevant before you can even look for the problem.


That's true if topic-starter is willing to do some work and narrow down the code. I think it's still isn't the case for current thread, that's why we can't find out how many instances of the class created and what threads are making the modification.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

Tony thanks very much for your guidance, i resolved the issue by your suggestion: Implement a Event Listener in the class and have objects that need to be notified of the state changes listen for events.

Surlac also thank you for your input.

I have attached files as requested. The first link is the code that contains the issue dicussed here.
You will see the class ShapePanel and DrawInternalFrame had a separate instance each of MyColourChooser4. MyColourChooser4 is a class that allows user to select a colour via JSliders for red, green , blue values. Debugging i noted the colour changed accordingly in MyColorChooser4 but not in the DrawPanel class whicb draws the shape.

http://depositfiles.com/folders/I0OFMJ9QX

The second link is the version with a event lsitener in DrawInternalFrame class where JSlider objects listen for state changes relating to the Sliders in MyColourChooser renedered on the InternalFrame. In this case only the one instance of the MyColorChooser4 is required.

http://depositfiles.com/folders/NE1KDWFYZ

Hope makes sense and the file links work (first time i used dfiles.ru)

Cheers
C
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'll check the source code shortly.
 
It's just a flesh wound! Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic