• 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

How to pass mouse event to a Label

 
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

How to pass mouse events to a Label, between 2 classes ?
Can you please help ?

Here below a simple example.

I have Class: "WinEnthalpie" which is creatinging the User Guide Interface
In this interface, there is a label component "lblCoordonate": which goal is to indicate the current coordinate of the mouse of panel: "panel_draw" = type: "PanelEnthalpie"

I would like recover this mouse coordinates from my Class "PanelEnthalpie" where there is a method : "mouseMoved", and which is doing that perfectly ;-)


What is the best approach to perform this task ?

Many thanks in advance


Regards

Christian






 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your paintComponent() method should NEVER call dispose() on the graphics instance passed to it.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even this will not answer my question.
Is there a reason why not call "dispose()" in paintComponent() ?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because dispose() is intended for closing the frame.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
OK.
I have removed all dispose() in, the example.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Christian,

a simple way to get those mouse coordinates to the panel is to create a MouseListener and/or a MouseMotionListener in your WinEnthalpie class. This Listener simply reports the mouse coordinates to a dedicated JLbel in the WinEnthalpie panel.

For instance:

in your initialize() method in WinEnthalpie I added this:



and a few lines further I invoked the PanelEntalpie constructor as follows:


Annd lastly, in th constructor of PanelEnthalpie:

It works for a mouse drag, but if you want the coordinates for a mouseclick as well, then just add a mouselistener too.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet,

What a smart solution. :-)
Great !

On my side I was focused to access, from method mouseMoved(), to the "parent"  and then to update the label.
But your solution is much easier....

Many many thanks for your prompt answer

Merci
Christian  
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short modification:
in code:

I replaced
mouseDragged
by
mouseMoved()

Which is much appropriated
:-)
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you not come across the MouseAdapter and MouseMotionAdapter classes? They will allow you to implement only those methods you actually need, by overriding them. Make sure to mark the overridden methods with the @Override annotation.You often implement it as an anonymous class; because mouse listener is not a functional interface, you cannot use a λ.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK,
Was not aware of these abstract Class.

The first proposal worked well, I had just to click on the mouse to update the coordinate, by replacing with mouseMoved() , the code did what I expected.
I will let it like that.
As seen in my example, I inkode the separate classes:
MouseWheelListener, MouseListener, MouseMotionListener.
Perhaps not the most optimized, but work :-)

Thanks
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what Campbell is meaning, since we did use Adapters.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It wasn't you I meant, PS; If you look at lines 38 and 108‑128 in the first post, you find the panel implements several listener interfaces and there are three methods implemented empty. A quick look through the documentation for MouseAdapter (as in link in previous post) tells us, it has eight methods and implements four interfaces. Cay Horstmann warns against the XYZAdapter classes in earlier editions of his book because the tiniest spelling error can cause a method not to work:-Now, reinstating the @Override annotation will cause the compiler to complain about the tiny spelling error. With @Override it is perfectly all right to use an XZYAdapter class.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I will rise the difficulty :-)


allows to get the coordinates of the mouse (x,y).
But suppose we would like to get with this method, the results of an operation performed in class: PanelEnthalpie
for instance for abscisse :
x = 140 + (530-140) * (X/zoom-xOrigineH) /  (xFinalH-xOrigineH)
where: zoom,xOrigineH,xFinalH are some private variables of class : PanelEnthalpie

OK, I could get the variables: zoom,xOrigineH,xFinalH, thanks to get methods, and perform the operation in MouseMotionAdapter(), but is there a smarter solution ?

Thanks

Christian


 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget my stupid question.
I will handle as following.
Call a method declared in PanelEnthalpie, with as input parameters the coordinates (x,y) and then return the computed value...
Much easiest :-)

Christian
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is easy enough, but it can be automated. I use in these cases sometimes a PropertyChangeSupport variable, and fire a PropertyChange around to any listener interested. It is a bit of work to get this up and running, unfortunately, but after that the pocess is completely automated and you do not need to call a special method on your PanelEnthalpie to get these updated coordinates.

But what you wrote, as said, is easy enough.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too Greenhorn to address this level of approach.
However, I will keep that behind of my mind :-)

Thanks
Christian
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Klugesherz wrote:. . . Much easiest :-)

Christian

And much more object‑oriented, too. The zoom is a feature of the panel, so let the panel do the calculations for you.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Klugesherz wrote:Too Greenhorn to address this level of approach.
However, I will keep that behind of my mind :-)

Thanks
Christian


You're welcome! But for a greenhorn I find your code pretty advanced and well written! So enjoy a truly deserved cow!
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great :-)
Thanks
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know I should open a new topic for the next question, but as it is closely linked to the topic I try to adress, I will ask it here.
Thanks in advance

The image in my panel is widther and higher that the panel itself.

With help of  
public void mousePressed(MouseEvent evt)
and
public void mouseDragged(MouseEvent evt)
I succeeded (with mouse click entry point, and mouse Drag Points) to compute (xImgOrgRef,yImgOrgRef) == Image Relative position  / Jpanel.
This is working well.

Now I have seen, that through by zooming:

 
The coordinates of "Image Relative position  / Jpanel" can also change.
Unfortunately, here I don't see how to compute : (xImgOrgRef,yImgOrgRef) based on the Zoom Factor.

Is there a simple way to get (xImgOrgRef,yImgOrgRef) ?



Many many thanks in advance

Christian
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To not pollute this topic, I will create a new topic .
This point can be considered closed.

Thanks for all your help

Christian
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there a reason why not call "dispose()" in paintComponent() ?


your paintComponent() method is called from the superclass's paint() method, which passes the Graphics instance to it. But the paint() method continues to use the graphics instance after your paintComponent() method returns. if you have called dispose() on that Graphics instance, you will have released some resources that the paint() method may need to use later, causing erratic or undefined behavior.

In general, it is rarely a good idea to modify anything that comes through the calling sequence unless you restore it before returning (unless, of course, you know that the caller wants or expects you to modify some of the resources of such an object).

 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is understood, and my code has been modified as highlighted above.
As my first question has been answered meantime, I propose to close this topic.
.
The new question is relative to a method to get the coordinates of image / Jpanel.
Topic open: https://coderanch.com/t/673538/java/Image-Relative-position-Jpanel

Merci

Christian
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic