2016-09-26 27 views
0

我在寫一個小配置嚮導樣式的JavaFX應用程序。我想強制用戶通過配置窗格的不同步驟。所以我選擇了一個有幾塊面板的手風琴箱。用戶只能進入下一個或上一個手風琴面板,但不能從面板1跳到3.帶有JavaFX Accordions的嚮導

我通過添加手風琴下的「上一個」和「下一步」按鈕來實現此目的,並且設置了鼠標手風琴的透明度爲真。到目前爲止一切正常。用戶可以從面板1走到5並返回...

不幸的是(由於任何奇怪的原因)用戶不能在不同的面板內配置任何東西。等等...我將鼠標透明度設置爲「真」,正確...但是當我將它設置爲false時,用戶可以在沒有任何順序的情況下跳過面板。

有什麼建議嗎?

+0

您是否曾嘗試將每個'TitledPane'上的'disable'設置爲'true',直到它們被允許在那裏導航? –

回答

2

一種可能的方法是將所有窗格的collapsible屬性設置爲false,並在每個窗格需要展開或摺疊(可以通過編程方式執行)時將其設置爲true。這是一個有點難看,但這裏有一個例子:

import javafx.application.Application; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Accordion; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.TitledPane; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 

public class AccordionBasedWizard extends Application { 

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

     for (int i = 0 ; i < 5 ; i++) { 
      TitledPane pane = new TitledPane("Page "+(i+1), new Label("Wizard page "+(i+1))); 
      accordion.getPanes().add(pane); 
      pane.setCollapsible(false); 
     } 


     accordion.setExpandedPane(accordion.getPanes().get(0)); 

     Button previousButton = new Button("Previous"); 
     previousButton.disableProperty().bind(accordion.expandedPaneProperty().isEqualTo(accordion.getPanes().get(0))); 
     previousButton.setOnAction(e -> { 
      TitledPane current = accordion.getExpandedPane(); 
      int index = accordion.getPanes().indexOf(current); 
      TitledPane previous = accordion.getPanes().get(index - 1); 
      current.setCollapsible(true); 
      previous.setCollapsible(true); 
      accordion.setExpandedPane(previous); 
      previous.setCollapsible(false); 
      current.setCollapsible(false); 
     }); 

     Button nextButton = new Button("Next"); 
     nextButton.disableProperty().bind(accordion.expandedPaneProperty().isEqualTo(accordion.getPanes().get(accordion.getPanes().size()-1))); 
     nextButton.setOnAction(e -> { 
      TitledPane current = accordion.getExpandedPane(); 
      int index = accordion.getPanes().indexOf(current); 
      TitledPane next = accordion.getPanes().get(index + 1); 
      current.setCollapsible(true); 
      next.setCollapsible(true); 
      accordion.setExpandedPane(next); 
      next.setCollapsible(false); 
      current.setCollapsible(false); 
     }); 

     HBox buttons = new HBox(5, previousButton, nextButton); 
     buttons.setAlignment(Pos.CENTER); 
     buttons.setPadding(new Insets(5)); 

     BorderPane root = new BorderPane(accordion); 
     root.setBottom(buttons); 

     primaryStage.setScene(new Scene(root, 400, 400)); 
     primaryStage.show(); 
    } 


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

對於它的價值,我可能不會嘗試使用Accordion作爲嚮導,將只使用一個BorderPane,與當前的「嚮導頁「在中心,底部的下一個和上一個按鈕。如果需要,您也可以添加麪包屑路徑。

+0

10分的10分!感謝它的工作。甚至你的建議最後都是正確的。但它不適合我的應用程序(我有6個「主要」頁面,每個頁面有一個4-5個子窗格的手風琴)。我真的使用你的「解決方法」,因爲我喜歡「手風琴」的所有即用型效果。 – Sauer