2016-09-27 59 views
1

我有一個應用程序,用戶應該只能選擇一個按鈕。JavaFX - 無法將ToggleButtons轉換爲ToggleGroup

我的問題是,我無法將ToggleButton s變成ToggleGroup,這應該可以解決我的問題。使用if語句創建ToggleButton s。我將代碼縮減爲重要部分。 有什麼幫助嗎?

我的代碼:

public SymbolTableCell() { 
} 

@FXML 
public void initialize() { 
    collectFonts(); 
    buildGridPaneAddButtons(); 
    listView.setOnMouseClicked(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent t) { 
      buildGridPaneAddButtons(); 
@FXML 
    public void buildGridPaneAddButtons() { 
     gridPane.getChildren().clear(); 
     int numRows = 14; 
     int numColumns = 14; 
     for (int row = 0; row < numRows; row++) { 
      RowConstraints rc = new RowConstraints(); 
      rc.setFillHeight(true); 
      rc.setVgrow(Priority.ALWAYS); 
      gridPane.getRowConstraints().add(rc); 
     } 
     for (int col = 0; col < numColumns; col++) { 
      ColumnConstraints cc = new ColumnConstraints(); 
      cc.setFillWidth(true); 
      cc.setHgrow(Priority.ALWAYS); 
      gridPane.getColumnConstraints().add(cc); 
     } 

     String selectedFamily = listView.getSelectionModel().getSelectedItem(); 
     if (selectedFamily != null) { 
      System.out.println("selected Font: '" + selectedFamily + "'"); 
      for (int i = 0; i < 196; i++) { 
       Font selectedFont = Font.font(selectedFamily, 18.0); 
       Character character = new Character((char) (i)); 
       java.awt.Font awtFont = new java.awt.Font(selectedFamily, 0, 40); 

       if (awtFont.canDisplay(character.charValue()) { 
        ToggleButton buttonForSymbols = createButton("" + character); 
        buttonForSymbols.setFont(selectedFont); 
        buttonForSymbols.setAlignment(Pos.CENTER); 

// Here I try to get the ToggleButtons into the Group. 
        ToggleGroup groupForToggleButtons = new ToggleGroup(); 
        buttonForSymbols.setToggleGroup(groupForToggleButtons); 

        HBox symbolHBox = new HBox(); 
        symbolHBox.getChildren().add(buttonForSymbols); 

        Label symbolLabel = new Label(); 
        symbolLabel.setText("" + i); 

        Separator symbolSeparator = new Separator(); 

        BorderPane symbolBorderPane = new BorderPane(); 
        symbolBorderPane.setTop(symbolHBox); 
        symbolBorderPane.setCenter(symbolSeparator); 
        symbolBorderPane.setBottom(symbolLabel); 
        symbolLabel.setAlignment(Pos.CENTER); 
        symbolBorderPane.setAlignment(symbolLabel, Pos.CENTER); 

        // when ToggleButton selected, set new Style of BorderPane 
        symbolBorderPane.styleProperty() 
          .bind(Bindings.when(buttonForSymbols.selectedProperty()) 
            .then("-fx-border-color: red; -fx-border-width: 1.5;") 
            .otherwise("-fx-border-color: black; -fx-border-width: 1;"));      
        gridPane.add(symbolBorderPane, i % numRows, i/numColumns); 

       } 
      } 
     } 
    } 

    // ToggleButton to use .then .otherwise style properties 
    private ToggleButton createButton(String text) { 
     ToggleButton buttonPanelForSymbols = new ToggleButton(text); 
     buttonPanelForSymbols.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
     buttonPanelForSymbols.setOnAction(e -> System.out.println("ok")); 
     buttonPanelForSymbols.setStyle(
       "-fx-border-color: transparent; -fx-border-width: 0; -fx-background-radius: 0; -fx-background-color: transparent;"); 
     return buttonPanelForSymbols; 
    } 

回答

3

在此行中:

ToggleGroup groupForToggleButtons = new ToggleGroup(); 

您爲每個ToggleButton創建一個新的ToggleGroup爲你生成的按鈕for loop內部執行此命令。

創建ToggleGroup外循環的:

ToggleGroup groupForToggleButtons = new ToggleGroup(); 
for (int i = 0; i < 196; i++) { 
    ... 
} 

然後在循環內使用此ToggleGroup實例爲每個ToggleButton(刪除循環內的ToggleGroup初始化)。

注:你甚至可以封裝這個在createButton方法:

private ToggleButton createButton(String text, ToggleGroup group) { 
    ToggleButton buttonPanelForSymbols = new ToggleButton(text); 
    // Current code here 
    buttonPanelForSymbols.setToggleGroup(group) 
    return buttonPanelForSymbols; 
} 

然後在循環:

ToggleButton buttonForSymbols = createButton("" + character, groupForToggleButtons); 

,同時消除每一個ToggleGroup從循環有關。

+0

非常感謝你! – Blabsl