Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener for menuItem is not working with label  RSS feed

 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've designed a code with some menuItem. In that i m planning to prompt label as "Hey Programmer" in window while clicking Open(menuItem)


But for me it show's nothing when clicking Open(menuItem) What is the issue with this code?
If i click exit(menuItem) it works fine Please anyone assist me. I would be very grateful for any help you are able to provide.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why are you spending time learning AWT? Your time would be better spend learning Swing: http://docs.oracle.com/javase/tutorial/uiswing/TOC.html

2. Variable names should NOT start with upper case characters. Some of your variables are correct, others are not. Be consistent and follow Java conventions!!!

3. Did you add the label to the frame?

 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works a lot better if you actually add the label to the GUI.

All sorts of other things wrong, I am afraid.
  • Don't use Frame or Label Use Swing components throughout, not AWT.
  • Don't make a display component implement action listener. You used an anonymous class for one listener, why not use the same for the others? In Java8 you can go one better and use a λ instead:-
    openItem.addActionListener(evt -> myLabel.setText("vino for ever!"));
    More elegant isn't it.
  • Give all fields private access. I think most of those components should not be fields in the first place, but local variables in the set GUI method. You will probably have to mark the label as final.
  • Dreadful names for things. NewClass is the worst. The only way you could make that worse is by starting it with n rather than N.
  • But you will see the text if you write
    add(lb);
    before the setVisible call.

    Once you start adding more components you will have to find a suitable layout, too.
     
    Campbell Ritchie
    Sheriff
    Posts: 53742
    127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Camick wrote: . . .
    3. Did you add the label to the frame?

    Yes, I did. But VV didn't.
     
    vinoth vino
    Ranch Hand
    Posts: 67
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:It works a lot better if you actually add the label to the GUI.

    All sorts of other things wrong, I am afraid.
  • Don't use Frame or Label Use Swing components throughout, not AWT.
  • Don't make a display component implement action listener. You used an anonymous class for one listener, why not use the same for the others? In Java8 you can go one better and use a λ instead:-
    openItem.addActionListener(evt -> myLabel.setText("vino for ever!"));
    More elegant isn't it.
  • Give all fields private access. I think most of those components should not be fields in the first place, but local variables in the set GUI method. You will probably have to mark the label as final.
  • Dreadful names for things. NewClass is the worst. The only way you could make that worse is by starting it with n rather than N.
  • But you will see the text if you write
    add(lb);
    before the setVisible call.

    Once you start adding more components you will have to find a suitable layout, too.


    Yeah, You are right after adding label into Frame it works fine Thanks a lot
     
    vinoth vino
    Ranch Hand
    Posts: 67
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Camick wrote:1. Why are you spending time learning AWT? Your time would be better spend learning Swing: http://docs.oracle.com/javase/tutorial/uiswing/TOC.html

    2. Variable names should NOT start with upper case characters. Some of your variables are correct, others are not. Be consistent and follow Java conventions!!!

    3. Did you add the label to the frame?



    Thankssss and Why ? AWT is not good? Now i m step into Event Handling bro. So in some sites they written event handling concept in AWT only. Thats why i did with AWT. Still now i didn't studied about Swing concepts.
    Can you please explain me ? what is the exact difference between AWT and Swing?
     
    Rob Camick
    Ranch Hand
    Posts: 2752
    11
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    AWT is not good?


    Swing replaced AWT over 10 years ago.

    Now i m step into Event Handling bro.


    Event handling is the same in Swing and AWT.

    what is the exact difference between AWT and Swing?


    Swing was designed to replace AWT 15 years ago and the API has been improved over the years. Look at the tutorial link I gave you. Maybe the section on "How to Use Labels" or "How to Write an ActionListener".

    So in some sites they written event handling concept in AWT only


    They are all old tutorials.
     
    vinoth vino
    Ranch Hand
    Posts: 67
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Camick wrote:
    AWT is not good?


    Swing replaced AWT over 10 years ago.

    Now i m step into Event Handling bro.


    Event handling is the same in Swing and AWT.

    what is the exact difference between AWT and Swing?


    Swing was designed to replace AWT 15 years ago and the API has been improved over the years. Look at the tutorial link I gave you. Maybe the section on "How to Use Labels" or "How to Write an ActionListener".

    So in some sites they written event handling concept in AWT only


    They are all old tutorials.


    Thanks for your valuable Suggestions Gonna learn SWING
    What is createAndShowGUI() method ? what is the purpose of using this? @Rob Camick
     
    Campbell Ritchie
    Sheriff
    Posts: 53742
    127
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    vinoth vino wrote: . . . Gonna learn SWING
    Don't.
    If you haven't learnt any GUI frameworks, yet, forget all about Swing. Learn JavaFX which is better and is intended to replace Swing within a few years.

    What is createAndShowGUI() method ? what is the purpose of using this? @Rob Camick
    Its meaning is what it sounds like. It creates and shows a GUI. You can set up all the GUI in the constructor instead if you wish.
     
    Brian Cole
    Author
    Ranch Hand
    Posts: 932
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    vinoth vino wrote:
    What is createAndShowGUI() method ? what is the purpose of using this?


    "createAndShowGUI" is just a method method name. You could have called it "fuzzyBunny" and your code would work the same.

    Or you could copy the method body into the constructor. But the reason that's not a good idea, and why many programs contain createAndShowGUI() methods, is that instead of calling it directly from the constructor it is somewhat more proper to call SwingUtilities.invokeLater( () -> createAndShowGUI() ).
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!