Я читал довольно много постов о заполнении таблиц данными, но все еще не могу понять.Я создал таблицу, фактически 2 таблицы, с помощью построителя сцен.Тем не менее, я не могу найти, как заполнить хотя бы одну из этих таблиц.
Каждый объект "Inventory" имеет 2 массива, один тип "Part" и один тип "Product".«Часть» и «Продукт» являются отдельными классами.«inHouse» является дочерним классом «Part».
Теперь я понятия не имею, как использовать метод initialize в контроллере, я сделал 1 наблюдаемый список и хочу заполнить одну из таблиц этимсписок.Как я могу это сделать?Нужно ли создавать объект класса контроллера?Где я могу использовать метод инициализации?Какие значения я передаю?Где в коде я использую переменные столбцов и таблиц в классе контроллера?Я видел много сообщений, но я просто не понимаю.
Вот контроллер:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class PartTable implements Initializable{
@FXML
private TreeTableView<Part> partTable;
@FXML
private TreeTableColumn<Part, Integer> partID;
@FXML
private TreeTableColumn<Part, String> partName;
@FXML
private TreeTableColumn<Part, Integer> inventoryLevel;
@FXML
private TreeTableColumn<Part, Double> partPrice;
@FXML
private TreeTableView<Product> productTable;
@FXML
private TreeTableColumn<Product, Integer> productID;
@FXML
private TreeTableColumn<Product, String> productName;
@FXML
private TreeTableColumn<Product, Integer> inventoryLevel2;
@FXML
private TreeTableColumn<Product, Double> productPrice;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
partID.setCellValueFactory(new PropertyValueFactory("PartID"));
partName.setCellFactory(new PropertyValueFactory("Part Name"));
inventoryLevel.setCellFactory(new PropertyValueFactory("Inventory Level"));
partPrice.setCellFactory(new PropertyValueFactory("Price/Cost per Unit"));
}
}
Вот Main.java (я знаю,многие импорты избыточны, но в любом случае):
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
public class Main extends Application {
private TableView table1 = new TableView();
@Override
public void start(Stage primaryStage) throws Exception{
try {
Inventory i1 = new Inventory();
i1.addPart(new InHouse(54534, "boli", 156.0, 2, 0, 100));
i1.addPart(new InHouse(74541, "libo", 156.0, 2, 0, 100));
i1.addPart(new InHouse(24551, "luci", 156.0, 2, 0, 100));
i1.addPart(new InHouse(44581, "buci", 156.0, 2, 0, 100));
i1.addPart(new InHouse(94531, "boluci", 156.0, 2, 0, 100));
ObservableList<Part> oPart = FXCollections.observableList(i1.getPartList());
PartTable table = new PartTable();
Pane mainPane = (Pane)FXMLLoader.load(Main.class.getResource("MainMenu.fxml"));
Scene scene = new Scene(mainPane);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Inventory Management System");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Вот файл fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TreeTableColumn?>
<?import javafx.scene.control.TreeTableView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="482.0" prefWidth="1220.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.PartTable">
<children>
<HBox layoutX="7.0" layoutY="12.0" prefHeight="440.0" prefWidth="1200.0" spacing="30.0">
<children>
<GridPane prefHeight="440.0" prefWidth="600.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="174.0" minHeight="10.0" prefHeight="109.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="249.0" minHeight="9.0" prefHeight="218.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="284.0" minHeight="10.0" prefHeight="108.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane GridPane.columnSpan="4">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" text="Search" GridPane.columnIndex="2">
<font>
<Font size="15.0" />
</font>
</Button>
<TextField GridPane.columnIndex="3" GridPane.columnSpan="2" />
<Label prefHeight="63.0" prefWidth="88.0" text="Parts">
<font>
<Font size="17.0" />
</font>
</Label>
</children>
</GridPane>
<TreeTableView fx:id="partTable" prefHeight="220.0" prefWidth="100.0" GridPane.columnSpan="4" GridPane.rowIndex="1">
<columns>
<TreeTableColumn fx:id="partID" editable="false" prefWidth="75.0" text="PartID" />
<TreeTableColumn fx:id="partName" editable="false" prefWidth="75.0" text="Part Name" />
<TreeTableColumn fx:id="inventoryLevel" editable="false" prefWidth="75.0" text="Inventory Level" />
<TreeTableColumn fx:id="partPrice" editable="false" prefWidth="75.0" text="Price/Cost per Unit" />
</columns>
<columnResizePolicy>
<TreeTableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TreeTableView>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="142.0" text="Delete" GridPane.columnIndex="3" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="202.0" text="Modify" GridPane.columnIndex="2" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="162.0" text="Add" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Button>
</children>
</GridPane>
<GridPane layoutX="10.0" layoutY="10.0" prefHeight="440.0" prefWidth="600.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="174.0" minHeight="10.0" prefHeight="109.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="249.0" minHeight="9.0" prefHeight="218.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="284.0" minHeight="10.0" prefHeight="108.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane GridPane.columnSpan="4">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" text="Search" GridPane.columnIndex="2">
<font>
<Font size="15.0" />
</font>
</Button>
<TextField GridPane.columnIndex="3" GridPane.columnSpan="2" />
<Label prefHeight="63.0" prefWidth="88.0" text="Products">
<font>
<Font size="17.0" />
</font>
</Label>
</children>
</GridPane>
<TreeTableView fx:id="productTable" prefHeight="220.0" prefWidth="100.0" GridPane.columnSpan="4" GridPane.rowIndex="1">
<columns>
<TreeTableColumn fx:id="productID" editable="false" prefWidth="75.0" text="ProductID" />
<TreeTableColumn fx:id="productName" editable="false" prefWidth="75.0" text="Product Name" />
<TreeTableColumn fx:id="inventoryLevel2" editable="false" prefWidth="75.0" text="Inventory Level" />
<TreeTableColumn fx:id="productPrice" editable="false" prefWidth="75.0" text="Price per Unit" />
</columns>
<columnResizePolicy>
<TreeTableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TreeTableView>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="142.0" text="Delete" GridPane.columnIndex="3" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="172.0" text="Modify" GridPane.columnIndex="2" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="39.0" prefWidth="219.0" text="Add" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Button>
</children>
</GridPane>
</children>
</HBox>
</children>
</Pane>