• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing Text on JButton through its actionPerformed-method

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ActionEvent has a getSource() method, which returns the component the generated the event.
 
Harold Lime
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or declare the MyButton as final, then it can be referred to with the ActionListener.
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2599
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2599
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2599
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Dee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the solution for you!!! First this is in a randomizer, but this should apply to any condition.
**setText();!!!**

Here is the code:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch They might not still be waiting after 6 years
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Dee wrote:**setText();!!!**

See line 14 in the code posted in the OP (Original Post). Apart from that, setText(...) has been mentioned or used at least three times more in this thread.

Quite apart from that, I don't believe you executed the code you posted to see what the resulting GUI might look like. Also, your code shows several bad programming techniques:
  • Import-on-demand for use of a single class (lines 2 and 3)
  • Meaningless variable names (lines 18 and 21)
  • Using setBounds(...) instead of leveraging a layout manager (lines 19 and 22) ... which doesn't work as the JFrame's content pane still has the default BorderLayout
  • Irrelevant code (line 23)

  • Finally, we at the Ranch believe in guiding others towards a solution, not denying them the learning opportunity by posting a working solution without explanation.

    These comments are not meant to dissuade you from trying to be helpful here, but you may benefit from going through How To Answer Questions On Java Ranch
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic