2016-11-02 16 views
3

我需要在我的應用程序15個按鍵,但我不想通過一個聲明他們一個像使用JavaFX現場建設者,以填補按鈕FX ID陣列中的

@FXML 
private Button button1; 
@FXML 
private Button button2; 
@FXML 
private Button button3; 
... 
... 
... 
... 

相反,我想這一點,創建一個按鈕陣列。

@FXML 
private Button[][] button = new Button[5][3]; 

然而,在現場建設者,只有一個按鈕,我可以選擇,我認爲它參考陣列不是單一的按鈕元素。 有什麼方法可以輕鬆完成這項工作?

回答

1

我假設你只能通過播放fxml文件的代碼來修改它。

對於一個簡單的ArrayList可以執行以下(查fx:definefx:reference):

首先way-> 1

 <fx:define> 
      <!-- create buttons and store them in a list --> 
      <ArrayList fx:id="buttonsArray"> 
       <Button fx:id="firstButton" /> 
       <Button fx:id="secondButton" /> 
       <Button fx:id="thirdButton" /> 
      </ArrayList> 
     </fx:define> 


     <!-- add buttons in the list to scene graph --> 
     <fx:reference source="firstButton"/> 
     <fx:reference source="secondButton"/> 
     <fx:reference source="thirdButton"/> 

二way-> 2

 <!-- add buttons in the list to scene graph --> 
     <Button fx:id="firstButton" /> 
     <Button fx:id="secondButton" /> 
     <Button fx:id="thirdButton" /> 

     <fx:define> 
      <!--store them into a list --> 
      <ArrayList fx:id="buttonsArray"> 
       <fx:reference source="firstButton"/> 
       <fx:reference source="secondButton"/> 
       <fx:reference source="thirdButton"/> 
      </ArrayList> 
     </fx:define> 

關於一多維ArrayList中[3] [2]使用第一way-> 1它必須是這樣的:

 <fx:define> 
      <!-- create buttons and store them in a list --> 
      <ArrayList fx:id="array"> 
       <Button fx:id="Button_0_0" /> //first row , first column 
       <Button fx:id="Button_0_1" /> 
       <Button fx:id="Button_0_2" /> 
       <ArrayList fx:id="buttonsArray"> 
        <Button fx:id="Button_1_0" /> //second row , first column 
        <Button fx:id="Button_1_1" /> 
        <Button fx:id="Button_1_2" /> 
       </ArrayList> 
      </ArrayList> 
     </fx:define> 


     <!-- add buttons in the list to scene graph --> 
     <fx:reference source="Button_0_0" /> 
     <fx:reference source="Button_0_1" /> 
     <fx:reference source="Button_0_2" /> 
     <fx:reference source="Button_1_0" /> 
     <fx:reference source="Button_1_1" /> 
     <fx:reference source="Button_1_2" /> 

我建議你閱讀上面也給出了所有鏈接:

1)create array of Label using FXML in JavaFX

2)Grouping together JavaFX FXML Objects

+0

非常感謝!我完全理解它 –

3

請注意,可以將數據作爲GoXR3Plus的答案存儲在ArrayList中。但是你不能在fxml中創建一個數組。爲此,您需要從java代碼創建數組(例如,在initialize方法中)並將一些信息附加到允許您檢索索引信息的Node

這可以例如通過使用靜態輔助方法來附加信息,以及一個CSS類選擇的節點,並注入這些節點中的一個共同的祖先到控制器來完成:

<VBox fx:id="root" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.arraystore.StoreController"> 
    <children> 
     <Button text="1" Helper.indices="0 0"/> 
     <Button text="2" Helper.indices="0 1"/> 
     <Button text="3" Helper.indices="1 1"/> 
     <Button text="4" Helper.indices="1 0"/> 
     <Button text="5" Helper.indices="2 0"/> 
     <Button text="6" Helper.indices="2 1"/> 
    </children> 
</VBox> 

助手

public class Helper { 

    private static final String MARK_CLASS = "array-store-mark"; 
    private static final String MARK_CLASS_SELECTOR = "." + MARK_CLASS; 
    private static final String INDICES_KEY = "arrayStoreIndices"; 

    public static void setIndices(Node node, String indices) { 
     if (node == null || indices == null) { 
      throw new IllegalArgumentException(); 
     } 
     String[] parts = indices.split("\\s+"); 
     int[] is = new int[]{Integer.parseInt(parts[0]), Integer.parseInt(parts[1])}; 

     // add css class 
     node.getStyleClass().add(MARK_CLASS); 

     // add index data 
     node.getProperties().put(INDICES_KEY, is); 
    } 

    public static <T extends Node> T[][] getAsArray(Node parent, Class<T> nodeClass) { 
     if (parent == null) { 
      throw new IllegalArgumentException(); 
     } 
     int max1 = 0; 
     int max2 = 0; 

     // find nodes by class 
     Set<Node> marked = parent.lookupAll(MARK_CLASS_SELECTOR); 

     // find array size 
     for (Node n : marked) { 
      n.getStyleClass().remove(MARK_CLASS); 
      if (nodeClass.isAssignableFrom(n.getClass())) { 
       int[] is = (int[]) n.getProperties().get(INDICES_KEY); 
       if (max1 < is[0]) { 
        max1 = is[0]; 
       } 
       if (max2 < is[1]) { 
        max2 = is[1]; 
       } 
      } 
     } 

     T[][] result = (T[][]) Array.newInstance(nodeClass, max1+1, max2+1); 

     // put data in array 
     for (Node n : marked) { 
      int[] is = (int[]) n.getProperties().remove(INDICES_KEY); 

      if (nodeClass.isAssignableFrom(n.getClass())) { 
       result[is[0]][is[1]] = (T) n; 
      } 
     } 

     return result; 
    } 

} 

控制器類別

public class StoreController { 

    // common parent 
    @FXML 
    private Parent root; 

    private Button[][] buttons; 

    @FXML 
    private void initialize() { 
     buttons = Helper.getAsArray(root, Button.class); 
     System.out.println(Arrays.deepToString(buttons)); 
    } 

} 
+0

非常感謝你!這就像下一個級別的解決方案,我可能沒有得到它。我會很努力地學習你的代碼! –