Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mouse and Key Listeners don't work on JPanel

 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the class Pete:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic