• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do i change the JLable text by pressing a JButton?  RSS feed

 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel silly for asking but i just started java and I'm trying to change the JLabel text by clicking on the JButton however it isn't working, help please?


 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You need to add a listener to your buttons. Since your 4 buttons are similar, I have rewritten your code with the 4 buttons in an array, and then added a Listener using an anonymous class:-You should be able to use a λ instead in Java8. Change lines 9 to 16 above to read:-
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags to your post. Always use the tags: doesn't it look better
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You need to add a listener to your buttons. . . .


This how my code looks like now but it still isn't working?

 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You need to add a listener to your buttons. . . .


This how my code looks like now but it still isn't working?



 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell said, your code would be more readable if you did this: UseCodeTags. That's a link, please read it and when you post code, do that.

As for your question, you should read this other FAQ entry: ItDoesntWorkIsUseless. That's another link, please read it and when you have read it, try posting a better question.
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:As Campbell said, your code would be more readable if you did this: UseCodeTags. That's a link, please read it and when you post code, do that.

As for your question, you should read this other FAQ entry: ItDoesntWorkIsUseless. That's another link, please read it and when you have read it, try posting a better question.


Aneeqa Rustam wrote: I'm sorry, What I meant to say way, this is now my code, I know that an ActionListener should be put in there but when i did type it all in and at the top i put



it still was not compiling, I need to know why it isn't compiling and how I can fix this problem?

 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags to the previous post and restored it. Don't make your frames implement action listener. Once the code had been corrected for two missing imports, a missing setVisible call and a main method, I compiled and ran it and it did work. On my machine there seemed to be a delay of a few seconds after clicking the first button before anything happened.
The code I posted last night also worked.
Please read the link I gave you (and those Paul C gave); it says to use code tags not highlight as you had.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two missing imports: action listener and action event have to be imported.
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aneeqa Rustam wrote:. I have done what you told me to but I don't now how to add the setVisible and the main method and I'm stuck, when i compile the code it comes up with " local variable b is accessed from within inner class; needs to be declared final" what does that mean?


 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which version of Java® are you using? It compiles all right in Java8. Do what it says in the compiler error and mark b final; that was necessary in Java7 and before.
for (final JButton b : buttons) ...
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is java version 1.7.0_45, my university uses this one so I have to aswell. I did do that but this comes up "variable b is already defined in constructor BorderLayoutJFrame()

 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you have defined the same loop twice.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, see this recent post.
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how have I done that?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at lines 26-27 in your most recent post with code in.
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It maybe a bit easier to understand (or maybe not) if we only use one
actionlistener, in stead of 4. You would get something like:



Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe. But anything that encourages people to use action command can only lead them down the road of addActionListener(this). Action command only works in this particular instance because it actually sets the text you want.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!