坍塌防止JavaFX的手风琴(Prevent an accordion in JavaFX from

2019-07-29 05:31发布

有没有预防的JavaFX 2.1的手风琴从完全塌陷的一种简单的方法? 我有几个条目手风琴,但如果用户点击激活手风琴的加入,因此崩溃手风琴。

我大概可以用鼠标点击监听,检查做了检查,并采取相应的行动,但这种感觉就像它应该是更简单的比来完成。

Answer 1:

一个监听器添加到当前扩大手风琴窗格,防止它被倒塌用户通过修改它的可折叠特性。

下面是一个示例应用程序:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class AccordionSample extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage primaryStage) {
    // create some titled panes to go in an accordion.
    TitledPane adminPane = new TitledPane("Animals", 
      VBoxBuilder.create().style("-fx-padding: 10").spacing(10).children(
        ButtonBuilder.create().text("Zebra").maxWidth(Double.MAX_VALUE).build(),
        ButtonBuilder.create().text("Shrew").maxWidth(Double.MAX_VALUE).build()
      ).build()            
    );
    TitledPane viewPane = new TitledPane("Vegetables", 
      VBoxBuilder.create().style("-fx-padding: 10").spacing(10).children(
        ButtonBuilder.create().text("Eggplant").maxWidth(Double.MAX_VALUE).build(),
        ButtonBuilder.create().text("Carrot").maxWidth(Double.MAX_VALUE).build()
      ).build()            
    );

    // create an accordion, ensuring the currently expanded pane can not be clicked on to collapse.
    Accordion accordion = new Accordion();
    accordion.getPanes().addAll(adminPane, viewPane);
    accordion.expandedPaneProperty().addListener(new ChangeListener<TitledPane>() {
      @Override public void changed(ObservableValue<? extends TitledPane> property, final TitledPane oldPane, final TitledPane newPane) {
        if (oldPane != null) oldPane.setCollapsible(true);
        if (newPane != null) Platform.runLater(new Runnable() { @Override public void run() { 
          newPane.setCollapsible(false); 
        }});
      }
    });
    for (TitledPane pane: accordion.getPanes()) pane.setAnimated(false);
    accordion.setExpandedPane(accordion.getPanes().get(0));

    // layout the scene.
    StackPane layout = new StackPane();
    layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
    layout.getChildren().add(accordion);
    primaryStage.setScene(new Scene(layout));
    primaryStage.show();
  }
}


Answer 2:

下面是确保手风琴将永远不会完全崩溃另一种解决方案。 从@jewelsea伟大的原来的答案不同的是小 - 我不喜欢的事实,默认下向箭头从开放手风琴TitledPane脸上消失,因为它的“可折叠”属性被设置为false。 我用它更多的发挥,实现更“自然”的感觉对我的界面。

    /* Make sure the accordion can never be completely collapsed */
    accordeon.expandedPaneProperty().addListener((ObservableValue<? extends TitledPane> observable, TitledPane oldPane, TitledPane newPane) -> {
        Boolean expand = true; // This value will change to false if there's (at least) one pane that is in "expanded" state, so we don't have to expand anything manually
        for(TitledPane pane: accordeon.getPanes()) {
            if(pane.isExpanded()) {
                expand = false;
            }
        }
        /* Here we already know whether we need to expand the old pane again */
        if((expand == true) && (oldPane != null)) {
            Platform.runLater(() -> {
                accordeon.setExpandedPane(oldPane);
            });
        }
    });


文章来源: Prevent an accordion in JavaFX from collapsing