我一直想知道如何让按钮工作一周,我仍然无法找到解决方案。
我的主类基本上加载了所有.fxml文件。
rootLayout.fxml - 在BoardPane中心的菜单栏/两个按钮/设置了包含TableView的contentLayout.fxml。
import java.io.IOException;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.fxml.FXMLLoader;
import javafx.stage.Modality;
import javafx.stage.Stage;
import vp.dict.model.Data;
import vp.dict.view.ContentController;
import vp.dict.view.EditDialogController;
import vp.dict.view.RootController;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
public class MainApp extends Application {
private Stage window;
private BorderPane rootLayout;
private ObservableList<Data> dataList = FXCollections.observableArrayList();
public MainApp(){
dataList.add(new Data("Hi", "Haj", "Ahoj, cau"));
dataList.add(new Data("Hello", "Helou", "Ahoj"));
dataList.add(new Data("New", "Nju", "Novy"));
}
public ObservableList<Data> getDataList(){
return dataList;
}
public void start(Stage window) throws IOException {
this.window = window;
this.window.setTitle("EN/CZ Dictionary 2017");
initRootLayout();
initContent();
}
public void initRootLayout() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("../view/rootLayout.fxml"));
rootLayout = (BorderPane)loader.load();
Scene scene = new Scene(rootLayout);
window.setScene(scene);
RootController controller = loader.getController();
controller.setMainApp(this);
window.show();
}
public void initContent() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("../view/contentLayout.fxml"));
AnchorPane ap = (AnchorPane)loader.load();
rootLayout.setCenter(ap);
ContentController controller = loader.getController();
controller.setMainApp(this);
}
public boolean showEditWindow(Data data) throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("../view/editDialogLayout.fxml"));
AnchorPane ap = (AnchorPane)loader.load();
Stage editWindow = new Stage();
editWindow.setTitle("Add a New Word");
editWindow.initModality(Modality.WINDOW_MODAL);
editWindow.initOwner(window);
Scene scene = new Scene(ap);
editWindow.setScene(scene);
EditDialogController controller = loader.getController();
controller.setStage(editWindow);
controller.setData(data);
editWindow.showAndWait();
return controller.isOkClicked();
}
public Stage getWindow(){
return window;
}
public static void main(String[] args) {
launch(args);
}
}
内容控制器只显示TableView数据
package vp.dict.view;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import vp.dict.main.MainApp;
import vp.dict.model.Data;
public class ContentController {
@FXML
private TableView<Data> dataTable;
@FXML
private TableColumn<Data, String> englishColumn;
@FXML
private TableColumn<Data, String> pronunciationColumn;
@FXML
private TableColumn<Data, String> meaningColumn;
private MainApp mainApp;
public ContentController(){
}
@FXML
public void initialize(){
englishColumn.setCellValueFactory(CellData -> CellData.getValue().englishProperty());
pronunciationColumn.setCellValueFactory(CellData -> CellData.getValue().pronunciationProperty());
meaningColumn.setCellValueFactory(CellData -> CellData.getValue().meaningProperty());
}
public void setMainApp(MainApp mainApp){
this.mainApp = mainApp;
dataTable.setItems(mainApp.getDataList());
}
public TableView<Data> geTableView(){
return dataTable;
}
}
RootController类是我需要做的工作。在方法private void handleDelete()中你可以在注释中看到一些代码,这是一个有效的代码,但是我无法想出一种从ContentController类加载数据的方法,这是上面代码的垃圾。
package vp.dict.view;
import java.io.IOException;
import javax.security.auth.login.Configuration;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TableView;
import vp.dict.main.MainApp;
import vp.dict.model.Data;
public class RootController {
private MainApp mainApp;
public void initialize(){
}
public RootController(){
}
public void setMainApp(MainApp mainApp){
this.mainApp = mainApp;
}
@FXML
private void addClicked() throws IOException{
Data tempData = new Data();
boolean okClicked = mainApp.showEditWindow(tempData);
if(okClicked){
mainApp.getDataList().add(tempData);
}
}
@FXML
private void handleDelete() throws IOException{
/* int selectedIndex = contentTable.geTableView().getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
contentTable.geTableView().getItems().remove(selectedIndex);
} else {
// Nothing selected.
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getWindow());
alert.setTitle("No Selection");
alert.setHeaderText("No Person Selected");
alert.setContentText("Please select a person in the table.");
alert.showAndWait();
}*/
}
}
我很欣赏任何有关如何使其发挥作用的想法。
答案 0 :(得分:0)
首先,您无法从ContentController
到达RootController
,因为您在这两者之间没有任何参考。其次,让ContentController
处理它可能会更加整洁,而RootController
只会进行通知。
好的,我在这里搞砸了执行命令。这是正确的。
MainApp:
private void RootController rootController;
...
// In initRoot()
this.rootController = loader.getController();
// Replace the below line in initContent():
//ContentController controller = loader.getController();
//controller.setMainApp(this);
ContentController controller = loader.getController();
controller.setMainApp(this);
this.rootController.setContentController(controller);
RootController:
private ContentController contentController;
public void setContentController(ContentController content)
{
this.contentController = content;
}
...
@FXML
private void handleDelete() throws IOException
{
if (this.contentController != null)
this.contentController.handleDelete();
}
ContentController:
public void handleDelete()
{
// Whatever delete logic you originally had here
}