Results 1 to 3 of 3
  1. #1
    qwertyuiop23 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default Problem w/ ActionListener on a button

    Hi there, please forgive me if this post is not set out so that it is easy to read. Also I think this is the correct forum, although I it is an applet the problem is not actually with the applet part rather I think the AWT part hence the reason for me posting here. If this is not the case can someone please move it to the correct forum?

    This is in an applet and the HTML I am using to display it is:
    <APPLET CODE="MenuApplication.class" WIDTH=300 HEIGHT=300></APPLET>
    I do not know if that is important or not but I will put it there just in case.

    The aim of what I am trying to do is to get a popup menu to show when i click on a button. So far I have got it so that the popup menu will come up when you right click, so I know the popup will shows. Also I have got the popup menu to work as in when one of the options are pressed I can get it to do something(at the moment it just changes a string). Also I have got the button implemented and I can get it to do something (it changes the background colour from white to black). My problem comes when I try to put show popup menu bit in where I have to background change bit. Here's what I mean:

    Java Code:
    public void actionPerformed(ActionEvent event) {
         if (event.getSource() == okButton)
         {
             ....
             menu.show(event.getComponent(),event.getX(),                 event.getY());
             //BIT THAT DOESN'T WORK
         }
        ....
        repaint();
      }
    Now I thought this should show a popup menu at that location but it does not. I think it has something to do with getComponent() only being able to be used in the mouse event. But if that is the case then how do I do a work around?

    Thank-you for your time and help and I hope this is clear enough for you to understand.
    Below I will put the entire source below just in case it is somewhere else that I am going wrong

    Thank-you
    ~Qwertyuiop


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.AWTEvent;
    import java.awt.PopupMenu;
    import java.awt.event.MouseEvent;
    import java.awt.Component;
    import java.lang.String;
    
    public class MenuApplication extends Applet
                                implements ActionListener
                                {
     private String[] menuEntries =
       { "Action 1", "Action 2", "Action 3"};
    
      private PopupMenu menu;
      Button okButton;
      String TESTERTEXT = "";
      boolean TF_COLOR = false;
    
    
      public void init() {
    
        setBackground(Color.BLACK);
    
        okButton = new Button("BUTTON");
        setLayout(null);
        okButton.setBounds(0,280,80,20);
        add(okButton);
    
        menu = new PopupMenu("Menu");
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
        MenuItem menuEntry;
        for(int i=0; i<menuEntries.length; i++)
        {
          menuEntry = new MenuItem(menuEntries[i]);
          menu.add(menuEntry);
          menuEntry.addActionListener(this);
          menu.addSeparator();
        }
        add(menu);
        okButton.addActionListener(this);
      }
    
      public void paint(Graphics graphics)
      {
        graphics.setColor(Color.RED);
        graphics.drawString(TESTERTEXT, 20,20);
        graphics.setColor(Color.BLACK);
      }
    
      public void processMouseEvent(MouseEvent event) {
    
        if (event.getButton() == MouseEvent.BUTTON3)
        {
            menu.show(event.getComponent(),
            event.getX(), event.getY());
        }
        super.processMouseEvent(event);
      }
    
    
      public void actionPerformed(ActionEvent event) {
         if (event.getSource() == okButton)
         {
             if (TF_COLOR == false)
             {
                 setBackground(Color.WHITE);
                 TF_COLOR = true;
             }else
             {
                 setBackground(Color.BLACK);
                 TF_COLOR = false;
             }
             menu.show(event.getComponent(),event.getX(), event.getY());//BIT THAT DOESN'T WORK
         }
        String actionEntry = event.getActionCommand();
        if (actionEntry=="Action 1")
        {
            TESTERTEXT = "You Clicked Action 1";
        }
        if (actionEntry=="Action 2")
        {
            TESTERTEXT = "You Clicked Action 2";
        }
        if (actionEntry=="Action 3")
        {
            TESTERTEXT = "You Clicked Action 3";
        }
        repaint();
      }
    
    }

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    menu.show(event.getComponent(),event.getX(), event.getY());//BIT THAT DOESN'T WORK
    My compiler will not compile this statement?!
    The trouble is that ActionEvents do not have the getX/getY methods.
    Where do you expect the popup menu to be shown?
    If you know, then you can set the coordinates to that point.

  3. #3
    qwertyuiop23 is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    Yeah I know it does work.... Hence the question.

    However, I have found a solution. The solution is:

    Java Code:
    menu.show(this,0,280);
    Cheers
    ~Qwertyuiop23

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2009, 10:31 PM
  2. ActionListener run automatically problem
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:42 PM
  3. Replies: 8
    Last Post: 03-09-2009, 11:32 AM
  4. ActionListener Applet problem
    By xander5511 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:42 AM
  5. problem on button
    By udayforu_1986 in forum Java Applets
    Replies: 1
    Last Post: 08-03-2008, 01:57 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
  •