aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Selecting portion of a 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 "Selecting portion of a JPanel" Watch "Selecting portion of a JPanel" New topic
Author

Selecting portion of a JPanel

Shitij Agarwal
Greenhorn

Joined: Dec 09, 2004
Posts: 21
I need some help understanding how I would need to structure my classes to obtain the following behavior.

I need to provide the ability for a user to select a portion of a JPanel such that the user should be able to see a box around the selecion area as he drags his mouse to select a portion of the JPanel. My final aim is to obtain the Rectangle coordinates for the selection, so that I can create an image out of the selected portion.

Now, from my little understanding, I think it would be necessary to subclass JPanel so that I can override paintComponent(Graphics) in order for the box to be displayed while dragging. Is this true? Ideally, wouldn't it be better if I could have a class A in which I create a JFrame, add a JPanel to it, and then do something like B b = new B(panel);

Now, the B class would really be the selection utility class, in which I do all the required initialization, look for mouse events and mouse motion events, and draw the box around the selected area of the panel.

Am I making sense? If you could please provide some basic code outline/structure on how to arrange my classes, that's be very helpful.

Thanks.
Paras Ahuja
Ranch Hand

Joined: May 22, 2012
Posts: 62
I have the same problem and i also need the solution of it desperately as i have to implement it in my java program.
I believe it must be possible as there are classes to do every piece of work in java. I just don't know how to do it.
Anyone out there please help.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
mousePressed gives you your starting point

mouseDragged gives you the rectangle from the starting point to the drag point,
changing the rectangle co-ords as it goes, and calling panel.repaint() for the panel's
paintComponent() to draw the dashed horizontal/vertical lines (BasicStroke)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selecting portion of a JPanel