Results 1 to 9 of 9

Thread: Action Listener

  1. #1
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Question Action Listener

    Sorry I am new to this forum so please tell me how to use code tags.

    Following are my code

    static JMenuItem c = new JMenuItem("Hye");

    public static void one
    {
    c.addActionListener(null);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == m11)
    JOptionPane.showMessageDialog(null,"Hye");
    }



    My problem is that it is not taking any action even i press any key. Some on told me to add "this" instead of "null", but i cant do it when i have static specifier. Hope you understand and help me.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    you can do this:

    Java Code:
    c.addActionListener (new ActionListener  () {
      public void actionPerformed(ActionEvent e)
      {
        if(e.getSource() == m11)
          JOptionPane.showMessageDialog(null,"Hye");
      }
    });

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Sorry I am new to this forum so please tell me how to use code tags.
    Same as any other forum or word processor.

    You paste your code and then select it. Then you click on a button that looks like it will do what you want.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by greatmajestics View Post
    Sorry I am new to this forum so please tell me how to use code tags.
    see my signature below.

    Following are my code

    static JMenuItem c = new JMenuItem("Hye");
    You've got a problem right there. If your JMenuItem needs to be static, then something is possibly wrong with your code design. Why is this static by the way?

  5. #5
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    Because it is shared by other outer classes. That's Why i kept it static. By the way i have solved my problem , Thanx for the code tags help.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by greatmajestics View Post
    Because it is shared by other outer classes. That's Why i kept it static. By the way i have solved my problem , Thanx for the code tags help.
    Actually that may be poor reason for making it static. Your code is at risk for not being very object oriented in its structure and may not be as extensible as it would otherwise be.
    Last edited by Fubarable; 03-24-2010 at 10:07 PM.

  7. #7
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    Then how can i share one data type among different running threads simultaneously , Correct me Please if I am wrong. Also Extend your post with reason's.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    By creating and sharing objects, passing objects as parameters, using design patterns in appropriate situations. That's what OOPS is all about.

    How can you do it specifically in your situation? Hard to say without seeing your program structure/UML diagram.

  9. #9
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    Indeed you are right but can you please explain why static is used then? My teacher told me that it is used for sharing information among many running sessions.

Similar Threads

  1. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 09:17 PM
  2. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM
  3. Listener for action "team->commit"
    By feva in forum Eclipse
    Replies: 0
    Last Post: 07-27-2009, 08:40 PM
  4. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 11:14 AM
  5. [SOLVED] action listener and Jtextfield
    By tOpach in forum AWT / Swing
    Replies: 4
    Last Post: 12-16-2008, 02:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •