Sam Ritter wrote:In this case if I use JColorChooser.showDialog, how would I set the location? Eventually I will need to set other customizations before displaying the dialog. I need the instance of JColorChooser to do that, right? Obviously I am still missing something.
That is because you have declared the Color at too high a level. Call setBackground(...) on the individual buttons, and only from one action listener.Sam Ritter wrote:. . . . When I choose a color, it changes all the buttons not just the one being used. . . . .
I have multiple buttons that need to use a different instance of the dialog.
When I choose a color, it changes all the buttons not just the one being used
Sam Ritter wrote:Sorry about the confusion. It started as what I thought was a simple issue of static methods. It evolved into more details about what I was doing. Responses made it clear my entire approach was wrong (i.e. poor programming practices).
Assuming each of the Animal, Lion, and Elephant classes has a method called staticMethod(), which one is actually executed?Brian Cole wrote:. . . The reason people (and also the IDE) recommend you do it the second way and not the first way is not because it works any better (which it doesn't—it works exactly the same) but because it's less confusing.
Campbell Ritchie wrote:
Ricky Bee wrote:
Why would you want to code something like this?![]()
Junilu Lacar wrote:Campbell is trying to point out an example where a call using a object reference instead of a class reference actually does more than just make the code confusing
Brian Cole wrote:
Junilu Lacar wrote:Campbell is trying to point out an example where a call using a object reference instead of a class reference actually does more than just make the code confusing
... all the Lion and Elephant instances are doing is trying to cause more confusion. The confusion is intentional, of course, but it would have been clearer to just write Animal.staticMethod(...) and be done with it.
There are three kinds of actuaries: those who can count, and those who can't.
Ricky Bee wrote:but let me rephrase my previous question (and steer it a little from the inheritance of a static method problem which originated it in the first place):
Ricky Bee wrote:is such a structure a common-place in Java programing? I mean, is it normal to instantiate an object from a superclass and then pass it an object from a subclass?
Yes, of course it is easier to get it right first time and use the correct code, but as Junilu said, I am trying to point out a pitfall, where the semantics of the call on an instance is different from the correct way to call a static method.[ It doesn't work exactly the same now. Yes, of course I was intending to confuse things, but also to bring the confusion to people's notice.Brian Cole wrote:. . . . it would have been clearer to just write Animal.staticMethod(...) and be done with it. . . .
You can still write rubbish like...and it will both compile and run with the expected result.it's not if instanceVariable is null . . .
As Junilu Piet and Brian have told you already, yes.Ricky Bee wrote:. . . is such a structure a common-place in Java programing? I mean, is it normal to instantiate an object from a superclass and then pass it an object from a subclass? . . .
Campbell Ritchie wrote:You can still write rubbish like...and it will both compile and run with the expected result.
it would have been clearer to just write Animal.staticMethod(...) and be done with it
That could have shown return this; or return new Test3(); both of which would have constituted calling a static method on an instance reference.Brian Cole wrote:. . . In fact the JLS shows this nifty example: . . .
Apology acceptedI apologize for being less than clear.
Ricky Bee wrote:
Why would you want to code something like this?![]()
Campbell Ritchie wrote:That could have shown return this; or return new Test3();
By the way: which section in the JLS?
Thank youBrian Cole wrote:. . . . (BTW, I happen to be looking at the Java SE 7 edition of the JLS.)
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|