2

Resources and examples for this Popup widget are vague.

Suppose I have a random Node somewhere on the stage. How do I open a Popup exactly under it (e.g. like a dropdown menu, but with other nodes inside it).

I'm trying to avoid boilerplate code (i.e. fine-tuning the position myself).

Update 1:

Either Point2D point = node.localToScene(0.0, 0.0); does not work as I imagine it should, or I'm using it wrong.

Update 2:

See here a simple example, but lacking the functionality I'm needing

Community
  • 1
  • 1
Georgian
  • 8,795
  • 8
  • 46
  • 87

1 Answers1

3

Let's say you have the node node

you can get its position by

Point2D point = node.localToScene(0.0, 0.0);

// now get point.getX() and point.getY() here

Considering the example that you have given (in Update 2):

I removed this bit:

popup.setX(300);
popup.setY(200);

and modified this code:

show.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent event) {
          popup.show(primaryStage);
          Point2D point = show.localToScene(0.0,  0.0);
          popup.setX(primaryStage.getX() + point.getX());
          popup.setY(primaryStage.getY() + point.getY() + 40); 
          // this 40 could be show.getPrefHeight() if height of button is set
      }
});

Since Popup is a separate window, you need to set its position by adding the offset of the Stage.

AKS
  • 18,983
  • 3
  • 43
  • 54
  • [Notice the "Meh" text in the upper left corner](http://oi58.tinypic.com/axk6dd.jpg). The label is placed on the popup, and the popup is opened at those coordinates taken from the "Show" button. – Georgian Mar 25 '14 at 10:50
  • A little bit of code would also help in understanding it better. – AKS Mar 25 '14 at 11:28
  • Frankly there's not much to it. [See here the example I've used](http://stackoverflow.com/a/12587771/1774643). Now it would be nice for that circle to "stick" to the "Show" button each time it's opened. – Georgian Mar 25 '14 at 11:32
  • @GGrec look at the updated answer. It worked for me. – AKS Mar 25 '14 at 11:58
  • This solution is a bit incovenient since I need the stage to properly position the popup on the screen. However, it is a resonable workaround for something that does not exist out of the box in JavaFX. Thanks. – Georgian Mar 26 '14 at 14:04
  • @GGrec _I need the stage to properly position the popup on the screen_ hmm, don't quite understand what you mean here: you want the control of the positioning somewhere else than the eventHandler? If so, where exactly is the problem? As long as you have a reference to the node and the popup, any code listening to the popup getting visible could calculate the location. – kleopatra Mar 27 '14 at 12:57