Forum Stats

  • 3,853,797 Users
  • 2,264,275 Discussions
  • 7,905,449 Comments

Discussions

how to fit object according to screen resolution in javaFX

3463663
3463663 Member Posts: 7
edited Jun 19, 2017 10:25AM in JavaFX 2.0 and Later

Hi all,

I am new in javaFX development. I want to know how to fit object according to screen resolution in javaFX. I means how my button, label , table like object fit to screen according to the resolution..

Answers

  • Muzib
    Muzib Member Posts: 10
    edited May 8, 2017 11:38AM

    There are numerous ways to do that.

    If you only care about the height of the nodes (button , labels) , then use a VBox and set the components'  vgrow to always.

    VBox.setVgrow(button1, Priority.ALWAYS);

    On the other hand, if you care about width, use a HBox and set the components'  hgrow to always.

    If you want both of width and height of the nodes to fit to the screen, then you can :

    1.      Use a gridpane. GridPane will resize the nodes of the screen according to screen size.
    2.      Use a VBox inside an AncorPane and set the constraints to 0 , minSize to 0 , maxSize to Double.MAXVALUE like this:
    <?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <VBox layoutX="53.0" layoutY="25.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
             <children>
                <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" prefHeight="41.0" prefWidth="458.0" text="Button" VBox.vgrow="ALWAYS" />
                <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" prefHeight="41.0" prefWidth="458.0" text="Button" VBox.vgrow="ALWAYS" />
                <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" prefHeight="41.0" prefWidth="458.0" text="Button" VBox.vgrow="ALWAYS" />
                <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" prefHeight="41.0" prefWidth="458.0" text="Button" VBox.vgrow="ALWAYS" />
                <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" prefHeight="41.0" prefWidth="458.0" text="Button" VBox.vgrow="ALWAYS" />
                <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" prefHeight="41.0" prefWidth="458.0" text="Button" VBox.vgrow="ALWAYS" />
                <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" prefHeight="41.0" prefWidth="458.0" text="Button" VBox.vgrow="ALWAYS" />
             </children>
          </VBox>
       </children>
    </AnchorPane>

    I suggest you to use the scene builder to test different possible cases and choose one that fits your application.

  • 3463663
    3463663 Member Posts: 7
    edited Jun 17, 2017 3:24AM

    Thanks for help how do I use this same concept for the BorderPane Layout

  • Muzib
    Muzib Member Posts: 10
    edited Jun 19, 2017 10:25AM

    In scene builder, I set the Min Width and Min Height to USE_COMPUTED_SIZE and this works fine.

This discussion has been closed.