ScrollPane прокручивается сама по себе - PullRequest
0 голосов
/ 04 августа 2020

У меня есть экран, на котором компонент root - это ScrollPane. Однако когда диалоговое окно открыто, полоса прокрутки уже прокручена до конца. Я проверил, что getVvalue() вернул во время initialize(), и это было 0. Я пробовал использовать setVvalue() как для 0, так и 1 в initialize(), но ни один из них не работал.

Main. java:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(new ScrollableDialog(), 1024, 768);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

ScrollableDialog. java:

public class ScrollableDialog extends ScrollPane {
    public ScrollableDialog() {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/pkg/lib/fxml/scrollable-dialog.fxml"));

        loader.setRoot(this);
        loader.setController(this);

        try {
            loader.load();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    @FXML
    public void initialize() {
        System.out.println(getVvalue());
    }
}

scrollable-dialog.f xml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<fx:root hbarPolicy="NEVER" prefHeight="768.0" prefWidth="1024.0" type="ScrollPane" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
    <stylesheets>
        <URL value="@../css/new-fnrh-wizard.css" />
    </stylesheets>
    <content>
        <AnchorPane prefHeight="1585.0" prefWidth="1024.0" style="-fx-background-color: white;">
            <children>
                <Label layoutX="14.0" layoutY="14.0" text="Top feels so much better than the bottom" AnchorPane.leftAnchor="50.0" AnchorPane.topAnchor="35.0">
                    <font>
                        <Font name="System Bold" size="28.0" />
                    </font>
                </Label>
                <AnchorPane layoutX="50.0" layoutY="120.0" prefHeight="580.0" prefWidth="924.0" style="-fx-border-color: lightgrey;" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0">
                    <children>
                        <Label layoutX="14.0" layoutY="14.0" text="Hóspede" AnchorPane.leftAnchor="25.0" AnchorPane.topAnchor="20.0">
                            <font>
                                <Font name="System Bold" size="18.0" />
                            </font>
                        </Label>
                        <FormEntry fx:id="nameEntry" keyText="Name:" layoutX="26.0" layoutY="65.0" valueText="Ingmar Bergman" AnchorPane.leftAnchor="26.0" AnchorPane.topAnchor="65.0" />
                        <FormEntry fx:id="nationalityEntry" keyText="Nacionalidade:" layoutX="235.0" layoutY="66.0" valueText="Sweden" AnchorPane.topAnchor="65.0" />
                        <FormEntry fx:id="genderEntry" keyText="Gender:" layoutX="713.0" layoutY="65.0" valueText="Male" AnchorPane.leftAnchor="725.0" AnchorPane.rightAnchor="26.0" AnchorPane.topAnchor="65.0" />
                        <FormEntry fx:id="birthDateEntry" keyText="BirthDate:" layoutX="493.0" layoutY="67.0" valueText="14/06/1918" AnchorPane.topAnchor="65.0" />
                        <FormEntry fx:id="occupationEntry" keyText="Occupation:" layoutX="31.0" layoutY="141.0" valueText="Film-maker" AnchorPane.leftAnchor="26.0" AnchorPane.topAnchor="141.0" />
                        <FormEntry fx:id="cellphoneEntry" keyText="Cellphone:" layoutX="713.0" layoutY="149.0" valueText="12345678" AnchorPane.leftAnchor="725.0" AnchorPane.rightAnchor="26.0" AnchorPane.topAnchor="141.0" />
                        <FormEntry fx:id="telephoneEntry" keyText="Telephone:" layoutX="493.0" layoutY="148.0" valueText="87654321" AnchorPane.topAnchor="141.0" />
                        <FormEntry fx:id="emailEntry" keyText="Email:" layoutX="235.0" layoutY="141.0" valueText="ingberg@man.com" AnchorPane.leftAnchor="235.0" AnchorPane.topAnchor="141.0" />
                        <FormEntry fx:id="documentTypeEntry" keyText="Document Type:" layoutX="235.0" layoutY="225.0" valueText="Passport" AnchorPane.leftAnchor="235.0" AnchorPane.topAnchor="225.0" />
                        <FormEntry fx:id="documentNumberEntry" keyText="Document Number:" layoutX="493.0" layoutY="225.0" valueText="12345678910" AnchorPane.topAnchor="225.0" />
                        <FormEntry fx:id="dispatcherEntry" keyText="Dispatcher:" layoutX="715.0" layoutY="225.0" prefHeight="68.0" prefWidth="172.0" valueText="Federal Police of Sweden" AnchorPane.leftAnchor="725.0" AnchorPane.rightAnchor="26.0" AnchorPane.topAnchor="225.0" />
                        <FormEntry fx:id="cpfEntry" keyText="CPF:" layoutX="26.0" layoutY="225.0" valueText="123.456.789-10" AnchorPane.leftAnchor="26.0" AnchorPane.topAnchor="225.0" />
                        <AnchorPane layoutX="30.0" layoutY="302.0" prefHeight="253.0" prefWidth="867.0" style="-fx-border-color: lightgrey;" AnchorPane.bottomAnchor="22.0" AnchorPane.leftAnchor="29.0" AnchorPane.rightAnchor="28.0">
                            <children>
                                <Label layoutX="18.0" layoutY="15.0" text="Address" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="15.0">
                                    <font>
                                        <Font name="System Bold" size="16.0" />
                                    </font>
                                </Label>
                                <FormEntry fx:id="cepEntry" keyText="CEP:" layoutX="21.0" layoutY="62.0" valueText="49026030" AnchorPane.topAnchor="64.0" />
                                <FormEntry fx:id="streetEntry" keyText="Street:" layoutX="207.0" layoutY="66.0" prefHeight="69.0" prefWidth="215.0" valueText="Fighter" AnchorPane.topAnchor="64.0" />
                                <FormEntry fx:id="addressNumberEntry" keyText="Number:" layoutX="465.0" layoutY="65.0" valueText="1024" AnchorPane.topAnchor="64.0" />
                                <FormEntry fx:id="addressAdditionalInfoEntry" keyText="Additional Info:" layoutX="630.0" layoutY="67.0" prefHeight="147.0" prefWidth="227.0" valueText="By the black and white beach" AnchorPane.leftAnchor="623.0" AnchorPane.rightAnchor="9.0" AnchorPane.topAnchor="64.0" />
                                <FormEntry fx:id="addressCityEntry" keyText="City:" layoutX="21.0" layoutY="157.0" prefHeight="69.0" prefWidth="152.0" valueText="Zurich" AnchorPane.topAnchor="157.0" />
                                <FormEntry fx:id="addressStateEntry" keyText="State:" layoutX="207.0" layoutY="157.0" prefHeight="46.0" prefWidth="215.0" valueText="Of Calamity" AnchorPane.topAnchor="157.0" />
                                <FormEntry fx:id="addressCountryEntry" keyText="Country:" layoutX="465.0" layoutY="158.0" valueText="Sweden" AnchorPane.topAnchor="157.0" />
                            </children>
                        </AnchorPane>
                    </children>
                </AnchorPane>
                <AnchorPane layoutX="50.0" layoutY="726.0" prefHeight="463.0" prefWidth="924.0" style="-fx-border-color: lightgrey;" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0">
                    <children>
                        <Label layoutX="45.0" layoutY="9.0" text="Viagem" AnchorPane.leftAnchor="25.0" AnchorPane.topAnchor="20.0">
                            <font>
                                <Font name="System Bold" size="18.0" />
                            </font>
                        </Label>
                        <AnchorPane layoutX="27.0" layoutY="166.0" prefHeight="138.0" prefWidth="866.0" style="-fx-border-color: lightgrey;" AnchorPane.leftAnchor="27.0" AnchorPane.rightAnchor="27.0" AnchorPane.topAnchor="140.0">
                            <children>
                                <Label layoutX="19.0" layoutY="4.0" text="Última procedência" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="15.0">
                                    <font>
                                        <Font name="System Bold" size="16.0" />
                                    </font>
                                </Label>
                                <FormEntry fx:id="lastCityEntry" keyText="City:" layoutX="21.0" layoutY="57.0" prefHeight="71.0" prefWidth="251.0" valueText="São Miguel dos Milagres" AnchorPane.topAnchor="56.0" />
                                <FormEntry fx:id="lastStateEntry" keyText="State:" layoutX="296.0" layoutY="57.0" prefHeight="70.0" prefWidth="230.0" valueText="Rio Grande do Norte" AnchorPane.bottomAnchor="12.0" AnchorPane.leftAnchor="295.0" AnchorPane.rightAnchor="343.0" AnchorPane.topAnchor="56.0" />
                                <FormEntry fx:id="lastCountryEntry" keyText="Country:" layoutX="582.0" layoutY="58.0" prefHeight="77.0" prefWidth="239.0" valueText="Brasil" AnchorPane.bottomAnchor="4.0" AnchorPane.leftAnchor="581.0" AnchorPane.rightAnchor="48.0" AnchorPane.topAnchor="56.0" />
                            </children></AnchorPane>
                        <AnchorPane layoutX="28.0" layoutY="295.0" prefHeight="138.0" prefWidth="866.0" style="-fx-border-color: lightgrey;" AnchorPane.bottomAnchor="22.0" AnchorPane.leftAnchor="28.0" AnchorPane.rightAnchor="28.0">
                            <children>
                                <Label layoutX="19.0" layoutY="4.0" text="Próximo destino" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="15.0">
                                    <font>
                                        <Font name="System Bold" size="16.0" />
                                    </font>
                                </Label>
                                <FormEntry fx:id="nextCityEntry" keyText="City:" layoutX="23.0" layoutY="57.0" prefHeight="77.0" prefWidth="250.0" valueText="São Miguel dos Milagres" AnchorPane.bottomAnchor="6.0" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="594.0" AnchorPane.topAnchor="55.0" />
                                <FormEntry fx:id="nextStateEntry" keyText="State:" layoutX="294.0" layoutY="68.0" prefHeight="64.0" prefWidth="265.0" valueText="Rio Grande do Norte" AnchorPane.bottomAnchor="36.0" AnchorPane.leftAnchor="294.0" AnchorPane.rightAnchor="306.0" AnchorPane.topAnchor="55.0" />
                                <FormEntry fx:id="nextCountryEntry" keyText="Country:" layoutX="582.0" layoutY="59.0" prefHeight="71.0" prefWidth="230.0" valueText="Brasil" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="581.0" AnchorPane.rightAnchor="55.0" AnchorPane.topAnchor="55.0" />
                            </children></AnchorPane>
                        <FormEntry fx:id="vehicleEntry" keyText="Meio de transporte:" layoutX="28.0" layoutY="91.0" valueText="Ônibus" AnchorPane.leftAnchor="28.0" AnchorPane.topAnchor="75.0" />
                        <FormEntry fx:id="purposeEntry" keyText="Motivo:" layoutX="225.0" layoutY="92.0" valueText="Negócios" AnchorPane.topAnchor="75.0" />
                    </children>
                </AnchorPane>
                <AnchorPane layoutX="50.0" layoutY="1210.0" prefHeight="255.0" prefWidth="924.0" style="-fx-border-color: lightgrey;" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0">
                    <children>
                        <Label layoutX="14.0" layoutY="14.0" text="Dados Complementares" AnchorPane.leftAnchor="25.0" AnchorPane.topAnchor="20.0">
                            <font>
                                <Font name="System Bold" size="18.0" />
                            </font>
                        </Label>
                        <FormEntry fx:id="roomIdEntry" keyText="Room Id:" layoutX="27.0" layoutY="67.0" prefHeight="48.0" prefWidth="187.0" valueText="1001" AnchorPane.bottomAnchor="149.0" AnchorPane.leftAnchor="26.0" AnchorPane.rightAnchor="711.0" AnchorPane.topAnchor="65.0" />
                        <FormEntry fx:id="checkInEntry" keyText="Check in:" layoutX="348.0" layoutY="64.0" prefHeight="46.0" prefWidth="227.0" valueText="12/09/2020 às 14:50" AnchorPane.bottomAnchor="145.0" AnchorPane.leftAnchor="320.0" AnchorPane.rightAnchor="350.0" AnchorPane.topAnchor="65.0" />
                        <FormEntry fx:id="checkOutEntry" keyText="Check out:" layoutX="627.0" layoutY="64.0" valueText="14/09/2020 às 06:00" AnchorPane.bottomAnchor="144.0" AnchorPane.leftAnchor="627.0" AnchorPane.rightAnchor="36.0" AnchorPane.topAnchor="65.0" />
                        <FormEntry fx:id="invoiceIdEntry" keyText="Invoice id:" layoutX="26.0" layoutY="142.0" prefHeight="49.0" prefWidth="192.0" valueText="104025" AnchorPane.bottomAnchor="73.0" AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="707.0" AnchorPane.topAnchor="140.0" />
                        <FormEntry fx:id="invoiceValueEntry" keyText="Invoice value:" layoutX="349.0" layoutY="133.0" valueText="R$1040,24" AnchorPane.bottomAnchor="71.0" AnchorPane.leftAnchor="320.0" AnchorPane.rightAnchor="350.0" AnchorPane.topAnchor="140.0" />
                        <FormEntry fx:id="additionalInfoEntry" keyText="Additional info:" layoutX="633.0" layoutY="142.0" prefHeight="123.0" prefWidth="257.0" valueText="Thanks James you're the absolute best! Without you I would never make it!" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="627.0" AnchorPane.rightAnchor="35.0" AnchorPane.topAnchor="140.0" />
                    </children>
                </AnchorPane>
                <Button fx:id="printButton" layoutX="809.0" layoutY="1497.0" mnemonicParsing="false" prefHeight="48.0" prefWidth="165.0" style="-fx-font-size: 16px;" styleClass="primary-button" text="Print" />
                <Button fx:id="closeButton" layoutX="686.0" layoutY="1497.0" mnemonicParsing="false" prefHeight="48.0" prefWidth="111.0" style="-fx-font-size: 16px;" styleClass="secondary-button" text="Close" />
            </children>
        </AnchorPane>
    </content>
</fx:root>

Как видите, используется еще один пользовательский компонент, FormEntry. Вот оно:

FormEntry. java

public class FormEntry extends VBox {
    @FXML Label keyLabel;
    @FXML Label valueLabel;

    private String keyText;
    private String valueText;

    public FormEntry(@NamedArg("keyText") String keyText, @NamedArg("valueText") String valueText) {
        this.keyText = keyText;
        this.valueText = valueText;

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/pkg/lib/fxml/form-entry.fxml"));

        loader.setRoot(this);
        loader.setController(this);

        try {
            loader.load();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    @FXML
    public void initialize() {
        keyLabel.setText(keyText);
        valueLabel.setText(valueText);
    }

    public String getKeyText() {
        return keyText;
    }

    public void setKeyText(String keyText) {
        this.keyText = keyText;
        keyLabel.setText(keyText);
    }

    public String getValueText() {
        return valueText;
    }

    public void setValueText(String valueText) {
        if (valueText == null || valueText.isBlank() || valueText.isEmpty()) {
            this.valueText = "-";
        } else {
            this.valueText = valueText;
        }

        valueLabel.setText(this.valueText);
    }

    public void setValueText(int number) {
        this.valueText = String.valueOf(number);
    }
}

form-entry.f xml

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<fx:root spacing="10.0" type="VBox" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="keyLabel" text="Key:" textFill="#212121">
         <font>
            <Font name="System Bold" size="14.0" />
         </font>
      </Label>
      <Label fx:id="valueLabel" text="Value" textFill="#212121" wrapText="true">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
   </children>
</fx:root>

У вас есть представление о том, что может быть причиной этого?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...