• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to build focus rectangle  RSS feed

 
James Goh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi does anyone have any idea how to build those rectangles that appears around an object when you clicked on it in wysiwyg software application like GUI designer in IDE and other paint software?

I tried to use Rectangle, but it is not visible, I need to show which object is focused (clicked) to the user. Thanks!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're talking about components:

You can save a reference to the original border for restoring it later when the component becomes de–selected or you can use it as the inside of a compond border with the line border on the outside, extracting it later for the reset.

If you're talking about shapes rendered in paint code it's a little more involved.
[ May 31, 2004: Message edited by: Craig Wood ]
 
James Goh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay that works, but I wanted to let the user to be able to resize the "border" first before actually resizing the component. I.e when the user resize the component, the "border" is resized first and when the user release the mouse button, the component would resize to the size of the border.

similarly for moving the component, when the user clicked the component and then drag, it drags the "border" with the mouse pointer( to show where the new location should be) while the actual component's location remain unchanged.When the user release the mouse button then component is actually moved to the location where the "border" is placed.

how do they do that? I thought of putting the component on top of a JLabel component. Where the JLabel is slightly bigger than the component to show a border surrounding the component. when the user select the component, the JLabel is set visible, when you resize or move the component, the JLabel get resize or moved first , and the new size of location is used to modify the component's size.

Is there a better way to do it?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a way to move the components around after mouse release. It's fairly easy to add the selected "border" functionality to this. I didn't do any more because I don't know where you're headed with it, especially the user interaction part for the resizing...
 
James Goh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks this is just what I wanted! Using the same method, I think I should be able to add function for resizing the components. I am always thinking of adding listener to individual component instead of adding listener to the container and then deciding which component was clicked. Your code is much easier to maintain!
 
James Goh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I was trying out the code you have posted previously and I find that when I use component such as JButton or JTextField, I am not able to get to the mousepressed event of the JPanel but i have invoke the JButton pressed event.Is there anyway to get around this?

I tried to disabled the JButtons, but it didnt work, I am thinking maybe I need to pass the JButton's event to the panel's mousepressed event?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way to do it. This doesn't deal with passing MouseEvents through to the DragPanel JButtons but it does remove the problem of the buttons consuming the MouseEvents. This is an alternative (fake) glass pane using OverlayLayout to position one panel over another.

All mouse code goes with the top, non–opaque JPanel (actionPanel). Since OverlayLayout makes all of its components the same size the MouseEvent coordinates of actionPanel will map directly into DragPanel. Therefore, we are free of translation challenges. The only other significant change is the need for DragAction to register itself (during construction) with DragPanel.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!