0

Is it possible to create a custom dialog in JavaFX which isn't an OS-level window? (It would be a popup window displayed over top everything else on the main stage, which can't leave the application window.) i.e. An Adobe/Apache Flex style dialog, for anyone familiar.

User
  • 293
  • 3
  • 19
  • 1
    ControlsFX used to have this feature ([see lightweight dialogs](http://fxexperience.com/controlsfx/features/dialogs/)). However, recent builds of ControlsFX [dropped the lightweight dialog feature](http://stackoverflow.com/questions/26341152/controlsfx-dialogs-deprecated-for-what). Instead, ControlsFX now uses only the dialogs implemented in the JavaFX core API, which are self-contained stages. You might wish to review some [old branch source code of ControlsFX](https://bitbucket.org/controlsfx/controlsfx/branches/) to see how they achieved their original lightweight dialog implementation. – jewelsea Aug 22 '16 at 20:48
  • ControlsFX [lightweight dialog source implementation](https://bitbucket.org/controlsfx/controlsfx/src/371bca0e905536ca6742d181fdade07258260659/controlsfx/src/main/java/org/controlsfx/dialog/LightweightDialog.java?at=8.0.6_20&fileviewer=file-view-default) (now unsupported). – jewelsea Aug 22 '16 at 20:57

3 Answers3

1

Its actually quite easy and without a need to use 3rd party library:

Embed your root container within stackpane and over the top put anchor pane , create dialog box control(container/containers+controls).

To demonstrate what i mean here is a small fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<StackPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: black;" />
      <AnchorPane prefHeight="200.0" prefWidth="363.0">
         <children>
            <BorderPane layoutX="84.0" layoutY="123.0" prefHeight="99.0" prefWidth="227.0" style="-fx-background-color: red;">
               <center>
                  <Button mnemonicParsing="false" text="Button" BorderPane.alignment="CENTER" />
               </center>
            </BorderPane>
         </children>
      </AnchorPane>
   </children>
</StackPane>

Looks like : enter image description here

You can make it moveable with properties,listener. It will require some work bud its doable.

Bud as jewelsea mentioned controlsfx has already what you need implemented, if i remember correctly they also provided controlfx samples jar where you can see sourcecode and all the included stuff in action, first take a look at that if anything in there fits your needs.Its really trivial to then implement it from that point on, i only had trouble in a past with one of theyr controls when i updated to new version of java.

Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
  • Don't really need it to be movable, just a static container like that should do the trick! – User Aug 23 '16 at 05:46
0

You can listen to the X and Y property. Here is how I update the position of dialog base on the root stage position.

public static Stage getStage() {
    return StageHelper.getStages().get(0);
}

public static void stayInRootStage(Dialog dialog) {

    Window window = dialogPane.getScene().getWindow();
    Stage mainStage = getStage();

    ChangeListener<Number> yListener = new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            double newY = mainStage.getY() + mainStage.getHeight() / 2 - dialog.getHeight() / 2;
            dialog.setY(newY);

        }
    };

    ChangeListener<Number> xListener = new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            double newX = mainStage.getX() + mainStage.getWidth() / 2 - dialog.getWidth() / 2;
            dialog.setX(newX);
        }

    };

    mainStage.getScene().getWindow().yProperty().addListener(yListener);

    mainStage.getScene().getWindow().xProperty().addListener(xListener);
}
Mai Hữu Lợi
  • 559
  • 2
  • 8
  • 18
0
Dialog dialog = new Dialog();

dialog.initStyle(StageStyle.UTILITY);
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '22 at 04:53