在fxml

时间:2017-02-21 05:51:20

标签: javafx

我想在我点击第一个按钮时加载inventory_layout但是我在那里得到加载异常,而相同的代码与其他fxml一起工作正常。

控制器的路径是对的。 fxml在正确的位置。

这是我的代码 - 库存

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import pharmbooks.Utils.DbSingleton;
import pharmbooks.Utils.UIController;

import java.io.IOException;

public class MainController {


    @FXML
    private BorderPane borderPane;


    @FXML
    public void initInventory(ActionEvent actionEvent) throws IOException{
        UIController<InventoryController> uiController=new UIController<InventoryController>();
        uiController.setUp(borderPane,"inventory_layout.fxml");

    }


    public void initPurchase(ActionEvent actionEvent) throws IOException{
        UIController<PurchaseController> uiController=new UIController<>();
        uiController.setUp(borderPane,"purchase_layout.fxml");
    }

}

Ui控制器

public class UIController<T> {

    private T controller;
    private Pane pane;
    public T getController() {
        return controller;
    }


    public void setUp(Pane mainPane,String fxmlLocation){
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getClassLoader().getResource(fxmlLocation));
            pane = loader.load();

            controller = loader.getController();

            mainPane.getChildren().clear();
            mainPane.getChildren().add(pane);

        }catch (Exception e){
            System.out.println(UIController.class.getSimpleName()+" "+e.toString());
        }
    } }

此代码在ubuntu中运行良好:/

这是错误:

UIController javafx.fxml.LoadException: /Users/manyamadan/Desktop/Everything/pharmbooks-v2/target/classes/inventory_layout.fxml

库存控制器:

公共类InventoryController实现Initializable {

public TableColumn batchId;
public TableColumn company;
public TableColumn name;
public TableColumn packing;
public TableColumn quantity;
public TableColumn expiry;
public TableColumn mrp;

@FXML
private TableView<Inventory> personTable;

public void InventoryController()
{

}

public void initialize(URL location, ResourceBundle resources) {




    batchId.setCellValueFactory(new PropertyValueFactory<Inventory, String>("BATCH_ID"));
    company.setCellValueFactory(new PropertyValueFactory<Inventory, String>("COMPANY"));
    name.setCellValueFactory(new PropertyValueFactory<Inventory, String>("PRODUCT_NAME"));
    packing.setCellValueFactory(new PropertyValueFactory<Inventory, String>("PACKING"));
    quantity.setCellValueFactory(new PropertyValueFactory<Inventory, Integer>("QUANTITY"));
    expiry.setCellValueFactory(new PropertyValueFactory<Inventory, String>("EXPIRY_DATE"));
    mrp.setCellValueFactory(new PropertyValueFactory<Inventory, String>("MRP"));




    personTable.setEditable(true);


    personTable.setItems(InventoryData.getInstance().getList());
    // name.setOnEditCommit(TableColumn.CellEditEvent(Inventory,String)event);
    Inventory inventory_data = personTable.getSelectionModel().getSelectedItem();
    System.out.println(inventory_data.getPRODUCT_NAME()+ "" + inventory_data.getBATCH_ID());

    personTable.setOnKeyPressed(event -> {
        TablePosition<Inventory, ?> pos = personTable.getFocusModel().getFocusedCell() ;
        if (pos != null) {
            AnchorPane anchorPane= null;
            try {
                anchorPane = FXMLLoader.load(getClass().getClassLoader().getResource("edit_inventory_data.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            Stage stage=new Stage();
            stage.setTitle("Apna Dialog");
            stage.initModality(Modality.WINDOW_MODAL);
            stage.initOwner(StageManager.getInstance().getPrimaryStage());

            Scene scene = new Scene(anchorPane);
            stage.setScene(scene);

            stage.showAndWait();

        }
    });



    name.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Inventory, String>>() {
                             @Override
                             public void handle(TableColumn.CellEditEvent<Inventory, String> t) {
                                 System.out.println("ON edit commit" + t);
                                 ((Inventory) t.getTableView().getItems().get(
                                         t.getTablePosition().getRow())).setPRODUCT_NAME(t.getNewValue());
                             }
                         }
    );
}


@FXML
public void addData(ActionEvent actionEvent) throws IOException {
    AnchorPane anchorPane= FXMLLoader.load(getClass().getClassLoader().getResource("add_data_inventory.fxml"));

    Stage stage=new Stage();
    stage.setTitle("Apna Dialog");
    stage.initModality(Modality.WINDOW_MODAL);
    stage.initOwner(StageManager.getInstance().getPrimaryStage());

    Scene scene = new Scene(anchorPane);
    stage.setScene(scene);

    stage.showAndWait();

    personTable.setItems(InventoryData.getInstance().getList());

}

}

inventory_layout.fxml

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane maxHeight="600.0" maxWidth="800.0" prefHeight="600.0" prefWidth="800.0" stylesheets="@beautify_inventory.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pharmbooks.controllers.InventoryController">
    <center>
        <TableView fx:id="personTable" prefWidth="10000.0" BorderPane.alignment="CENTER">
            <columns>
                <TableColumn fx:id="batchId" prefWidth="75.0" text="BatchId" />
                <TableColumn fx:id="packing" prefWidth="75.0" text="packing" />
                <TableColumn fx:id="quantity" prefWidth="75.0" text="quantity" />
                <TableColumn fx:id="expiry" prefWidth="75.0" text="expiry" />
                <TableColumn fx:id="mrp" prefWidth="75.0" text="mrp" />
            </columns>
        </TableView>
    </center>
    <top>
        <HBox prefHeight="67.0" prefWidth="800.0" BorderPane.alignment="CENTER">
            <children>
                <TextField prefHeight="47.0" prefWidth="163.00009155273438">
                    <HBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                    </HBox.margin>
                </TextField>
                <Button mnemonicParsing="false" onAction="#addData" text="Button">
                    <HBox.margin>
                        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                    </HBox.margin>
                </Button>
                <Button mnemonicParsing="false" prefHeight="39.0" prefWidth="121.0" text="Button">
                    <HBox.margin>
                        <Insets bottom="20.0" right="20.0" top="20.0" />
                    </HBox.margin>
                </Button>
                <Button mnemonicParsing="false" prefHeight="39.0" prefWidth="129.0" text="Button">
                    <HBox.margin>
                        <Insets bottom="20.0" top="20.0" />
                    </HBox.margin>
                </Button>
            </children>
        </HBox>
    </top>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

inventory_layout.fxml中,您将fx:id属性设置为所有表格列:

<TableColumn fx:id="batchId" prefWidth="75.0" text="BatchId" />
<TableColumn fx:id="packing" prefWidth="75.0" text="packing" />
<TableColumn fx:id="quantity" prefWidth="75.0" text="quantity" />
<TableColumn fx:id="expiry" prefWidth="75.0" text="expiry" />
<TableColumn fx:id="mrp" prefWidth="75.0" text="mrp" />

但在InventoryController中,您没有使用@FXML注释此组件。没有这个注释FXMLLoader将无法向控制器注入正确的对象。正确的代码:

@FXML
public TableColumn batchId;
@FXML
public TableColumn company;
@FXML
public TableColumn name;
@FXML
public TableColumn packing;
@FXML
public TableColumn quantity;
@FXML
public TableColumn expiry;
@FXML
public TableColumn mrp;

当您尝试在表列上NullPointerException时,我猜测您在initalize()方法中获得setCellValueFactory()

我假设您正在正确访问fxml资源文件。