2015-12-27 70 views
0

如何從if語句更新標籤currentPlayerFileLabel的文本,以便從文件中獲取路徑(如果存在);否則獲取默認的字符串?在JavaFX中更新標籤

守則截至目前:

package sample; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import sample.controllers.Util; 
import sample.model.Config; 

import java.io.File; 
import java.net.URL; 
import java.security.spec.ECField; 
import java.util.ResourceBundle; 

public class Main extends Application implements Initializable { 
    private Stage primaryStage = new Stage(); 
    private BorderPane rootLayout; 
    private AnchorPane anchorPanePlayer; 
    private BorderPane borderPaneGame; 
    private Config config = new Config(); 
    private StringProperty isPlayerFileThere = new SimpleStringProperty("No playerfile was fund! Add new one in \"File\""); 

    @FXML 
    private Button playersButton; 
    @FXML 
    private Button gamesButton; 
    @FXML 
    private Button quitButton; 

    @FXML 
    private Label currentPlayerFileLabel = new Label(); 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     this.primaryStage = primaryStage; 
     this.primaryStage.setTitle("Main window"); 

     initLayout(); 
     initConfig(); 

    } 
    //----------- MY QUESTION IS ABOUT THIS METHODE ------------------------------- 
    public void initConfig() { 
     File configFile = new File(System.getProperty("user.dir") + "/Config/config.txt"); 
     if (configFile.exists()) { // Returns true as of now, so the "true" statement of the if statement will be called 
      config = Util.initConfigFile(); 
      isPlayerFileThere.setValue(config.getPlayerFileLocation().toString()); 
      currentPlayerFileLabel.setText(getIsPlayerFileThere()); 
     } else { 
      currentPlayerFileLabel.setText(getIsPlayerFileThere()); 

     } 
    } 
    //----------- MY QUESTION IS ABOUT THIS METHODE ------------------------------- 

    public void initLayout() { 
     try { 
      //Load root layout from fxml 
      FXMLLoader loader = new FXMLLoader(); //Makes a new FXMLLoader 
      loader.setLocation(Main.class.getResource("view/mainView.fxml")); //sets the location of the main fxml file 
      rootLayout = loader.load(); //Loads the anchorpane from the loader, (AnchorPane) is redundent. 
      Scene scene = new Scene(rootLayout); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 

     } catch (Exception e) { 
      e.getStackTrace(); 
     } 

    } 

    public void initPlayerLayout() { 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(Main.class.getResource("view/playerEdit.fxml")); // Gets the new layout. 
      anchorPanePlayer = loader.load(); //Loades the new layout 
      Scene playerScene = new Scene(anchorPanePlayer); //adds a new scene with the loaded layout 
      Stage prevStage = (Stage) playersButton.getScene().getWindow(); //Get the stage from where we come from. 
      prevStage.close(); //Closes the prev stage 
      primaryStage.setScene(playerScene); //Sets new stage with the new layout 
      primaryStage.show(); //Shows new stage 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void initGameLayout() { 
     try { 

      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(Main.class.getResource("view/editGame.fxml")); 
      borderPaneGame = loader.load(); 
      Scene gameScene = new Scene(borderPaneGame); 
      Stage prevStage = (Stage) gamesButton.getScene().getWindow(); 
      prevStage.close(); 
      primaryStage.setScene(gameScene); 
      primaryStage.show(); 

     } catch (Exception e) { 
      e.getStackTrace(); 

     } 
    } 

    public void quitProgram() { 
     Stage stageToQuit = (Stage) quitButton.getScene().getWindow(); 
     stageToQuit.close(); 
    } 

    public Stage getPrimaryStage() { 
     return primaryStage; 
    } 

    public AnchorPane getBorderPanePlayer() { 
     return anchorPanePlayer; 
    } 

    public Config getConfig() { 
     return config; 
    } 

    public void addPlayerFile() { 
     config.setPlayerFileLocation(Util.addPlayerFile()); 
    } 

    public String getIsPlayerFileThere() { 
     return isPlayerFileThere.get(); 
    } 

    public StringProperty isPlayerFileThereProperty() { 
     return isPlayerFileThere; 
    } 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     currentPlayerFileLabel.setText(getIsPlayerFileThere()); 
    } 

    public static void main(String[] args) { 

     launch(args); 
    } 


} 

FXML:

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

