aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Mouse and Key Listeners don't work on JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mouse and Key Listeners don Watch "Mouse and Key Listeners don New topic
Author

Mouse and Key Listeners don't work on JPanel

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I want to use mouse listeners and key listeners in A Jpanel because i use it to edit a JPG. To allow listeners I use:

jWriteOnImage extends JPanel. The strange thing is that in a JFrame everything works perfectly, but the listeners don't work if the JPanel is in a JDialog.

Thanks for your help,

Alejandro Barrero


Your help will be greatly appreciated,
Alejandro Barrero
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

To get better help sooner, post a SSCCE that clearly demonstrates your problem.

And do you think you could indent your code with smaller/less indents? The snippet you posted is almost out of sight.


luck, db
There are no new questions, but there may be new answers.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Thank you very much Craig, but I don't think you are addressing my problem; I am afraid I did not explained clearly. What I am doing is adding listeners to the JPanel to react to events when the user clicks on the panel to select a position to type and when the user types a key to enter data into the JPane. My code works when the JPanel is in a JFrame (the user can enter text in different places and then can save the image with the text entries); the mouse listeners and key listeners do the work. My problem is that if I change the JFrame to a JDialog my code doesn't work.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
So it seems that your problem only occurs in specified situations, situations that we won't be able to reproduce without seeing your code I'll bet. My recommendation goes back to Darryl's: please post an SSCCE. You'll find a helpful link on how to create this creature and why it's important in Darryl's post again.

For instance, this is my SSCCE of your problem. Note that it's a bit too long for a true SSCCE, sorry. Also, to change the JTextField to a JLabel, you must press enter after entering text:

[ June 22, 2008: Message edited by: pete stein ]
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Thank you Pete; I enjoyed studying your code. My code also works when it is in a JFrame. Here are the two classes the I use:
The JPanel

The JDialog:
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
If you run mine, you'll see that it works the same for a JFrame as for a JDialog. Unfortunately I can't run yours as you are missing the TextDialog class code.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Here is the class Pete:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mouse and Key Listeners don't work on JPanel