Mouse Motion Problem
I have a JFrame which I have added a MouseMotion Listener to it, it contains many JPanels with border layout manager. I want to detect a MouseMotion in the whole Frame. But I found that only the menu bar area could fire a MouseMotionEvent when the cursor moves there. There are no mouse motion event be fired when the mouse moves on other area on the Frame. What should I do to make it works? Or should I do anything to those insided JPanels ?
Add your MouseMotionListener to the frame's glass pane. Be sure to set the glass pane visible in order to retrieve the mouse moved events. See attached sample working code.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class GlassMotion
public static void main(String args[])
new GlassMotionFrame();
class GlassMotionFrame extends JFrame implements ActionListener
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu view = new JMenu("View");
JMenu help = new JMenu("Help");
JSeparator separator = new JSeparator();
JMenuItem fileOpen = new JMenuItem("Open...");
JMenuItem fileSaveAs = new JMenuItem("Save As...");
JMenuItem editCut = new JMenuItem("Cut");
JMenuItem editCopy = new JMenuItem("Copy");
JMenuItem editPaste = new JMenuItem("Paste");
JMenuItem helpAbout = new JMenuItem("About...");
JPanel north = new JPanel();
JPanel south = new JPanel();
JPanel center = new JPanel();
JTextField jtf = new JTextField(20);
JTextArea jta = new JTextArea(20,30);
JScrollPane jsp = new JScrollPane(jta);
JButton ok = new JButton("OK");
GlassMotionFrame()
super();
/* Components should be added to the container's content pane */
Container cp = getContentPane();
Component glassPane = getGlassPane();
glassPane.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent evt) {
System.out.println("Mouse motion event!");
glassPane.setVisible(true);
north.add(new JLabel("Last Name:"));
north.add(jtf);
center.add(jsp);
south.add(ok);
cp.add(BorderLayout.NORTH,north);
cp.add(BorderLayout.CENTER,center);
cp.add(BorderLayout.SOUTH,south);
/* Add menu items to menus */
file.add(fileOpen);
file.add(separator);
file.add(fileSaveAs);
edit.add(editCut);
edit.add(editCopy);
edit.add(editPaste);
help.add(helpAbout);
/* Add menus to menubar */
mb.add(file);
mb.add(edit);
mb.add(view);
mb.add(help);
/* Set menubar */
setJMenuBar(mb);
/* Add the action listeners */
fileOpen.addActionListener(this);
fileSaveAs.addActionListener(this);
editCut.addActionListener(this);
editCopy.addActionListener(this);
editPaste.addActionListener(this);
helpAbout.addActionListener(this);
/* Add the window listener */
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent evt)
dispose();
System.exit(0);
/* Size the frame */
pack();
/* Center the frame */
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
/* Show the frame */
setVisible(true);
public void actionPerformed(ActionEvent evt)
Object obj = evt.getSource();
if (obj == fileOpen);
else if (obj == fileSaveAs);
else if (obj == editCut);
else if (obj == editCopy);
else if (obj == editPaste);
else if (obj == helpAbout);
}
Similar Messages
-
I need help adding a mouse motion listner to my game. PLEASE i need it for
I need help adding a mouse motion listner to my game. PLEASE i need it for a grade.
i have a basic game that shoots target how can use the motion listner so that paint objects (the aim) move with the mouse.
i am able to shoot targets but it jus clicks to them ive been using this:
public void mouse() {
dotX = mouseX;
dotY = mouseY;
int d = Math.abs(dotX - (targetX + 60/2)) + Math.abs(dotY - (targetY + 60/2));
if(d < 15) {
score++;
s1 = "" + score;
else {
score--;
s1 = "" + score;
and here's my cross hairs used for aiming
//lines
page.setStroke(new BasicStroke(1));
page.setColor(Color.green);
page.drawLine(dotX-10,dotY,dotX+10,dotY);
page.drawLine(dotX,dotY-10,dotX,dotY+10);
//cricle
page.setColor(new Color(0,168,0,100));
page.fillOval(dotX-10,dotY-10,20,20);
please can some1 help meplease can some1 help meNot when you triple post a question:
http://forum.java.sun.com/thread.jspa?threadID=5244281
http://forum.java.sun.com/thread.jspa?threadID=5244277 -
Mouse motion listener for JTable with JScrollpane
Hi All,
I have added mouse motion listener for JTable which is added in JScrollPane. But if i move the mouse, over the vertical/horizontal scroll bars, mouse motion listener doesn't works.
So it it required to add mousemotionlistener for JTable, JScrollPane, JScrollBar and etc.
Thanks in advance.
Regards,
TamizhanI am having one popup window which shows address information. This window contains JTable with JScrollPane and JButton components to show the details. While showing this information window, if the mouse cursor is over popupwindow, it should show the window otherwise it should hide the window after 30 seconds.
To achieve this, i have added mouse listener to JPanel, JTable, JButton and JScrollPane. so if the cursor is in any one of the component, it will not hide the window.
but for this i need to add listener to all the components in the JPanel. For JScrollPane i have to add horizontal, vertical and all the top corner buttons of Scroll bar.
Is this the only way to do this? -
Mouse navigations problem with Sun Java 1.5.0_*
Hello gurus,
We need to move from JInitiator to Sun Java Plugin; according to the Forms10g Client Platform Support (http://www.oracle.com/technology/products/forms/htdocs/10gR2/clientsod_forms10gR2.html) one of the certified version for Forms 10.1.2.0.2 of the the Sun Java Plugin is 1.5.0_06 and Internet Explorer 6.0, we have tested also SJPI 1.5.0_09 and we get the same mouse navigation problem reported in the following Oracle Forum threads.
Mouse navigation don't work with Java Plugin.
Mouse navigation don't work with Java Plugin.
Forms 10gR2, Sun Java 1.5.0_06 gives mouse problems
Forms 10gR2, Sun Java 1.5.0_06 gives mouse problems
Does anybody know a solution to this problem?
Any help will be really appreciated.
Regards,
Victor.You need to use 1.5.0_12 or newer. The client SOD is not up-to-date on this specific point. A lot of navigation problems was fixed in this very release.
The Management team should change this in the client SOD, wonder why they still refer to 1.5.0_06. For Applications, the first 1.5.0 to be certified was _12.
Also make sure, that you are using the latest Patch 3 for 10.1.2 (10.1.2.3). This patch also contains fixes for a lot of these wellknown issues.
Hope this helps,
Jacob -
Plz Help! How to control mouse motion(use program to move/click the mouse)?
Hi people, plz give me some help on this: I wanna control mouse motion at another program's window, eg. control mouse movement in Internet Explorer's window. For a particular pixel on the screen, I need to move the mouse onto it instantly, then click. Please note that the java program(no applet) is going to run at the background, the current active window is some other programs like IE, notepad, etc.
Plz give me some clue about how to achieve this, thanks.
Do I need to do this through windows API or it can be done purely in JAVA?
Also, how to calculate the RGB color from a particular pixel on the screen?
Thank you very much for your time, plz help :).The same question each day (exe) !!!
There are many posts on this point!
All the soft that allows the build a native executable file on Windows
are not free!
You can take a look at JET excelsior
http://www.excelsior-usa.com/home.html
Denis -
Reg: MouseListener and Mouse Motion Listener Interfaces
Hi,
Good day,
I have added the mouselistener and mouse motion listeners to JButton..... With this i am trying to draw Circle and Line use mouse... When i was drawing at a particular point the actual drawn one is not placed at that point... so where can be error be present..how to overcome it.........
ByeInsufficient information to find a solution. Please go through these links and respond accordingly.
[How to ask questions the smart way|http://catb.org/~esr/faqs/smart-questions.html]
SSCCE
Use code tags to post codes -- [code]CODE[/code] will display asCODEOr click the CODE button and paste your code between the {code} tags that appear.
db -
Having choppy 24p motion problems.
Thanks for any info anyone might have on this.
I have a show open motion graphic sequence for a long running video series that has previously been produced in standard def NTSC and is now going HD.
It's a straight to Bluray/DVD instructional show.
The show is being shot in 1080/24p and the authoring house is insisting we deliver the final show 1080/24p (23.976 of course).
Unfortunately part of this open contains video clips floating right to left across the screen along with other graphic elements perfectly perpendicular to the screen and the motionit's is
strobing like crazy in 24p. This is not some kind of field type artifact, but just the problem of horizontal motion in 24p.
I have tried turning on motion blur and that helps some but due to the speed of the movement the video clips and graphic elements become very
blurry. I have tried adjusting the 3D camera shutter angle and of course can reduce the blur but that then increases the choppy motion problems.
I have told the producer I would be happy to make them a new open that avoided this problem but they are insistent they want to keep this one.
Does anyone have any workarounds on this?
How would this be done for graphics used in a feature film that would need to be 24p?
Note: I am rendering this out for use in Avid Media Composer 6.
Thanks!If your motion is at or near a critical speed then you have only one choice, change the speed. Take a look at this article. You may get some ideas. In my cinematographers handbook there's a chart of must avoid panning speeds. That's what you are getting with your horizontal movement. Unfortunately the only remedy is to change the speed of the move. You have to get it as close as you can to match the rest of the piece (audio, other elements) and then hide the rest with motion blur. Unfortunately, if you are right on a critical speed motion blur won't stop the strobing but it will make it less difficult to look at.
And to answer your question about doing this for a feature film at 24fps... you just have to follow the rules. In the same way a cinematographer must adjust his panning speed you have to adjust your design to the limitations imposed by the frame rate. -
AIR application object - mouse drag problem
Hi,
We face the following problem. Attached herewith are code sample and simulation for the problem. Would be great if someone could help us.
Problem: Objects created in AIR application experience mouse drag problem.
To simulate the problem please execute the sample application, a grey box (object) appears on the screen. Hold down left and right mouse buttons and drag the object and release both the mouse buttons. Object sticks to mouse pointer and moves along with mouse pointer to wherever it is dragged.
Steps to Reproduce - Hold down left and right mouse buttons and drag the object and later release both the mouse buttons. Notice Object sticks to mouse pointer and moves along with mouse pointer to wherever it is dragged. Object stops to move along with mouse pointer after sometime, then move the mouse pointer to the object and see that it sticks to pointer again and gets dragged along with the pointer.
Actual Result - Object sticks to mouse pointer and moves along with mouse pointer to wherever it is dragged.
Expected Result - Object should not stick to mouse pointer even after the mouse buttons are released.
Due to problems with uploading the attachment here, please send a email to the following email-id ([email protected]) and I shall send the attachment in response.
Thanks in advance.After some research, it seems this problem has been there for years with Bootcamp. Hope Apple can develop a better touchpad driver soon.
-
Detect Key Press and Mouse Motion in ONE event?
Hi
I am trying to create a functionality where a mouse motion from left to right performs one action, and the same motion while a given key is pressed performs another action.
How can i combine the two events? How do I unify the KeyEvent and MouseEvent, particulary when I need to capture the directional motion of the mouse together with the key press?
many thanks for any insightyou cannot actualy combine them into 1 event but there is an easy way around this:boolean mouseKeyPressed = false;
public void keyPressed (KeyEvent e)
if (e.getKeyCode () == ??) \\ or e.isCtrlDown(), e.isAltDown(), ...
mouseKeyPressed = true;
public void keyReleased (KeyEvent e)
if (e.getKeyCode () == ??) \\ or !e.isCtrlDown(), !e.isAltDown(), ...
mouseKeyPressed = false;
public void mouseMoved (MouseEvent e)
if (mouseKeyPressed)
//processAction
}hope this helps,
greetz,
Stijn -
How can i detect when the mose moves when the mouse is not located on the Frame?
Here is my mouse motion listener
private class EventHandler implements AWTEventListener {
private JPanel myPanel;
public EventHandler(JPanel thepanel){
myPanel = thepanel;
public void eventDispatched(AWTEvent event) {
if (event.getID() == MouseEvent.MOUSE_MOVED) {
MouseEvent me = (MouseEvent)event;
myScreenShooter.setCaptureLocation(me.getX(), me.getY());
myPanel.repaint();
}and I register it with a JPanel which I extended
Toolkit.getDefaultToolkit().addAWTEventListener(new EventHandler(this), AWTEvent.MOUSE_MOTION_EVENT_MASK);But the only time the motion is detected is when the mouse is within the panel...I need to know when it is moved anywhere on the screen.
Any ideas?
Thanksimport java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;
public class MotionTest
public MotionTest()
JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
new Monitor(label);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.setSize(300,100);
f.setLocation(200,200);
f.setVisible(true);
public static void main(String[] args)
new MotionTest();
class Monitor
JLabel label;
Point lastLocation;
boolean continueToMonitor;
NumberFormat nf;
public Monitor(JLabel label)
this.label = label;
lastLocation = new Point();
continueToMonitor = true;
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
new Thread(runner).start();
Runnable runner = new Runnable()
public void run()
while(continueToMonitor)
Point p = MouseInfo.getPointerInfo().getLocation();
double distance = p.distance(lastLocation);
if(distance != 0)
Toolkit.getDefaultToolkit().beep();
label.setText("mouse moved " + nf.format(distance));
lastLocation = p;
else
label.setText("mouse still");
try
Thread.sleep(250);
catch(InterruptedException ie)
System.err.println("interrupt: " + ie.getMessage());
} -
Constraining mouse motion to specific distance (width and height)
Hi,
I want to constrain the mouse motion over a canvas by allowing it to move through specified distance (width and height).
How can i do this?
What does the translatePoint() in MouseEvent do? How do i use it?
Can someone help me with this?
Thanks
NiteenThe correct way with Pixel Bender 1.0 is indeed to pass in the width and height as parameters. There are no ramifications.
-
For some reason my custom JComponent or cutom Jpanel do not detect any mouseMotion events. Does anyone know why that is?
I have a class like the following
public class GeometryPanel extends JComponent implements MouseInputListenerin my main class (the frame class) I have the following
JFrame frame = new JFrame();
JPanel p = new MyPanel();
JComponent geometryPanel;
JComponent buttonPanel = new ButtonPanel(this);
public MainWindow()
geometryPanel = new GeometryPanel(this,600, 600);
setupGUI();
displayFrame();
public void setupGUI()
p.setBackground( Color.white );
p.setPreferredSize(new Dimension(width, height));
geometryPanel.setPreferredSize(new Dimension(600, 600));
buttonPanel.setPreferredSize(new Dimension(620, 80));
p.add( geometryPanel );
p.add( buttonPanel );
public void displayFrame()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(p);
frame.pack();
frame.setLocationRelativeTo(null);
frame.show();
}For some reason all of the mouse pressed events are caught, but not the motion events. They are not fired.Ah I got it, thank you. Forgot to add the mouse motion listener next to the mouse listner
-
Hello Everyone!
I've written a mouse driver for Xsun (sparc). It is implemented as XINPUT module. The module calls miPointerDeltaCursor to move the cursor. mieqEnqueue is used to enqueue button changes.
The cursor moves correctly when the mouse is moved but any events (even events coming from miPointerDeltaCursor) are not done immediately but the moment I press a key on the keyboard.
The keyboard focus does not change when the mouse cursor crosses a window's border but the moment I press a key on the keyboard.
Mouse button clicks will be delayed until a key on the keyboard is pressed.
Any ideas why the X server behaves like this ??
Thanks.
Martin.Can you tell which GE60 model do you have? (there are different models of GE60 and we need the exact model for example GE60 0ND/2OD/2PE..)
Which external mouse are you using? can you give the model of the mouse and the driver version you installed?
Does the mouse freezing problem also happen when using another external mouse?
Also, can you explain little more details when you mentioned "It seems that this problem happens when I unplug the mouse."? -
How to deactivate mouse (motion) event coalescing (by EventQueue)?
Hello everybody,
I would like to know if it is possible to deactivate the event coalescing for mouse events. I refer to the following method found in "java.awt.EventQueue":
private boolean coalesceMouseEvent(MouseEvent e) {
EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
if (cache == null) {
return false;
int index = eventToCacheIndex(e);
if (index != -1 && cache[index] != null) {
cache[index].event = e;
return true;
return false;
} As I can see, all necessary method for modifiing the EventClass class are private. I did not found any method where I can explicitely deactivate the coalescing of mouse events.
Other "interesting" methods are:
- java.awt.EventQueue.coalesceEvent(AWTEvent, int)
- java.awt.Component.coalesceEvents(AWTEvent, AWTEvent)
But unfortunately, I did not manage to deactivate it.
For some background information: I would like to build a drawing panel for a Tablet-PC application to draw with a digitizer/pen on it. As an optimal goal, it should be as smooth as the Windows 7 handwriting panel (don't know the official name). The trivial implementation is not fast enough - drawing quickly produces too few points (round lines with edges). And I think one reason for this is, that some mouse (motion) events get coalesced/skipped while drawing - hence this request for support.
I'm greateful for any hint you may have
Thanks a lot!if (whatever) {
// (MouseEvent e)
e.consume()
} else {
// some usefull, but not EventQeue.push()
} you can consume Key and Mouse events
you have to check JComponentHierarchy because each of JComponent have got implemented KeyBindings (e.g. F2 edit JTable cell ...), -
Hi, I have GE60 notebook with Win 7 64-bit and my external mouse sometimes suddenly stops working. The mouse driver completely freezes. When I try to uninstall and reinstall the mouse driver in device manager it just stops responding. Same goes with shutdown - Windows tries to unload the driver but nothing happens and it just won't turn off. I have tried reinstalling windows, replacing the mouse with another, all kinds of stuff but I can't figure out what's the problem. It seems that this problem happens when I unplug the mouse. All drivers are official from MSI website. Thanks in advance
Can you tell which GE60 model do you have? (there are different models of GE60 and we need the exact model for example GE60 0ND/2OD/2PE..)
Which external mouse are you using? can you give the model of the mouse and the driver version you installed?
Does the mouse freezing problem also happen when using another external mouse?
Also, can you explain little more details when you mentioned "It seems that this problem happens when I unplug the mouse."?
Maybe you are looking for
-
Want to travel only with my Ipad. Would like to easily upload, with one click, large 7GB folders of both Excel spreadsheets and Word documents to ICloud and then be able to view, change and create individual spreadsheets and documents using Numbers
-
Friend responds to ichat and msg saying user did not respond
Problem: When I start an ichat with a friend, she accepts and soon after, ichat says the user did not respond, even though she did. We are both out of North America at the moment - could this be the problem? MacBook Pro Mac OS X (10.4.8)
-
HT3728 after soft resetting how to set a new password to my airport express
after soft resetting the airport exppress how to set a new password to my airport express
-
Export option in KE30 Custom report
Hi All, We have custom reports in KE30 and when we execute them the output is getting displayed. But for few users they are getting the option to export the data into file in the Menu options (Report -> export) but for few it is not coming. Any idea
-
Forcing an updated version of edited photos to another iOS device
I took some photos with my iPhone, they transferred to the iPad, and then I edited them (removed a filter). The updated versions have not transferred to the ipad. How do I force this to happen?