I generated the following FXML in Scene Builder. When I preview it in Scene Builder and resize the window the inner split view maintains the spacing between itself and the anchor view. However when I run the FXML in my application it does not. Any ideas? I am using javafx 2.2.51.

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

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

<AnchorPane prefHeight="477.0" prefWidth="515.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.nm.haze.LibraryViewController">
  <children>
    <SplitPane dividerPositions="0.09484536082474226" focusTraversable="true" prefHeight="415.0" prefWidth="487.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="48.0">
      <items>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
          <children>
            <ListView prefHeight="371.0" prefWidth="425.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
          </children>
        </AnchorPane>
      </items>
    </SplitPane>
  </children>
</AnchorPane>

And my controller code:

package com.nm.haze;

import java.net.URL;
import java.util.ResourceBundle;

public class LibraryViewController extends BaseController {

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }
}

Main code

private static final String LIBRARY_SELECT = "LibrarySelect";
private static final String LIBRARY_VIEW = "LibraryView";

@Override
public void start(Stage primaryStage) throws IOException {
    SceneController sceneController = new SceneController(primaryStage, new SettingsManager());
    sceneController.loadScene(LIBRARY_SELECT);
    sceneController.loadScene(LIBRARY_VIEW);
    if (sceneController.setupNeeded()) {
        sceneController.setScreen(LIBRARY_SELECT);
    } else {
        sceneController.setScreen(LIBRARY_VIEW);
    }
    Group root = new Group();
    root.getChildren().addAll(sceneController);
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

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

SceneController code:

public class SceneController extends StackPane {

Map<String, Node> scenes = new HashMap<>();
Stage stage;
SettingsManager settingsManager;

public SceneController(Stage stage, SettingsManager settingsManager) {
    this.stage = stage;
    this.settingsManager = settingsManager;
}

public void addScene(String name, Node scene) {
    scenes.put(name, scene);
}

public void loadScene(String name) throws IOException {
    FXMLLoader myLoader = new FXMLLoader(getClass().getResource(name + ".fxml"));
    Parent loadScreen = (Parent)myLoader.load();
    ((BaseController)myLoader.getController()).setSceneController(this);
    addScene(name, loadScreen);
}

public void setScreen(final String name) {
    List<Node> children = getChildren();
    Node scene = scenes.get(name);
    if(scene != null) {
        if (children.size() > 0) {
            children.remove(0);
        }
        children.add(0, scene);

    } else {
        throw new IllegalArgumentException("Scene has not been loaded");
    }
}

public Stage getStage() {
    return stage;
}

public boolean setupNeeded() {
    return settingsManager.setupNeeded();
}

}

Just to be clear about what the issue is, please see the before and after screenshots below. The distance between the ListView and the AnchorPane should stay the same (and does in Scene Builder).

enter image description here

enter image description here

有帮助吗?

解决方案

`You just have to fix the minimum and maximum width of the part of the SplitPane, whose width you dont want to change on resizing your window !

Here I have fixed the left part, by setting the minWidth and maxWidth of AnchorPane on the left side. You can do the same for the right side as well, depending on your requirement.

abcFxml.fxml

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

<AnchorPane prefHeight="477.0" prefWidth="515.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="bounty.LibraryViewController">
  <children>
    <SplitPane dividerPositions="0.09484536082474226" focusTraversable="true" prefHeight="415.0" prefWidth="487.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="48.0">
      <items>
        <AnchorPane minHeight="0.0" minWidth="100.0" prefHeight="160.0" prefWidth="100.0" maxHeight="160.0" maxWidth="100.0" />
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
          <children>
            <ListView prefHeight="371.0" prefWidth="425.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
          </children>
        </AnchorPane>
      </items>
    </SplitPane>
  </children>
</AnchorPane>

Controller

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class LibraryViewController extends Application
{

    @Override
    public void start(Stage arg0) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("abcFxml.fxml"));
        AnchorPane pane = (AnchorPane) fxmlLoader.load();
        Scene scene = new Scene(pane);
        arg0.setScene(scene);
        arg0.show();

    }

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

}

Screenshots

Size1

enter image description here

Size 2

enter image description here

EDIT : As per user updation

The problem you are facing is because of the Group you are using. Group itself is not resizable ! Try to put the StackPane directly to your scene(like I did) or you can use any other container like VBOX / HBOX etc

@Override
public void start(Stage primaryStage) throws IOException {
    SceneController sceneController = new SceneController(primaryStage, new SettingsManager());
    sceneController.loadScene(LIBRARY_SELECT);
    sceneController.loadScene(LIBRARY_VIEW);
    if (sceneController.setupNeeded()) {
        sceneController.setScreen(LIBRARY_SELECT);
    } else {
        sceneController.setScreen(LIBRARY_VIEW);
    }
    primaryStage.setScene(new Scene(sceneController));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top