GeeCON Prague 2014*
The moose likes Java in General and the fly likes addMouseListener to JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "addMouseListener to JFrame" Watch "addMouseListener to JFrame" New topic
Author

addMouseListener to JFrame

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi,
I have a Text Editor program I am working on. I need to be able to cut, copy, and paste with a right click of the mouse. I have this simple code here which does work:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test100
{
Test100()
{
JFrame theFrame = new JFrame();
theFrame.setSize(200,300);
theFrame.show();
theFrame.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
boolean test = me.isMetaDown();
if (test == true)
{
System.out.println("Meta is Down");
}
}
});
}
public static void main(String [] args)
{
new Test100();
}
}

However, in my project I have a JTextPane added to a JScrollPane added to a Container added to a JFrame. I try to add the MouseListener to the JFrame and the code compiles but does not work. I have also tried adding the MouseListener to the Container, and that too does not work. Does anyone have any ideas? Thanks in advance!!!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
The reason your example works is because the JFrame is on the top. In your actual code you said that you had the following from back to front:
JFrame
Container
JScrollPane
JTextPane
You said that you have attached the adapter to JFrame and Container. My suggestion is to place the adapter on the JTextPane. Two reasons come to mind: 1) The JTextPane is the only component that is present (if it fills the entire frame, which depends on Container), and 2) That is the component that you should be clicking in to apply cut/copy/paste.
Good Luck,
Manfred.
[This message has been edited by Manfred Leonhardt (edited January 10, 2001).]
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Actually, that did not work either. I am stumped! I am open to any more ideas you or any else might have. Thanks!!!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
I might not be understanding your exact problem. But the example below works exactly as I have described it to you. Maybe you can start from it and make it exactly like your situation.

Good Luck,
Manfred.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Manfred,
Are you saying that you actually compiled this and got it to work? I copy and pasted your exact test case and compiled it. When I ran it, it did absolutely nothing. I thought maybe it was the version of jdk I was using. (1.3) So, I recompiled under 1.2., and that also did not work. Very confusing to me....... It seems like it should work. What are your thoughts?

Barry
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Barry,
Yes I compiled the code and it works. I am using 1.2. I ran the example under Unix, Win98, and WinNT. Maybe it could be that you have a different OS (WinME, Win2000)?
I am stumped also ...
Sorry,
Manfred.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Manfred,
I have solved the mystery. I am using JDeveloper, and for some reason System.out is not working. So, when I ran your test case from the command line, I can see "Meta Is Down". So that part works fine, now I just have to figure out how to make my program do what I want it to after that. I am glad that puzzle is over. Thanks for your help!
Barry
 
Don't get me started about those stupid light bulbs.
 
subject: addMouseListener to JFrame