How can I make a button that shows a popup menu when you click in it?

72 views
Skip to first unread message

James Elliott

unread,
May 24, 2016, 6:59:24 PM5/24/16
to seesaw-clj
I have contextual-menu style popups working fine, but I would like to give new users a visual indicator that the contextual menu exists by having a button with a gear on it which, when clicked without modifier keys, brings up the same set of choices. But I am having trouble figuring out how to get the popup menu positioned correctly, and even more importantly, getting it to go away when you choose an option or click outside of it.

Is there any way to do this?

Dave Ray

unread,
May 25, 2016, 12:31:21 PM5/25/16
to seesa...@googlegroups.com
I think the popup example is probably a good starting point for what you want to do: https://github.com/daveray/seesaw/blob/develop/test/seesaw/test/examples/popup.clj

Dave

On Tue, May 24, 2016 at 3:59 PM, James Elliott <ja...@brunchboy.com> wrote:
I have contextual-menu style popups working fine, but I would like to give new users a visual indicator that the contextual menu exists by having a button with a gear on it which, when clicked without modifier keys, brings up the same set of choices. But I am having trouble figuring out how to get the popup menu positioned correctly, and even more importantly, getting it to go away when you choose an option or click outside of it.

Is there any way to do this?

--
You received this message because you are subscribed to the Google Groups "seesaw-clj" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seesaw-clj+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Elliott

unread,
May 25, 2016, 12:36:36 PM5/25/16
to seesaw-clj
Thanks, Dave, I took a look at that example, and I can’t understand how I would extend it to do what I want. In the example, all of the popups are being triggered by the normal platform context-menu event, so they can take advantage of seesaw’s great :popup feature. I am already using that to create a context menu with dynamic content.

The part I am missing is how I can cause that popup to be displayed, in the right place, on an ordinary (non context-menu) mouse event in a button.

Cheers,

  -James

James Elliott

unread,
May 25, 2016, 12:41:26 PM5/25/16
to seesaw-clj
And, by the way, thanks for this fantastically productive framework that let me throw together a system in about a week that was already used to synchronize visuals with music at the Electric Elements music festival this past weekend: https://github.com/brunchboy/beat-link-trigger#beat-link-trigger


On Wednesday, May 25, 2016 at 11:31:21 AM UTC-5, daveray wrote:
Electric Elements.jpg

Dave Ray

unread,
May 25, 2016, 1:09:12 PM5/25/16
to seesa...@googlegroups.com
Gotcha. In that case, you probably want to use a modified version of the popup handler:


In your case, I think you'd listen for a mouse event, but just change the `isPopupTrigger` handling to whatever you want to trigger on. It might be cleaner to attach this code to a button :action. In any case, that code shows how to properly position the menu.

Dave

Dave Ray

unread,
May 25, 2016, 1:09:41 PM5/25/16
to seesa...@googlegroups.com
oh, and it's awesome you're finding it so useful! :)

James Elliott

unread,
May 25, 2016, 2:49:51 PM5/25/16
to seesaw-clj
Brilliant, that was precisely the direction I needed. Here is the simple function I ended up with:

(defn- show-popup-from-button
  "Displays the popup menu when the gear button is clicked as an
  ordinary mouse event."
  [target popup event]
  (.show popup target (.x (.getPoint event)) (.y (.getPoint event))))

I listened for :mouse-pressed rather than :action because I wanted the menu to show up on the down-stroke, and that turned out to be a good thing because when I quickly tried :action to compare how it would feel, it turned out that the ActionEvent was missing the X and Y coordinates I needed anyway.

I suspect the biggest thing I was missing in my earlier efforts was attaching the popup to the target component, that may be why it was never going away.

Thanks again! Perhaps it is somewhat ironic that one of the authors of the second edition of O’Reilly’s “Java Swing” is finding it so delightful to not have to deal directly with Swing… but then again, that should not really surprise anyone. ;^)

Dave Ray

unread,
May 26, 2016, 12:20:13 AM5/26/16
to seesa...@googlegroups.com
I'm glad it worked :)

re: "Java Swing". You just blew my mind. That's crazy.

Dave
Reply all
Reply to author
Forward
0 new messages