Results 1 to 14 of 14
Thread: Simple mouse event question
- 06-20-2010, 04:57 AM #1
Simple mouse event question
Hi i have a simple question about mouse event.
At the moment i have implemented the MouseListener and the 5 compulsary methods to go along with it.
The method that im interested in for this example is the mouseEntered method.
i have two objects calling to the addMouseListener, a label and a JPanel.
Basically what i want to do is run different code depending on which of the two obove objects the mouse enters.
So far i can write code which will run if either of the two objects gets entered with the mouse however i dont know run code if the label gets entered and run a different code if the JPanel gets entered.
If tried using an if statement within the mouseEntered method ie
if (event.getSource() = "nameOfLabel") //do something
else if (event.getSource() = "nameOfJPanel") //do something else
but it seems the getSource() didnt work.
Any ideas?
i dont fancy calling a new Anonymous Class for each of the two objects because that will mean writing out 5 mouse methods twice and i think there must be a far easier/simple way!
thanks for reading.Teaching myself java so that i can eventually join the industry! Started in June 2010
- 06-20-2010, 04:59 AM #2
Are those two lines copied and pasted from your source? If so, the problem may be in th fact that you used a single = sign.
Instead, try:
if (event.getSource() == "nameOfLabel")
or
if (event.getSource().equals("nameOfLabel"))
Apologies if I'm way off of the problem, but I figured this was worth mentioning.
- 06-20-2010, 05:02 AM #3
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
For what it's worth, I think that you're on the money here. To the original poster, if Zack's suggestion doesn't help you fix the problem, then you may wish to create a very simple Swing application that reproduces your problem, has no extraneous code unrelated to the problem and is compilable and runnable so that we can test and alter the offending code ourselves. Also, if the actions of the mouse listener is vastly different for the two components, you're probably better off creating separate listeners for each. Also, if you haven't yet discovered it, the convenience class, MouseAdapter, a wrapper class that implements MouseListener, MouseMotionListener, and MouseWheelListener, can let you create MouseListeners that only declare the methods that you need.
Suerte!Last edited by curmudgeon; 06-20-2010 at 05:05 AM.
- 06-20-2010, 05:16 AM #4
Thnaks for quick replies.
To Zack, no this is not from my source, however my source does contain a a double equals sign.
i did a System.out.println(e.getSource()); before making this thread and it came up with some super long thingy which basically wasnt the name of the object, so i figured getSource() wasnt the solution however i think getSource works with actionListener and itemListener but not with mouse events.
To curmudgeon, thanks for suggesting MouseAdapter, i will use this in future once i've mastered the basics.
edit
ill try that getsource.equals method, didnt see u write that before. brb
edit
no joyLast edited by alacn; 06-20-2010 at 05:18 AM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 06-20-2010, 05:19 AM #5
heres the code. This program is purely just to test mouse event
just to explain the code a bit, if u look at the mouseEntered bit. it sets the label to show "you entered the area", regardless of which object the mouse is entered, however if its the label that the mouse enters, then it should say "you entered the label, naughty", but im just using the wrong if statment i think.PHP Code:package fresh; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.ArrayList; public class GUI extends JFrame{ private JPanel mousePanel; private JLabel statusBar; public GUI(){ super("GUI"); //setLayout(new FlowLayout()); mousePanel = new JPanel(); mousePanel.setBackground(Color.white); add(mousePanel,BorderLayout.CENTER); statusBar = new JLabel("defualt"); add(statusBar,BorderLayout.SOUTH); Handlerclass handler = new Handlerclass(); mousePanel.addMouseListener(handler); statusBar.addMouseListener(handler); // mousePanel.addMouseMotionListener(handler); } private class Handlerclass implements MouseListener{ public void mouseClicked(MouseEvent e){ statusBar.setText(String.format("%d, %d", e.getX(), e.getY() )); } public void mousePressed(MouseEvent e){ statusBar.setText("you pressed down"); } public void mouseReleased(MouseEvent e){ statusBar.setText("you released"); } public void mouseEntered(MouseEvent e){ statusBar.setText("you entered the area"); mousePanel.setBackground(Color.red); if(e.getSource().equals("statusBar")) statusBar.setText("you entered the label, naughty!"); } public void mouseExited(MouseEvent e){ // statusBar.setText("you exited"); mousePanel.setBackground(Color.white); } } }
btw please exuse my lazyness for not doing and if else statment for the two objects.!Last edited by alacn; 06-20-2010 at 05:27 AM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 06-20-2010, 05:26 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
The source is not a String but a component. So don't use Strings within your equals methods -- rather use the component variables:
Java Code:// if (e.getSource().equals("statusBar")) // **** incorrect if (e.getSource().equals(statusBar)) // **** correct statusBar.setText("you entered the label, naughty!");Last edited by curmudgeon; 06-20-2010 at 05:29 AM.
- 06-20-2010, 05:29 AM #7
yeah but if i use the components themself then how would i then tell the differences between two labels?
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 06-20-2010, 05:31 AM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 06-20-2010, 05:35 AM #9
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 06-20-2010, 05:36 AM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 06-20-2010, 05:44 AM #11
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
For example:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUI extends JFrame { private JPanel mousePanel; private JLabel statusBar; public GUI() { super("GUI"); // setLayout(new FlowLayout()); mousePanel = new JPanel(); mousePanel.setPreferredSize(new Dimension(400, 300)); mousePanel.setBackground(Color.white); add(mousePanel, BorderLayout.CENTER); statusBar = new JLabel("defualt"); add(statusBar, BorderLayout.SOUTH); Handlerclass handler = new Handlerclass(); mousePanel.addMouseListener(handler); statusBar.addMouseListener(handler); mousePanel.addMouseMotionListener(handler); } private class Handlerclass extends MouseAdapter { public void mouseClicked(MouseEvent e) { if (e.getSource().equals(mousePanel)) { statusBar.setText(String.format("%d, %d", e.getX(), e.getY())); } } public void mouseEntered(MouseEvent e) { if (e.getSource().equals(mousePanel)) { statusBar.setText("you entered the area"); mousePanel.setBackground(Color.red); } if (e.getSource().equals(statusBar)) { statusBar.setText("you entered the label, naughty!"); } } public void mouseMoved(MouseEvent e) { if (e.getSource().equals(mousePanel)) { statusBar.setText(String.format("you entered the area, %d, %d", e.getX(), e.getY())); } } public void mouseExited(MouseEvent e) { statusBar.setText(""); mousePanel.setBackground(Color.white); } } private static void createAndShowUI() { JFrame frame = new GUI(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
- 06-20-2010, 06:10 AM #12
thanks for code, seems MouseAdapter get the getSource working and is defo the way forward and saves alot of messing around.
thanks for upload and help.
Ill play around with your code when i wake up tommorow, ive been up all night practicing and very tired now. :)
thanks againTeaching myself java so that i can eventually join the industry! Started in June 2010
- 06-20-2010, 06:11 AM #13
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You're welcome. :)
- 06-21-2010, 03:53 AM #14
oh man im so stupid, i just tried out your code and then released why getSource() didnt work with mouseListener. its because i had the name of the component statusBar in quotes rather than just writing it normally.
I Wrote it without quotes and it worked with both mouseListener and MouseAdapter (for anyone else reading this in a few years time lol).
Although i do like mouse adapter alot, cas it means i can use anaymouse classes very smoothly now. ie
PHP Code:statusBar.addMouseListener( new MouseAdapter(){ public void mouseEntered(MouseEvent e){ statusBar.setText("entered label"); } } );Teaching myself java so that i can eventually join the industry! Started in June 2010
Similar Threads
-
Mouse event filtering?
By Taiko in forum AWT / SwingReplies: 12Last Post: 08-10-2011, 02:00 PM -
some simple question?
By jperson in forum New To JavaReplies: 4Last Post: 05-03-2010, 05:32 PM -
[SOLVED] Mouse event in JTabbed Pane
By javanewbie in forum AWT / SwingReplies: 6Last Post: 06-10-2009, 08:50 AM -
Mouse Event + Image Thresholding
By ojmayolebron in forum AWT / SwingReplies: 0Last Post: 03-27-2009, 12:17 AM -
Exception Event question
By josephdcoleman in forum New To JavaReplies: 0Last Post: 03-12-2009, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks