基本上我有2个TableView元素。一个是团队,另一个是人。
这些tableView中的每一个都有自己的FXML文件,并在控制器中创建。例如,这里是TeamViewController.java
:
public class TeamsViewController implements Initializable{
// Service layer for accessing datastore
private ServiceLayer serviceLayer = new ServiceLayer();
@FXML private TableView<Team> tableView;
private TableColumn<Team, Integer> idCol;
@Override
public void initialize(URL location, ResourceBundle resources) {
tableView.getColumns().addAll(
this.createIdCol(),
this.createTeamNameCol(),
this.createButtonColumn()); // Click to show Team members
tableView.getItems().setAll(serviceLayer.getAllTeams());
}
// ......... bunch of methods to create the columns, etc.
这里是FXML文件:
<AnchorPane prefHeight="342.0000999999975" prefWidth="568.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="main.ui.TeamsViewController">
<children>
<TableView fx:id="tableView" maxHeight="-1.0" maxWidth="-1.0" prefHeight="282.0" prefWidth="568.0" />
</children>
</AnchorPane>
现在,当我以这种方式将它们分别添加到我的应用程序的主场景时,两个表都正常工作:
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
// This shows the Team table
AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("teamViewController.fxml"));
Scene scene = new Scene(root,800,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
//This will show the People table
AnchorPane people = (AnchorPane) FXMLLoader.load(getClass().getResource("peopleViewController.fxml"));
scene.setRoot(people);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
log.severe(e.getMessage());
e.printStackTrace();
}
}
Teams Table的最后一列有一个按钮。所以,我被困的地方是: 当我单击Team表中的“Detail”按钮时,如何显示团队成员表(People)? 提前感谢任何提示!
答案 0 :(得分:0)
当您在两个tableviews中使用FXML时,您需要在“详细信息”按钮的单击事件中切换这些fxml。
您还需要更改应用程序文件以更改fxmls。
您可以使用此代码
public void start(Stage primaryStage) {
try {
stage = primaryStage;
gotohome();
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void gotohome() {
try {
FXMLController home = (FXMLController) replaceSceneContent("Homepage.fxml");
home.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void gotoproductwise() {
try {
SampleController product_wise = (SampleController) replaceSceneContent("/product_wise/product_wise.fxml");
product_wise.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Initializable replaceSceneContent(String fxml) throws Exception {
FXMLLoader loader = new FXMLLoader();
InputStream in = Main.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Main.class.getResource(fxml));
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
// Store the stage width and height in case the user has resized the window
double stageWidth = stage.getWidth();
if (!Double.isNaN(stageWidth)) {
stageWidth -= (stage.getWidth() - stage.getScene().getWidth());
}
double stageHeight = stage.getHeight();
if (!Double.isNaN(stageHeight)) {
stageHeight -= (stage.getHeight() - stage.getScene().getHeight());
}
Scene scene = new Scene(page);
if (!Double.isNaN(stageWidth)) {
page.setPrefWidth(stageWidth);
}
if (!Double.isNaN(stageHeight)) {
page.setPrefHeight(stageHeight);
}
// stage.setOpacity(1);
stage.setScene(scene);
stage.sizeToScene();
return (Initializable) loader.getController();
}
所以你必须将主应用程序类的对象发送到控制器,然后你可以调用切换方法。
将此代码添加到Controller类
private Main application;
public void setApp(Main application) {
this.application=application;
}