aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Opening window by clicking Paint Area ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Opening window by clicking Paint Area !" Watch "Opening window by clicking Paint Area !" New topic
Author

Opening window by clicking Paint Area !

Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
Hi friends,


I am new to Java swing ! Any body can tell me that in this code




Whether is it possible to click on #Paint Area and open new window or frame or dialog ?

Thanks in advance !


God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
OCPJP6
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
You would never add code like that to the paintComponent() method.

You add a MouseListener to the panel. Read the section from the Swing tutorial on How to Write a Mouse Listener for more information.
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
Rob Camick wrote:You would never add code like that to the paintComponent() method.

You add a MouseListener to the panel. Read the section from the Swing tutorial on How to Write a Mouse Listener for more information.


Thanks Rob for reply!
But I want to open a new window as user click on the list item of Paint Area .Is is possible or not ?

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
I already gave the answer, there is no need to repeat myself.

Of course it would appear to me it would be easier to just use a JList so you don't reinvent the wheel.
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
OK Thanks Rob !

Thanks a lot ! I got it !

Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
Ashish Tiwari wrote:
Rob Camick wrote:You would never add code like that to the paintComponent() method.

But I want to open a new window as user click on the list item of Paint Area .Is is possible or not ?



Actually your approach is complicated since you want to detect the string drown on the panel by using a mouse click.
You can do that with the following approach ( not recommended; the recommended is to use a list on the panel)

for each String draw a rectangular boundary ( g.drawRect..) and over the rectangle location put you string ( g.drawString(...))
you add a mouse Listener to the panel
if mouse is being clicked, you get the X, and Y location of the mouse and determine if it is in the boundary of your String ( actually you define your boundary
for each string ), then you retrieve it from the definition store ( some kind of data structure which maps you String literal to location on the screen, this defintion store is build relatively w.r.t panel size; so any panel resizing, will re-map the String literal to new location so that you can draw them again)

Just an idea and thoughts






Regards,
Alan Mehio
London, UK
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
Thanks Alan Mehio !

Thanks Alan for giving me solution .Thanks
 
wood burning stoves
 
subject: Opening window by clicking Paint Area !