JavaFX TitledPane 需要多次点击才能展开,场景不会更新

JavaFX TitledPane requires multiple clicks to expand, scene doesn't update

我 运行 遇到了一个奇怪的问题 issue/bug,其中 TitledPane 在第一次单击时不会展开,需要多次单击才能展开。除此之外,嵌套的 TitledPanes 根本不会展开,除非您多次单击它们并在选项卡之间切换。

所以代码可以正常工作,但不正确。

如果专家可以判断这是一个错误还是我做错了什么,那将是一个很大的帮助。谢谢!

package subtables;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Subtables extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        Accordion accordion = new Accordion();

        TitledPane pane = new TitledPane();
        VBox root = new VBox();

        HBox titles = new HBox();
        titles.setPadding(new Insets(5, 5, 5, 24));
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());

        FlowPane graphic = new FlowPane();
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());

        pane.setGraphic(graphic);

        pane.setOnMouseClicked((e) ->
        {
            TabPane tabPane = new TabPane();
            Tab tab1 = new Tab("Sub Table 1", getSubWindow());
            Tab tab2 = new Tab("Sub Table 2", new Label("Text inside a tab."));
            Tab tab3 = new Tab("Sub Table 3", new Label("Text inside a tab."));
            Tab tab4 = new Tab("Sub Table 4", new Label("Text inside a tab."));

            tabPane.getTabs().add(tab1);
            tabPane.getTabs().add(tab2);
            tabPane.getTabs().add(tab3);
            tabPane.getTabs().add(tab4);

            VBox vBox = new VBox(tabPane);
            pane.setContent(vBox);
        });

        accordion.getPanes().add(pane);
        root.getChildren().add(titles);
        root.getChildren().add(accordion);

        primaryStage.setTitle("Hello World!");
        Scene scene = new Scene(root, 640, 480);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public TextField getTextField()
    {
        TextField textField = new TextField();
        textField.setPromptText("Value");
        textField.setPrefWidth(50);
        return textField;
    }

    public VBox getSubWindow()
    {
        Accordion accordion = new Accordion();

        TitledPane pane = new TitledPane();
        VBox root = new VBox();

        HBox titles = new HBox();
        titles.setPadding(new Insets(5, 5, 5, 24));
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());
        titles.getChildren().add(getTextField());

        FlowPane graphic = new FlowPane();
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());
        graphic.getChildren().add(getTextField());

        pane.setGraphic(graphic);
        pane.setOnMouseClicked((e) ->
        {
            TabPane tabPane = new TabPane();
            Tab tab1 = new Tab("Sub Table 1", getSubWindow());
            Tab tab2 = new Tab("Sub Table 2", new Label("Text inside a tab."));
            Tab tab3 = new Tab("Sub Table 3", new Label("Text inside a tab."));
            Tab tab4 = new Tab("Sub Table 4", new Label("Text inside a tab."));

            tabPane.getTabs().add(tab1);
            tabPane.getTabs().add(tab2);
            tabPane.getTabs().add(tab3);
            tabPane.getTabs().add(tab4);

            VBox vBox = new VBox(tabPane);
            pane.setContent(vBox);
        });

        accordion.getPanes().add(pane);
        root.getChildren().add(titles);
        root.getChildren().add(accordion);

        return root;
    }

    /**
     * Main function to launch the application.
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

找到解决方法!

  1. setOnMouseClicked() 更改为 setOnMousePressed() 以修复展开所需的多次点击。
  2. 添加 VBox.setVgrow(tabPane, Priority.ALWAYS);VBox.setVgrow(accordion, Priority.ALWAYS); 以便 TitledPane 无需在选项卡之间切换即可刷新。

setConent() 方法调用了 onMouseClicked。首先单击 TitledPane header 将其展开,然后处理鼠标单击。鼠标点击的核心功能是设置内容。它还没有渲染。第二次单击折叠 TitledPane。第三次单击展开 TitledPane,其内容现在可见。 将创建 VBox 选项卡移出 setOnMouseClicked() 方法。

正确代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TitledPane;
import javafx.stage.Stage;

public class TitledPaneApp extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        TabPane subTabPane = new TabPane(new Tab("Sub tab1", new Label("Sub content1")), new Tab("Sub tab2", new Label("Sub content2")));
        TitledPane subTitledPane = new TitledPane("Sub titled pane", subTabPane);
        Accordion subAccordion = new Accordion(subTitledPane);

        Tab tab1 = new Tab("Tab1", subAccordion);
        Tab tab2 = new Tab("Tab2", new Label("Content2"));
        TabPane tabPane = new TabPane(tab1, tab2);
        TitledPane titledPane = new TitledPane("Titled pane", tabPane);
        Accordion accordion = new Accordion(titledPane);

        Scene scene = new Scene(accordion, 400, 400);
        stage.setScene(scene);
        stage.show();
    }
}