2015-11-06 46 views
0

所以我有一个BorderPane在我FXML,我想要做的就是重新加载我BorderPane.Center属性,以便它显示了不同的布局不同的窗格,如何从代码编辑fxml?

我怎样才能做到这一点?下面的代码不适用于我。

p.s:抱歉印尼人。

我FXML:

<BorderPane fx:id="bPane" xmlns:fx="http://javafx.com/fxml" fx:controller="fx.HomeMhsController" stylesheets="@css.css">  
<left> 
    <VBox spacing="10" fx:id="vbox" id="vbox" prefWidth="100" > 
     <Label id="Label" fx:id="biodata" text="Biodata" onMouseClicked="#showIdentity"/> 
     <Label id="Label" fx:id="histori" text="Histori Nilai" onMouseClicked="#showHistory" /> 
     <Label id="Label" fx:id="jadwal" text="Jadwal Kuliah" onMouseClicked="#showSchedule" /> 
     <Label id="Label" fx:id="pilih" text="Pilih Mata Kuliah" onMouseClicked="#chooseSchedule"/> 
    </VBox> 
</left> 

我的控制器:

public class HomeMhsController{ 
@FXML private Label biodata2; 
@FXML private BorderPane bPane; 

@FXML private void showIdentity() throws SQLException{ 
    init.initializeDB(); 

    String query="select * from mahasiswa where nama_dpn ='" + MainController.username + "'"; 
    ResultSet rset = init.stmt.executeQuery(query); 

    if(rset.next()){ 
      String nim = rset.getString(1); 
      String nama_dpn = rset.getString(2); 
      String nama_blkg = rset.getString(3); 
      String tgl_lahir = rset.getString(4); 
      String tempat_lahir = rset.getString(5); 
      String jns_kelamin = rset.getString(6); 
      String agama = rset.getString(7); 
      String alamat = rset.getString(8); 
      String no_hp = rset.getString(9); 
      biodata2.setText(nim + nama_dpn + nama_blkg + tgl_lahir + tempat_lahir + jns_kelamin + agama + alamat + no_hp); 
    } 
} 
@FXML private void showHistory() throws IOException{ 
    FXMLLoader loader = new FXMLLoader(); 
    Parent rootNode = null; 
    rootNode = loader.load(getClass().getResource("homeMhs_fxml.fxml")); 
    Label dua = new Label("2"); 
    Pane pane = new Pane(); 
    pane.getChildren().add(dua); 
    ((BorderPane) rootNode).setCenter(pane); 
} 
@FXML private void showSchedule() throws SQLException, IOException{ 
    FXMLLoader loader = new FXMLLoader(); 
    Parent rootNode = null; 
    rootNode = loader.load(getClass().getResource("homeMhs_fxml.fxml")); 
    Label tiga = new Label("3"); 
    Pane pane = new Pane(); 
    pane.getChildren().add(tiga); 
    ((BorderPane) rootNode).setCenter(pane); 
} 
@FXML private void chooseSchedule() throws SQLException{ 

} 

回答

0

在下面的代码片段:

@FXML private void showHistory() throws IOException{ 
    FXMLLoader loader = new FXMLLoader(); 
    Parent rootNode = null; 
    rootNode = loader.load(getClass().getResource("homeMhs_fxml.fxml")); 
    Label dua = new Label("2"); 
    Pane pane = new Pane(); 
    pane.getChildren().add(dua); 
    ((BorderPane) rootNode).setCenter(pane); 
} 

你再次加载FXML文件,该文件是一个新的实例ce和你已经拥有的不同(因为这个方法在它的控制器类中)。此外,您对这个新加载的内容没有做任何事情(即rootNode未被添加到任何场景中)。

,而不是仅仅使用现有的注入边境窗格:

@FXML private void showHistory() { 
    Label dua = new Label("2"); 
    Pane pane = new Pane(); 
    pane.getChildren().add(dua); 
    bPane.setCenter(pane); 
} 

以上短:

@FXML private void showHistory() { 
    bPane.setCenter(new Pane(new Label("2"))); 
} 
+0

我明白了,THX对于这一点,它的工作原理! – Jason