【发布时间】:2017-07-26 19:10:10
【问题描述】:
我有一个应用程序,我试图保持 mvc 规则。我有一个模型
public class CarTableView {
SimpleIntegerProperty id = new SimpleIntegerProperty();
SimpleStringProperty brand = new SimpleStringProperty();
SimpleStringProperty engine = new SimpleStringProperty();
SimpleBooleanProperty navi = new SimpleBooleanProperty();
SimpleBooleanProperty available = new SimpleBooleanProperty();
SimpleDoubleProperty liters = new SimpleDoubleProperty();
SimpleIntegerProperty power = new SimpleIntegerProperty();
ObservableList<CarTableView> observableList = FXCollections.observableArrayList();
public CarTableView()
{
}
public CarTableView(int id,String brand,String engine,Boolean navi,Boolean available,double liters,int power)
{
this.id.set(id);
this.brand.set(brand);
this.engine.set(engine);
this.navi.set(navi);
this.available.set(available);
this.liters.set(liters);
this.power.set(power);
}
我需要在我的两个控制器中使用它才能获得对 ObservableList 的引用。这就是我的问题所在。在哪里创建模型?我需要在调用 initialize() 方法之前创建他
public class MainController {
private CarTableView model = new CarTableView();
@FXML
private Button addVehicleButton;
@FXML
private Button showClientDatabaseButton;
@FXML
public void initialize()
{
(...)
model.getObservableList().add(new CarTableView(213,"FIAT","1.9 JTD",true,true,32.4,132)); // HERE I use model (it's fine here because its created in this class)
tableView.setItems(model.getObservableList());
@FXML
public void addNewVehicleButtonClicked() throws IOException
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/AddNewCar.fxml"));
Stage stage = new Stage();
Scene scene = new Scene((Pane)loader.load());
stage.setScene(scene);
stage.show();
AddNewCarController addNewCarController = loader.getController();
addNewCarController.initData(model); // HERE i try to initialize model in second controller
}
但我在另一个控制器的 initialize() 方法中也需要他
public class AddNewCarController {
private ObservableList<String> choiceBoxList = FXCollections.observableArrayList("YES","NO");
@FXML
public void initialize()
{
autoIncrementChekBox.setSelected(true);
if(autoIncrementChekBox.isSelected())
{
idTextField.setText(Integer.toString((model.getObservableList().size()+1))); // HERE I NEED HIM TOO!! But it is null... he havent been load yet!
idTextField.setDisable(true);
}
comboBox.setItems(choiceBoxList);
comboBox.setValue("YES");
}
在这种情况下,即使我将在第二个控制器中传递数据抛出函数 initModel()。我应该在哪里创建模型并传递给两个控制器? 我还将添加我的 Main() 文件以清除情况
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Managment System");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Main.fxml"));
Scene scene = new Scene((Pane)loader.load(),1800,900);
MainController mainController = loader.getController();
stage.setScene(scene);
stage.show();
}
}
【问题讨论】:
标签: model-view-controller javafx javafx-8