Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: Hide Panel when clicking outside of it

  1. #1
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default Hide Panel when clicking outside of it

    I have a Swing application that is composed of a 30x30 grid of objects extending JLabels, call it ExtendedJLabel. When you click an ExtendedJLabel a panel appears which lets you edit the properties of the ExtendedJLabel. I have a save and cancel button which work well enough to close the panel, but I wanted to include the functionality so that if the user clicks anywhere else outside the panel (but still inside the application) the panel will be hidden, and the user will again have the opportunity to interact with the grid.
    I could of course put a click event handler on each ExtendedJLabel to close the panel if it is open, but this seems ugly and I also have in my application above the 30x30 grid a panel containing some other menu type objects likes scrollpanes and JButtons and JLabels etc. and I would have to put the same closing logic for the panel inside all those as well.

    I was hoping there was some kind of way to do a loseFocus event handler on my panel to keep logic in the same place. But my attempts have not been successful. Does anyone have any ideas for achieving this behavior?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    loseFocus event handler
    Have you looked at the FocusListeners? There is a focus lost method.

  3. #3
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    I did try some stuff with a FocusListener without success, but maybe I wasn't doing it right. I found that:
    "Focus events are fired whenever a component gains or loses the keyboard focus."
    from
    How to Write a Focus Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    So it cares about keyboard focus and my mouse events were not triggering it...

    So when I attached a FocusListener to my panel that becomes visible, the onLoseFocus event was not being triggered when I clicked elsewhere unless I set each of the other components to focusable. But that got me back to this panel working being dependent on these other compents having setFocusable(true). The problem I see with this is if I add another component later or a new component type, I have to remember to set it to focusable. I would rather of course keep this as self contained as possible.

    Am I doing this wrong?
    Is my thinking off?
    Is there a way to setFocusable(true) to the parent panel that contains all these sub components and have the focusable bubble up or something so I only need it in one place?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    I guess you need to create a small program(SSCCE) to demonstrate the problem.
    I'm not sure what components you are interested in keeping track of knowing when they lose focus. If you add new components that can't get the focus then they can never lose it. But the following statement implies that you are worried about their being in focus but they can't get the focus without your coding for it. ???
    if I add another component later or a new component type, I have to remember to set it to focusable

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Use a JPopupMenu. You can add any component to it. Then you get this behaviour for free.

    Otherwise if you want to use a JDialog then you would add a WindowListener to the dialog and then handle the windowDeactivated() event.

  6. #6
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    Norm - It's hard to describe which is why you suggested the small example of my program. I don't think I did the best job of explaining it. But what I was trying to say was I have just one panel that I want to attach the loseFocus event to. All the other components don't have this loseFocus behavior nor do I want them to. When the panel which basically acts as a pop-up editor for the other components loses focus I want it to dissapear. This should happen if the user clicks anywhere but on the panel.

    I was saying the only way I got the loseFocus event to fire within this 'pop-up' like panel was if I explicitly made each of the other components focusable. I didn't want to do it that way because if I add new components in the future then they have to be set to focusable so that the original 'pop-up' like panel will react to them being clicked. But They don't necessarily need to be set to focusable. I didn't want this dependency.

    I hope it's more clear, but for now I will try Camickr's advice...Sounds like I was trying to write something the JPopupMenu already does...
    Last edited by steeveesas; 08-12-2011 at 08:27 PM.

  7. #7
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    Camickr - thanks for the advice. I will try a JPop up, sounds like I can have JComboBoxes and Icons etc. in there...if that's the case that is all I need.
    I will let you know how it goes later when I get home and try it.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    I have just one panel that I want to attach the loseFocus event to
    For What components do you want to know when they have lost the focus?
    If only one component, then The FocusListener should work. When it loses focus, setVisible(false);
    If more than one, then they ALL would need listeners.

    Are you saying that if only one component in your GUI can receive focus, then it never loses focus unless some other component gets the focus? Then maybe you need some other kind of event to indicate that the user is done with the component. Another choice is a mouse listener.

  9. #9
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    In the picture users can drop ExtendedJLabels (a1, a2, etc.) on the grid and delete them (this works). Then when they select an edit tool from another menu they can click on them and the blue pop-up like panel becomes visible and they can edit the properties. For example in the pic. the user clicks on a1 with the edit tool selected and the blue box comes up showing a1's properties. I want them to be able to click anywhere inside the app (red box) other than the blue pop up and the blue pop up panel goes away.

    That is the basic problem I am trying to solve.

    If I try to do it through FocusListeners, then I need to attach focusListeners to each component inside the blue pop up panel that can get focus. like a button or a drop down or a text field and the panel itself if the background is clicked.

    But when I tried this, clicking outside the panel would not close the panel (setVisible(false)) unless I put focusable(true) on all the other compenents within the app.

    I can try to write a mock up example tonight to show you.
    Attached Thumbnails Attached Thumbnails Hide Panel when clicking outside of it-untitled.jpg  

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    click anywhere
    That sounds like the place for a mouse listener.

    How about a Done button on the properties form? User clicks when finished with the form

  11. #11
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    I do have a save and a cancel which both close the panel (with save saving info first), but i wanted to add functionality to close the panel if the user just clicked elsewhere to operate like the cancel button.

    I don't see how a mouse listener can do this. Every other component in the app would need the listener to close the panel when clicked right?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    if the user just clicked elsewhere
    There would have to be some kind of listener to know when the event happened.
    Would just moving the mouse off the input panel be enough? I doubt user's would like that. I'd think a user would want a very positive action(click on a button) on his part to close the input panel.

  13. #13
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Would just moving the mouse off the input panel be enough?
    I don't want a onMouseExit listener, cause that would be annoying if the user accidently moved off. I want an intentional action like a click.

    Quote Originally Posted by Norm View Post
    I'd think a user would want a very positive action(click on a button) on his part to close the input panel.
    Suppose for example, the user chooses to edit a1 and so they click on a1. Then suppose they actually decide to edit a2. Instead of having to go close the pop-up panel and then move the mouse over to a2, they should be able to just click edit a2. When this happens I wanted to first close the edit panel for a1 and open the edit panel for a2 (they may have different properties to edit).

  14. #14
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    If more than one, then they ALL would need listeners.
    And that is why you would use the WindowListener (if you don't use the pupup menu approach).

    In my opinion a FocusListener or a MouseListener is the wrong approach.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    click edit a2. When this happens I wanted to first close the edit panel for a1 and open the edit panel for a2
    If the click on a2 is recognized as an event then the code can see that the a1 input panel is open and close/cancel it.

  16. #16
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    If the click on a2 is recognized as an event then the code can see that the a1 input panel is open and close/cancel it.
    Why go to all that trouble?

    Lets assume the forum has "n" different buttons the user can click. Each button displays a property panel of some kind. The ActionListener for each button should only be responsible for showing the property panel for that button. It should not be responsible for checking if any other property panel is showing.

    I don't understand why this discussion is continuing along the FocusLIstener or MouseListener approach. Both solutions are not scalable as they become more complicated with every button/panel that needs to be supported.

  17. #17
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    Camickr has nailed it. that is the problem with the above approaches in my application, even though they would work. at the moment I have about 10 different panels that show up and more could come in the future and I don't want each to check to see if every other panel is open and close it if it is. That's a lot of dependent and replicated code that would have to change in multiple places each time I change something. I want to contain everything in the pop up panel for self containment. It sounds like the JPopupMenu can work as long as it can contain arbitrary objects. I will try it tonight hopefully and let you know how it goes.

    Camickr, the discussion went on I think because I felt I had not explained it well and owed it to Norm to answer him as he had some good suggestions based on my limited explanation. I had sort of been down those routes and noticed the scalability problem.

    Thanks to everyone for their help.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    Some of the problems could be resolved by using a listener and interface method. When a panel is opened, it would register with a listener. The listener would see if another panel was open, if so, close the other panel and then register the new panel as open.

  19. #19
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Some of the problems could be resolved by using a listener and interface method. When a panel is opened, it would register with a listener. The listener would see if another panel was open, if so, close the other panel and then register the new panel as open.
    You are attempting to create a more complicated interface.

    What is the problem with either the JPopupMenu (which has this functionality built in), or using a WindowListener (with about 3 lines of code)?

    Why are you trying to force a solution when simple solutions exist?
    DarrylBurke likes this.

  20. #20
    steeveesas is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    My personal dev. machine crashed and won't even boot up...seems fried. I will let you know how things end up once I'm back 'online' with a new machine...might be a week or two before I get one and try this out.

Page 1 of 2 12 LastLast

Similar Threads

  1. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:29 PM
  2. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 08:43 PM
  3. right clicking JTable row
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 06-27-2010, 11:41 PM
  4. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:56 AM
  5. painting lines when clicking a panel
    By yuriythebest in forum New To Java
    Replies: 0
    Last Post: 12-14-2008, 09:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •