• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?



 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because you have defined the same loop twice.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, see this recent post.
 
Aneeqa Rustam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how have I done that?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at lines 26-27 in your most recent post with code in.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic