崩溃后,JavaFX TitledPane内容消失

时间:2014-12-18 12:14:50

标签: java javafx

我内部有一个TitledPane ListView,在我崩溃titledPane并将其展开后,列表不再可见,但它仍然在某处。如果我调整titledPane(通过调整应用程序窗口大小),它会重新出现。我需要立即重新出现该列表,并且最终这个名为“专栏”的内容将具有固定大小而没有缩放/增长,因此即使调整大小也不起作用。

如何在扩展titledPane之后立即重新显示列表?

FXML:

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

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

<AnchorPane maxHeight="560.0" maxWidth="1000.0" xmlns="http://javafx.com/javafx/8"       xmlns:fx="http://javafx.com/fxml/1"     fx:controller="com.humandevice.drive.fx.controller.MainWindowController">
<children>
  <GridPane layoutX="-159.0" layoutY="-131.0" prefHeight="560.0" prefWidth="1000.0"     AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"     AnchorPane.topAnchor="0.0">
    <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="201.0"     prefWidth="201.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="3.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      </rowConstraints>
       <children>
          <HBox alignment="CENTER" spacing="50.0" style="-fx-background-color: GREY;"   GridPane.columnIndex="2">
            <children>
               <Button fx:id="goBack" disable="true" mnemonicParsing="false"   onAction="#previousScreen" text="%buttons.back" />
              <Button mnemonicParsing="false" onAction="#openSettingsWindow"   text="%buttons.settings" />
              <Button fx:id="setPublicScreen" mnemonicParsing="false" text="Public" />
              <Button fx:id="setWelcomeScreen" mnemonicParsing="false" text="Welcome" />
              <Button fx:id="logoutButton" mnemonicParsing="false" onAction="#logout"   text="Logout" />
           </children>
           <padding>
              <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
           </padding>
        </HBox>
        <GridPane fx:id="contentPane" GridPane.columnIndex="2" GridPane.rowIndex="1">
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
        <Separator orientation="VERTICAL" prefHeight="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.rowSpan="2147483647" />
        <VBox alignment="TOP_CENTER" GridPane.rowSpan="2147483647">
           <children>
              <TitledPane animated="false" text="%labels.mypis">
                <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                       <children>
                          <ListView fx:id="userPisListView" layoutY="-47.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                       </children>
                    </AnchorPane>
                </content>
                 <VBox.margin>
                    <Insets />
                 </VBox.margin>
              </TitledPane>
              <TitledPane animated="false" text="placeholder">
                <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                </content>
                 <VBox.margin>
                    <Insets />
                 </VBox.margin>
              </TitledPane>
           </children>
        </VBox>
       </children>
    </GridPane>
  </children>
</AnchorPane>

控制器:

package com.humandevice.drive.fx.controller;

//imports

public class MainWindowController implements Initializable {

@FXML
public static GridPane contentPane, previousScreen = Main.welcomeScreen;
@FXML
public static Button goBack, setWelcomeScreen, setPublicScreen,
        logoutButton;
@FXML
ListView<String> userPisListView;

@Override
public void initialize(URL url, ResourceBundle res) {
    listUserPis();
    FXMLLoader loader = new FXMLLoader();
    GridPane content = new GridPane();
    if (Main.lang.equals("pl"))
        loader.setResources(ResourceBundle.getBundle(
                "com.humandevice.drive.fx.bundles.messages", new Locale(
                        "pl", "PL")));
    else
        loader.setResources(ResourceBundle.getBundle(
                "com.humandevice.drive.fx.bundles.messages", new Locale(
                        "en", "EN")));
    try {
        content = (GridPane) loader.load(Main.class.getResource(
                "/com/humandevice/drive/fx/view/WelcomeView.fxml")
                .openStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    content.setAlignment(Pos.TOP_RIGHT);
    contentPane.add(content, 0, 0);
    setWelcomeScreen.addEventHandler(ActionEvent.ACTION,
            new ContentChangeHandler());
    setPublicScreen.addEventHandler(ActionEvent.ACTION,
            new ContentChangeHandler());

    userPisListView.getSelectionModel().selectedItemProperty()
            .addListener(new ChangeListener<String>() {
                @Override
                public void changed(
                        ObservableValue<? extends String> observable,
                        String oldValue, String newValue) {
                    updateActivePi();
                    changeActivePiScreen();
                }
            });
}

/**
 * TODO Zaladowanie aktywnego Pi na MyPiView
 */
protected void changeActivePiScreen() {
}

/**
 * TODO zmiana aktywnego PI w Main na podstawie nazwy z userPis
 */
protected void updateActivePi() {

}

public void listUserPis() {
    ObservableList<String> piNames = FXCollections.observableArrayList();
    for (PI pi : Main.userPis) {
        piNames.add(pi.getDeviceName());
    }
    userPisListView.setItems(piNames);
}

@FXML
public static void openSettingsWindow() {
    Stage settingsStage = new Stage();
    settingsStage.setTitle("Settings");
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(Main.class
            .getResource("/com/humandevice/drive/fx/view/SettingsView.fxml"));
    if (Main.lang.equals("pl"))
        loader.setResources(ResourceBundle.getBundle(
                "com.humandevice.drive.fx.bundles.messages", new Locale(
                        "pl", "PL")));
    else
        loader.setResources(ResourceBundle.getBundle(
                "com.humandevice.drive.fx.bundles.messages", new Locale(
                        "en", "EN")));
    GridPane root = new GridPane();
    try {
        root = (GridPane) loader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Scene settingsScene = new Scene(root);
    settingsStage.setScene(settingsScene);
    settingsStage.show();
}

@FXML
public static void previousScreen() {
    System.out.println("Wracam na poprzedni ekran");
    MainWindowController.contentPane.getChildren().set(0, previousScreen);
    goBack.setDisable(true);
}

@FXML
public void logout() {
    try {
        Main.logout();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

结构:

enter image description here

这就是它的样子。不工作的listview位于左上角。

enter image description here

1 个答案:

答案 0 :(得分:0)

它由您使用listview的static修饰符引起。 静态变量适用于JavaFX 2.2中的FML。它在JavaFX 8中不起作用。 像这样,

private ListView<String> userPisListView;

同时从方法中删除静态以避免兼容性问题。