gpt4 book ai didi

JavaFX TitledPane 内容在折叠后消失

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:31 33 4
gpt4 key购买 nike

我有一个 TitledPane ,里面有 ListView ,当我折叠 titledPane 并展开它后,列表不再可见,但它仍然在某处。如果我调整 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>

Controller :

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

最佳答案

它是由您与 listview 一起使用的 static 修饰符引起的。静态变量可与 JavaFX 2.2 中的 FML 配合使用。它在 JavaFX 8 中不起作用。做成这样,

private ListView<String> userPisListView;

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

关于JavaFX TitledPane 内容在折叠后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27546389/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com