JavaFX-FXML不';从另一个类调用控制器方法后无法更新
这个问题困扰了我几个小时。它没有给我任何异常,控制台得到更新,它告诉我该方法正在执行,但ui中没有任何更改。以下是我的课程: 控制器类:JavaFX-FXML不';从另一个类调用控制器方法后无法更新,java,javafx,Java,Javafx,这个问题困扰了我几个小时。它没有给我任何异常,控制台得到更新,它告诉我该方法正在执行,但ui中没有任何更改。以下是我的课程: 控制器类: public class Controller { public HBox billbox; public int childnr; public void createBill(){ System.out.println("Creating"); TableView<Item> bill = new TableView<
public class Controller {
public HBox billbox;
public int childnr;
public void createBill(){
System.out.println("Creating");
TableView<Item> bill = new TableView<>();
DropShadow dropShadow = new DropShadow();
dropShadow.setRadius(5.0);
dropShadow.setOffsetX(3.0);
dropShadow.setOffsetY(3.0);
dropShadow.setColor(Color.color(0.4, 0.5, 0.5));
VBox fullbill = new VBox();
fullbill.setPadding(new Insets(1,1,1,1));
fullbill.getStyleClass().add("fullbill");
TableColumn<Item,String> nameColumn = new TableColumn<>("Emri");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Item,String> quantsColumn = new TableColumn<>("Sasia");
quantsColumn.setMinWidth(50);
quantsColumn.setCellValueFactory(new PropertyValueFactory<>("quants"));
double tablewidth = nameColumn.getWidth() + quantsColumn.getWidth();
Label tablenrlabel = new Label("Table 5");
tablenrlabel.getStyleClass().add("tablenr-label");
tablenrlabel.setMinWidth(tablewidth);
Button closebutton = new Button("Mbyll");
closebutton.setMinWidth(tablewidth);
closebutton.getStyleClass().add("red-tint");
bill.setItems(getItem());
bill.setMinWidth(256);
bill.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
bill.getColumns().addAll(nameColumn,quantsColumn);
fullbill.setEffect(dropShadow);
fullbill.getChildren().addAll(tablenrlabel,bill,closebutton);
billbox.getChildren().addAll(fullbill);
childnr += 1;
//Loops over every button in every vbox and gives it a seperate listener (the index of the button is hardcoded so it can cause problems if you add more items)
for(int i = 0; i < childnr; i++){
VBox box = (VBox) billbox.getChildren().get(i);
Button btn = (Button) box.getChildren().get(2); //if sudden issues change this
btn.setId(Integer.valueOf(i).toString());
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int index = billbox.getChildren().indexOf(btn.getParent());
billbox.getChildren().remove(index);
childnr -= 1;
System.out.println(btn.getId());
}
});
}
System.out.println("done");
}
public ObservableList<Item> getItem(){
ObservableList<Item> items = FXCollections.observableArrayList();
items.add(new Item("Fanta",1));
items.add(new Item("Kafe",1));
items.add(new Item("Schweps",1));
return items;
}
fxmloader.load()
加载一个FXML文件并从中创建一个对象层次结构:即它创建一个与根元素中指定的类相对应的新对象,并在其上设置属性(通常设置这些属性涉及创建更多的对象等)。fxmloader
(通常)创建的控制器与特定的对象层次结构相关联
在run()
方法中,加载FXML文件并检索关联的控制器,但从未实际显示FXML文件定义的UI:
URL location = getClass().getResource("sample.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
// Loads the FXML file and creates a bunch of objects,
// but you never display the result: you do nothing with root:
Parent root = (Parent) fxmlLoader.load(location.openStream());
// retrieve the controller associate with root:
Controller controller = fxmlLoader.<Controller>getController();
// not sure what this is for...
root.setUserData(controller);
// now call a method on the controller.
controller.createBill();
URL location=getClass().getResource(“sample.fxml”);
FXMLLoader FXMLLoader=新的FXMLLoader();
fxmlLoader.setLocation(位置);
setBuilderFactory(新的JavaFXBuilderFactory());
//加载FXML文件并创建一组对象,
//但您从未显示结果:您对root不做任何操作:
父根=(父)fxmlLoader.load(location.openStream());
//检索与根目录关联的控制器:
控制器控制器=fxmlLoader.getController();
//不知道这是干什么用的。。。
root.setUserData(控制器);
//现在在控制器上调用一个方法。
controller.createBill();
调用controller.createBill()
时,它会修改与控制器关联的UI元素,因此它会修改根
及其子元素。但是,由于root
从不显示在任何位置,因此您永远看不到结果
请注意,您两次加载同一个FXML文件,因此您有它定义的UI的两个副本,以及控制器类的两个实例。控制器的每个实例都与UI的一个实例相关联。在start()
方法中加载FXML文件所获得的UI就是显示的UI;因此,要修改该UI的外观,您需要调用由您在那里创建的fxmloader
创建的控制器实例上的方法,而不是其他一些fxmloader
fxmloader.load()
加载一个FXML文件并从中创建一个对象层次结构:即它创建一个与根元素中指定的类相对应的新对象,并在其上设置属性(通常设置这些属性需要创建更多的对象等)。fxmloader
(通常)创建的控制器与特定的对象层次结构相关联
在run()
方法中,加载FXML文件并检索关联的控制器,但从未实际显示FXML文件定义的UI:
URL location = getClass().getResource("sample.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
// Loads the FXML file and creates a bunch of objects,
// but you never display the result: you do nothing with root:
Parent root = (Parent) fxmlLoader.load(location.openStream());
// retrieve the controller associate with root:
Controller controller = fxmlLoader.<Controller>getController();
// not sure what this is for...
root.setUserData(controller);
// now call a method on the controller.
controller.createBill();
URL location=getClass().getResource(“sample.fxml”);
FXMLLoader FXMLLoader=新的FXMLLoader();
fxmlLoader.setLocation(位置);
setBuilderFactory(新的JavaFXBuilderFactory());
//加载FXML文件并创建一组对象,
//但您从未显示结果:您对root不做任何操作:
父根=(父)fxmlLoader.load(location.openStream());
//检索与根目录关联的控制器:
控制器控制器=fxmlLoader.getController();
//不知道这是干什么用的。。。
root.setUserData(控制器);
//现在在控制器上调用一个方法。
controller.createBill();
调用controller.createBill()
时,它会修改与控制器关联的UI元素,因此它会修改根
及其子元素。但是,由于root
从不显示在任何位置,因此您永远看不到结果
请注意,您两次加载同一个FXML文件,因此您有它定义的UI的两个副本,以及控制器类的两个实例。控制器的每个实例都与UI的一个实例相关联。在
start()
方法中加载FXML文件所获得的UI就是显示的UI;因此,要修改该UI的外观,您需要调用由您在那里创建的fxmloader
创建的控制器实例上的方法,不是其他的FXMLLoader
Hmm我怎样才能调用在我的start方法中从另一个类创建的控制器实例上的方法?我不能回答这个问题,因为问题中没有足够的信息。但是您必须安排“其他类”引用正确的控制器。或者,使用MVC/MVP类型的方法,让控制器和“其他类”都访问共享模型实例。嗯,我怎样才能从其他类调用在start方法中创建的控制器实例上的方法呢?我无法回答这个问题,因为问题中没有足够的信息。但是您必须安排“其他类”引用正确的控制器。或者,使用MVC/MVP类型的方法,让控制器和“其他类”都访问共享模型实例。
URL location = getClass().getResource("sample.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
// Loads the FXML file and creates a bunch of objects,
// but you never display the result: you do nothing with root:
Parent root = (Parent) fxmlLoader.load(location.openStream());
// retrieve the controller associate with root:
Controller controller = fxmlLoader.<Controller>getController();
// not sure what this is for...
root.setUserData(controller);
// now call a method on the controller.
controller.createBill();