Results 1 to 3 of 3
- 11-01-2009, 04:43 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
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(); }
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(); } }
- 11-01-2009, 06:02 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
menu.show(event.getComponent(),event.getX(), event.getY());//BIT THAT DOESN'T WORK
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.
- 11-02-2009, 06:25 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
addActionListener(java.awt.event.ActionListener) in java.awt.Button cannot be applied
By mathias in forum Java AppletsReplies: 3Last Post: 03-26-2009, 10:31 PM -
ActionListener run automatically problem
By cassysumandak in forum New To JavaReplies: 1Last Post: 03-23-2009, 09:42 PM -
Implementing ActionListener for "nameless " button
By hitmen in forum AWT / SwingReplies: 8Last Post: 03-09-2009, 11:32 AM -
ActionListener Applet problem
By xander5511 in forum Java AppletsReplies: 1Last Post: 02-21-2009, 03:42 AM -
problem on button
By udayforu_1986 in forum Java AppletsReplies: 1Last Post: 08-03-2008, 01:57 PM
Bookmarks