这个问题困扰了我几个小时。它没有给我任何例外,控制台得到更新,它告诉我方法正在执行但是ui没有变化。这是我的课程:
控制器类:
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;
}
调用控制器的类createBill()方法:
public void run(){
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
URL location = getClass().getResource("sample.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
Parent root = (Parent) fxmlLoader.load(location.openStream());
Controller controller = fxmlLoader.<Controller>getController();
root.setUserData(controller);
controller.createBill();
}catch (Exception x){
System.out.println(x);
}
}
});
}
这是我的主要方法:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("uPick Smart Service");
primaryStage.setScene(new Scene(root, 1600, 600));
primaryStage.show();
}
public static void main(String args[]){
launch(args);
}
}
答案 0 :(得分:2)
FXMLLoader.load()
加载FXML文件并从中创建一个对象层次结构:即创建一个与根元素中指定的类相对应的新对象,并在其上设置属性(通常设置这些属性涉及创建进一步的对象等)。 FXMLLoader
(通常)创建的控制器与该特定对象层次结构相关联。
在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();
当您调用controller.createBill()
时,它会修改与控制器关联的UI元素,因此它会修改root
及其子元素。但是,由于root
永远不会显示在任何地方,因此您永远不会看到结果。
请注意,您加载相同的FXML文件两次,因此您有两个它定义的UI副本,以及两个控制器类实例。控制器的每个实例与UI的一个实例相关联。您在start()
方法中加载FXML文件所获得的UI是显示的UI;因此,要修改UI的外观,您需要调用由您在那里创建的FXMLLoader
创建的控制器实例上的方法,而不是其他FXMLLoader
。