• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addMouseListener to JFrame

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic