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 Changing Text on JButton through its actionPerformed-method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Changing Text on JButton through its actionPerformed-method" Watch "Changing Text on JButton through its actionPerformed-method" New topic
Author

Changing Text on JButton through its actionPerformed-method

Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Hi there, long time no see or hear newbie questions, so here I come:

I'd like the text of my JButton to change when it is clicked. But I cannot refer to it from inside actionPerformed. More precisely:



But I (and I quote)

Cannot refer to a non-final variable button inside an inner class defined in a different method.

Is there a way around this? There's got to be an easy wy to change what a button says by clicking it, right?

Thanks a bunch, M


The problem people have in trying to create something foolproof is that they generally underestimate the inginuity of fools.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
The ActionEvent has a getSource() method, which returns the component the generated the event.
Harold Lime
Ranch Hand

Joined: Jul 20, 2009
Posts: 38
Or declare the MyButton as final, then it can be referred to with the ActionListener.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Harold Lime wrote:Or declare the MyButton as final, then it can be referred to with the ActionListener.


You will never learn how to write reusable code with suggestions like that!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Rob Camick wrote:
Harold Lime wrote:Or declare the MyButton as final, then it can be referred to with the ActionListener.


You will never learn how to write reusable code with suggestions like that!

But anonymous inner classes and reusable don't really go well together, do they?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Hm, ok, didn't mean to start a fight. Parley?

It's really a small little project, just a data-inputter for the real problem. So stupid solutions that work are fine.

I had tried that, but it didn't work. But if I cast MyButton, I can indeed setText. Like so:



But I cannot, unfortunately, access the variables from MyButton. This



does not work. Why is that? Do I have to @override in MyButton?

At a loss. Again.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
But anonymous inner classes and reusable don't really go well together, do they?


I guess you don't understand the concept of promoting better coding techniques. I try to give helpfull tips.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
So stupid solutions that work are fine.


Which lead to larger problems on larger projects. Learn the proper techniques now to minimize problems in the future.

a) I had tried that, but it didn't work. But if I cast MyButton, I can indeed setText. Like so:
b) But I cannot, unfortunately, access the variables from MyButton. This


You've proven the techniques works. There is no reason you shouldn't be able to access the variable as well assuming you have the rights to change the variable.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Rob Camick wrote:
But anonymous inner classes and reusable don't really go well together, do they?


I guess you don't understand the concept of promoting better coding techniques. I try to give helpfull tips.


Actually, at that point, you didn't. You just dissed his comment as useless and did not, in fact, give helpfull tips (well before, that was very helpful, but not in that comment). I am thankful for the advice from both of you, as you are obviously more experienced than I, but do keep it civil, please. Other coders, other solutions.

As to my problem, I did use the code tags, right? But what was actually missing was more casting - I have to cast .set as String and .dayOfYear as int, now it's working fine.

Thanks everyone.

M

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
You just dissed his comment as useless

I never said it wouldn't work.

and did not, in fact, give helpfull tips


My tip was "it doesn't promote reusable code".

Yes, there is always more than one way to solve a problem but too many people get in the habbit of doing shortcuts and they don't know the reason why.

My comment was about how to write code for an ActionListener (or any listener in general) and had nothing to do with inner classes.

Maybe in this simple case the inner class is not reusable but if you ever do decide to refactor and create a separate class the job is much more straight forward. Also if you get in the habbit of using this approach then when you do need a separate class you don't need to ask another question about how to do it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

If the question is specifically restricted to a button changing its own text, then Rob might be right and it's better in the long run to do the cast inside the ActionListener.

However that fails when the button wants to change the text of some other button. So, not all that reusable after all in this case.

Point being, you have to look at the requirements. Naturally we don't have all the requirements because we're just being told about a specific coding problem. And sometimes the requirements can change and make a mess of the solution you thought was reusable or extensible or whatever. That's just life in the programming biz.
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
I'm not even in any biz. Well, the medical biz, I guess. I just java for fun, and creating shiftplans for on-call doctors with java will save time in the years to come. But I don't have enough projects to really have need for reusable coding.

I do see the difference in the two approaches though and am very grateful for both. In the end you guys took the time to help out, little noob that I am, so thank y'all very much.

Just got done with the project. Will not post the code to keep anyone from suffocating with laughter

M
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Paul Clapham wrote:then Rob might be right

Which one?
But you are right, Rob C has a point. I'm just too lazy at times and don't plan ahead enough. I have found myself having to refactor because of earlier laziness just a few times too often.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing Text on JButton through its actionPerformed-method