Search code examples
javaxmljavafxscene

My JavaFX Scene is a mess


I'm having a very hard time aligning items in my JavaFX scene.

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>

<Pane minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.controller.TableViewController">
    <ScrollPane>
        <TableView fx:id="tableView" prefHeight="500.0" prefWidth="1100.0" snapToPixel="false" styleClass="100">
        </TableView>
    </ScrollPane>
    <ToolBar maxHeight="35.0" prefHeight="35.0" prefWidth="1200" style="-fx-background-color: #8C0000;">
        <ImageView fitHeight="35.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
            <Image url="@../images/qmit-search-toolbar-logo.png" />
        </ImageView>
        <TextArea fx:id="linkInput" maxHeight="30.0" maxWidth="250.0" />
        <TextArea fx:id="keywordInput" maxHeight="30.0" maxWidth="100.0" />
        <Button fx:id="buttonSearch" maxHeight="30.0" maxWidth="100.0" text="Search QMIT" />
    </ToolBar>
</Pane>

My goal is to have a ToolBar with a logo, two input text fields, and a button. Beneath, a table that is centered in the screen. What am I doing wrong here that I end up with such a tangled mess?


Solution

  • A Pane does no layout on its own.

    (As an aside, note that a TableView provides its own scrolling functionality: don't wrap it in a scroll pane.)

    Use, for example, a BorderPane:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.ScrollPane?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.control.TextArea?>
    <?import javafx.scene.control.ToolBar?>
    <?import javafx.scene.image.Image?>
    <?import javafx.scene.image.ImageView?>
    <?import javafx.scene.layout.BorderPane?>
    
    <BorderPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.controller.TableViewController">
        <center>
            <TableView fx:id="tableView" prefHeight="500.0" prefWidth="1100.0" snapToPixel="false" styleClass="100">
            </TableView>
        </center>
        <top>
            <ToolBar maxHeight="35.0" prefHeight="35.0" prefWidth="1200" style="-fx-background-color: #8C0000;">
                <ImageView fitHeight="35.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
                    <Image url="@../images/qmit-search-toolbar-logo.png" />
                </ImageView>
                <TextArea fx:id="linkInput" maxHeight="30.0" maxWidth="250.0" />
                <TextArea fx:id="keywordInput" maxHeight="30.0" maxWidth="100.0" />
                <Button fx:id="buttonSearch" maxHeight="30.0" maxWidth="100.0" text="Search QMIT" />
            </ToolBar>
        </top>
    </BorderPane>
    

    The Oracle JavaFX Tutorial has a section on layout panes.