My Fxml file:
***<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pos_draft3.Controller">
<children>
<TextField alignment="CENTER" layoutX="419.0" layoutY="2.0" prefHeight="31.0" prefWidth="179.0" promptText="Search">
<font>
<Font name="System Bold" size="12.0" />
</font>
</TextField>
<TableView fx:id="viewStocksTable" layoutY="38.0" prefHeight="362.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="38.0">
<columns>
<TableColumn fx:id="itemNameCol" prefWidth="418.0" text="Item_Name" />
<TableColumn fx:id="stocksCol" prefWidth="118.0" text="Stocks" />
<TableColumn fx:id="rateCol" prefWidth="63.0" text="Rate" />
</columns>
</TableView>
</children>
</AnchorPane>
And my Controller class:
package pos_draft3;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
public class Controller {
@FXML private TableView<Item> viewStocksTable;
@FXML private TableColumn itemNameCol;
@FXML private TableColumn stocksCol;
@FXML private TableColumn rateCol;
//The handler function for the checkout
public void checkout(ActionEvent e) {
try {
Parent root = FXMLLoader.load(getClass().getResource("checkout.fxml"));
Stage checkout_stage = new Stage();
checkout_stage.setTitle("Checkout");
checkout_stage.setScene(new Scene(root, 1080, 720));
checkout_stage.show();
} catch (Exception exception) {
exception.printStackTrace();
}
}
//The handler for the item lookup in the checkout_page
public void searchitem(ActionEvent e) {
try {
Parent root = FXMLLoader.load(getClass().getResource("viewStocks.fxml"));
Stage checkout_stage = new Stage();
checkout_stage.setTitle("Checkout");
checkout_stage.setScene(new Scene(root, 1080, 720));
checkout_stage.show();
System.out.println(viewStocksTable);
// this.populateTreeTableview();
} catch (Exception exception) {
exception.printStackTrace();
}
}
//This section of code populates the observable list of items into the table column into the viewStocks.fxml
//Declaring the method to populate the treeTableview
public void populateTreeTableview() {
final ObservableList<Item> items= FXCollections.observableArrayList(
new Item("Wai Wai noodle",5,20),
new Item("Dettol Soap",10,25),
new Item("Current Noodles",15,50)
);
itemNameCol.setCellValueFactory(new PropertyValueFactory<Item,String>("item_name"));
stocksCol.setCellValueFactory(new PropertyValueFactory<Item,Integer>("stocks"));
rateCol.setCellValueFactory(new PropertyValueFactory<Item,Double>("rate"));
viewStocksTable.setItems(items);
viewStocksTable.getColumns().addAll(itemNameCol,stocksCol,rateCol);
}
}***
I created a Tableview in fxml file using the Scenebuilder and specified the Controller class. In controller class I declared a variable called viewStocksTable which is type of Tableview. But the viewStocksTable variable is null. The program is showing the null pointer exception for viewStocksTable. How to use the tableview object crated in FXMl file in ControllerClass?Please help and thanks in advance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…