2016-12-12 28 views
0

我想爲使用JavaFX的程序設置一個簡單示例。 我想要的是一個帶有main.fxml的控制器;那麼main.fxml將有一個TabbedPane作爲根,帶有2個選項卡(tab1.fxml和tab2.fxml),每個選項卡都有其控制器(Tab1Controller和Tab2Controller)。使用@FXML註解不能訪問超類控制器中的元素

現在,這可能是prolem,但我不明白爲什麼這會是一個問題:

Tab1Controller和Tab2Controller都擴展控制器;因爲它們共享被操縱的各種字段,例如需要不斷更新的底部狀態欄。

到目前爲止,我設法讓所有的控制器和.fxml工作。

當我嘗試從其中一個子類設置控制器中的標籤文本時,它只是指向null,但標籤在其默認文本中被初始化爲gui。 正確編譯並且lblController似乎正確地鏈接到main.fxml中的fx:id。

任何幫助/鏈接非常感謝。

我一直在尋找不同的職位,但只有一個越來越接近我需要它這一個: JavaFX 8, controllers inheritance

主營:

package sample; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("../view/main.fxml")); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 600, 700)); 
     primaryStage.show(); 
    } 
} 

主控制器:

package sample; 

import javafx.fxml.FXML; 
import javafx.scene.control.Label; 

public class Controller { 
    @FXML 
    public Label lblController; 

    public Controller() { 
     System.out.println("CONTROLLER MAIN - CONSTRUCTOR"); 
    } 

    @FXML 
    public void initialize() { 
     System.out.println("CONTROLLER MAIN - INITIALIZER"); 
    } 

    protected void setSts(String sts) { 
     lblController.setText(sts); 
    } 
} 

main.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.Tab?> 
<?import javafx.scene.control.TabPane?> 
<?import javafx.scene.layout.*?> 
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" 
      xmlns="http://javafx.com/javafx/8.0.60" 
      fx:controller="sample.Controller"> 
    <BorderPane> 
     <center> 
      <TabPane tabClosingPolicy="UNAVAILABLE"> 
       <Tab text="Tab 1"> 
        <AnchorPane> 
         <!--FOR fx:id HAS TO HAVE THE SAME NAME BUT IN LOWER CASE AS THE .fxml--> 
         <fx:include fx:id="tab1" source="tab/tab1.fxml"/> 
        </AnchorPane> 
       </Tab> 
       <Tab text="Tab 2"> 
        <AnchorPane> 
         <fx:include fx:id="tab2" source="tab/tab2.fxml"/> 
        </AnchorPane> 
       </Tab> 
      </TabPane> 
     </center> 
     <bottom> 
      <AnchorPane> 
       <Label fx:id="lblController" text="Controller Test"/> 
      </AnchorPane> 
     </bottom> 
    </BorderPane> 
</AnchorPane> 

Tab1Controller

package sample.ctrTab; 

import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import sample.Controller; 


public class Tab1Controller extends Controller { 
    @FXML 
    Label lbl1; 
    @FXML 
    TextField txt1; 
    @FXML 
    Button btn1Save, btn1Load; 

    public Tab1Controller() { 
     super(); 
     System.out.println("CONTROLLER TAB 1 - CONSTRUCTOR"); 
    } 

    @FXML 
    void btn1Save() { 
     System.out.println("CONTROLLER TAB 1 - SAVE CLICK"); 
//  lblController.setText("ANYTHING"); //NULL POINTER HERE 
//  setSts(txt1.getText()); //OR HERE 
    } 

    @FXML 
    void btn1Load() { 
     System.out.println("CONTROLLER TAB 1 - LOAD CLICK"); 
    } 

    protected void setSts(String sts) { 
     super.setSts(sts); 
    } 
} 

tab1.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.AnchorPane?> 
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" 
      xmlns="http://javafx.com/javafx/8.0.60" fx:controller="sample.ctrTab.Tab1Controller"> 
    <children> 
     <Label fx:id="lbl1" layoutX="100" layoutY="50" text="Def tab 1"/> 
     <TextField fx:id="txt1" layoutX="100" layoutY="70"/> 
     <Button fx:id="btn1Save" onAction="#btn1Save" layoutX="100" layoutY="140" mnemonicParsing="false" 
       text="Save text"/> 
     <Button fx:id="btn1Load" onAction="#btn1Load" layoutX="200.0" layoutY="140" mnemonicParsing="false" 
       text="Send to tab 2"/> 
    </children> 
</AnchorPane> 

回答

2

的字段可以存在於控制器類,然而仍然FXMLLoader創造了新的控制器實例對於每個加載的FXML給出這些fxmls。由於tab1.fxml不包含帶fx:id="lblController"作爲屬性的Label標記,因此lblController字段永遠不會被注入到Tab1Controller實例中。

不延長Controller,但通過它的一個參考子控制器你會好起來的:

public class Controller { 
    @FXML 
    public Label lblController; 
    @FXML 
    private Tab1Controller tab1Controller; 
    @FXML 
    private Tab2Controller tab2Controller; 

    public Controller() { 
     System.out.println("CONTROLLER MAIN - CONSTRUCTOR"); 
    } 

    @FXML 
    public void initialize() { 
     System.out.println("CONTROLLER MAIN - INITIALIZER"); 
     tab1Controller.initParentController(this); 
     tab2Controller.initParentController(this); 
    } 

    public void setSts(String sts) { 
     lblController.setText(sts); 
    } 
} 
public class Tab1Controller { 
    @FXML 
    Label lbl1; 
    @FXML 
    TextField txt1; 
    @FXML 
    Button btn1Save, btn1Load; 

    public Tab1Controller() { 
     System.out.println("CONTROLLER TAB 1 - CONSTRUCTOR"); 
    } 

    private Controller parentController; 

    public void initParentController(Controller controller) { 
     parentController = controller; 
    } 

    @FXML 
    void btn1Save() { 
     System.out.println("CONTROLLER TAB 1 - SAVE CLICK"); 
     parentController.lblController.setText("ANYTHING"); 
     parentController.setSts(txt1.getText()); 
    } 

    @FXML 
    void btn1Load() { 
     System.out.println("CONTROLLER TAB 1 - LOAD CLICK"); 
    } 

} 

注意對孩子的控制器,你可以實際使用的一個抽象超實現initParentController方法只有一次。

+0

我已經看到這種方法,我試圖避免每個子控制器的inititialize()單獨;這可以設置主控制器。即使用分離的getter/setter進行訪問,我仍然不明白繼承是如何在這裏不起作用的。 –

+1

@ 4673_j加載'main.fxml'時,您只需獲得3個不同的控制器實例:每個使用的fxml文件一個。我經常看到fxml文件的經驗不足的用戶期望字段「神奇地」被注入到處;情況並非如此。對象只被注入到與聲明它們的fxml文件一起使用的控制器實例中。 – fabian

+0

我明白你的意思,我不希望發生魔法,我希望瞭解它是如何工作的以及如何正確使用它,並且我不明白如果按照注入請求初始化,我不能訪問元素。因此應用簡單的繼承;對於一個簡單的括號,在我的程序中(不是這個例子),我已經有9個控制器,並且還在增長; –

相關問題