<?import java.lang.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.Text?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main"> 
    <bottom> 
     <HBox prefHeight="100.0" prefWidth="200.0" spacing="40.0" BorderPane.alignment="CENTER"> 
     <children> 
      <Region HBox.hgrow="ALWAYS" /> 
      <Button fx:id="playersButton" mnemonicParsing="false" onAction="#initPlayerLayout" text="Players" /> 
      <Button fx:id="gamesButton" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#initGameLayout" text="Games" /> 
      <Button fx:id="quitButton" layoutX="69.0" layoutY="10.0" mnemonicParsing="false" onAction="#quitProgram" text="Quit" /> 
      <Region layoutX="10.0" layoutY="10.0" HBox.hgrow="ALWAYS" /> 
     </children> 
     </HBox> 
    </bottom> 
    <top> 
     <MenuBar BorderPane.alignment="CENTER"> 
     <menus> 
      <Menu mnemonicParsing="false" text="File"> 
      <items> 
       <MenuItem fx:id="addPlayerFileMenu" mnemonicParsing="false" onAction="#addPlayerFile" text="Add new player file" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
      </items> 
      </Menu> 
     </menus> 
     </MenuBar> 
    </top> 
    <center> 
     <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER_RIGHT"> 
     <children> 
      <VBox alignment="CENTER" HBox.hgrow="ALWAYS"> 
       <children> 
        <Label text="Welcom to the main menu!" /> 
        <Label fx:id="currentPlayerFileLabel" text="Label" /> 
       </children> 
      </VBox> 
     </children> 
     </HBox> 
    </center> 
</BorderPane> 

UPDATE現已經摸索出如何做到這一點,與在這個問題上的意見幫助。爲了其他有同樣的問題,這裏是我開始工作的代碼:

主要類:

package sample; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import sample.controllers.MainController; 
import sample.controllers.Util; 
import sample.model.Config; 

import java.io.File; 
import java.net.URL; 
import java.security.spec.ECField; 
import java.util.ResourceBundle; 

public class Main extends Application { 
    MainController mainController = new MainController(); 
    Stage primaryStage; 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     this.primaryStage = primaryStage; 
     FXMLLoader loader = new FXMLLoader(); 
     loader.setLocation(getClass().getResource("view/mainView.fxml")); 
     loader.setController(mainController); 
     Parent root = loader.load(); 
     primaryStage.setScene(new Scene(root)); 
     primaryStage.show(); 
    } 

    public Stage getPrimaryStage() { 
     return primaryStage; 
    } 

    public static void main(String[] args) { 

     launch(args); 
    } 


} 

控制器的主窗口的佈局和一部分有趣的是:

... 
    private Stage primaryStage = new Stage(); 
    private AnchorPane anchorPanePlayer; 
    private BorderPane borderPaneGame; 
    private Config config = new Config(); 
    private StringProperty isPlayerFileThere = new SimpleStringProperty("No playerfile was fund! Add new one in \"File\""); 

    @FXML 
    private Button playersButton; 
    @FXML 
    private Button gamesButton; 
    @FXML 
    private Button quitButton; 

    @FXML 
    private Label currentPlayerFileLabel; 

...  
    @Override 
      public void initialize(URL location, ResourceBundle resources) { 
       File configFile = new File(System.getProperty("user.dir") + "/Config/config.txt"); 
       if (configFile.exists()) { // Returns true as of now, so the "true" statement of the if statement will be called 
        config = Util.initConfigFile(); 
        isPlayerFileThere.setValue(config.getPlayerFileLocation().toString()); 
        currentPlayerFileLabel.setText(getIsPlayerFileThere()); 
       } else { 
        currentPlayerFileLabel.setText(getIsPlayerFileThere()); 

       } 
      } 
    ... 

已更新的FXML行:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" > 
+1

那麼問題是什麼?此外 - 看起來好像該標籤從未添加到場景圖中。 – Itai

+0

問題是我寫道:我如何更新標籤oppon switch語句的文本。該標籤來自fxml文件。 – Lasse

+1

爲什麼你使用'Application'類作爲控制器類? –

回答

1

要覆蓋的標籤,所以你不再使用FXML中定義的實例:

@FXML 
private Label currentPlayerFileLabel = new Label(); 

刪除賦值給新標籤。

+0

那麼..那麼我得到一個NullPointerException ..:/ – Lasse

+1

然後,似乎'currentPlayerFileLabel'沒有映射從FXML。你可以添加FXML到你的問題,這可能實際上有幫助。 – hotzst

+0

我已將它添加到主要問題:) – Lasse