Forums Register Login

ActionListener for menuItem is not working with label

+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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?

1
+Pie Number of slices to send: Send
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.
    +Pie Number of slices to send: Send
     

    Rob Camick wrote: . . .
    3. Did you add the label to the frame?

    Yes, I did. But VV didn't.
    +Pie Number of slices to send: Send
     

    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
    +Pie Number of slices to send: Send
     

    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?
    1
    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
     

    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
    +Pie Number of slices to send: Send
     

    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.
    +Pie Number of slices to send: Send
     

    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() ).
    Crusading Chameleon likes the size of this ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1052 times.
    Similar Threads
    menu problem
    PopupMenu in Menu?
    how to control components of another window
    my java project.
    two applets
    Thread Boost feature
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 07:28:22.