I know that the topic is on the Internet, but I can’t solve the problem. It seems everything is correct, but the data is not displayed. When you click a button, a Stage opens, in which there is a TableView. Implementation:
Main:
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; import java.io.IOException; import java.util.ArrayList; import static Client.NET.Client.STOP; import static Client.NET.Client.ShowCar; import static Client.NET.Client.getInstanceClient; public class Main extends Application { public static Stage primaryStage; @Override public void start(Stage primaryStage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("/Client/FXML/Scene1.fxml")); primaryStage.setScene(new Scene(root)); runStage(primaryStage); } public static void setStage(Stage stage){ primaryStage = stage; } public static Stage getStage() { return primaryStage; } public static void setScene(Scene scene) { primaryStage.setScene(scene); } public static Scene getScene() { return primaryStage.getScene(); } public static void runStage(Stage stage) throws IOException { primaryStage = stage; primaryStage.setTitle("ClientProgram"); primaryStage.setMinHeight(300); primaryStage.setMinWidth(530); primaryStage.setMaxHeight(400); primaryStage.setMaxWidth(630); getInstanceClient(); primaryStage.show(); } public static void main(String[] args) { launch(args); } } fxml tables:
<TableView fx:id="TableCars" layoutX="8.0" layoutY="9.0"prefHeight="312.0" prefWidth="586.0" AnchorPane.bottomAnchor="23.0" AnchorPane.leftAnchor="8.0" AnchorPane.rightAnchor="6.0" AnchorPane.topAnchor="9.0"> <columns> <TableColumn fx:id="columnMarka" prefWidth="75.0" text="columnMarka" /> <TableColumn fx:id="columnModel" prefWidth="75.0" text="columnModel" /> <TableColumn fx:id="columnYear" prefWidth="75.0" text="columnYear" /> <TableColumn fx:id="columnColor" prefWidth="75.0" text="columnColor" /> <TableColumn fx:id="columnPrice" prefWidth="75.0" text="columnPrice" /> <TableColumn fx:id="columnCount" prefWidth="75.0" text="columnCount" /> </columns> <columnResizePolicy> <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> </columnResizePolicy> </TableView> Controller1:
public void IsAuto(ActionEvent actionEvent) throws IOException { MakeTable makeTable = new MakeTable(); makeTable.MakeTable(); } class MakeTable implements Initializable { public void MakeTable () throws IOException { Parent root = FXMLLoader.load(getClass().getResource("/Client/FXML/ShowAuto.fxml")); Main.primaryStage.setScene(new Scene(root)); Main.primaryStage.show(); } @Override public void initialize(URL location, ResourceBundle resources) { ArrayList<Goods> arrayList = ShowCar();//ShowCar - возвращает ArrayList c данными ObservableList<Goods> Cars = FXCollections.observableArrayList(arrayList); columnMarka.setCellValueFactory(new PropertyValueFactory<Goods,String>("columnMarka")); columnModel.setCellValueFactory(new PropertyValueFactory<Goods,String>("columnModel")); columnColor.setCellValueFactory(new PropertyValueFactory<Goods,String>("columnColor")); columnYear.setCellValueFactory(new PropertyValueFactory<Goods,String>("columnYear")); columnPrice.setCellValueFactory(new PropertyValueFactory<Goods,Float>("columnYear")); columnCount.setCellValueFactory(new PropertyValueFactory<Goods,Integer>("columnCount")); TableCars.setItems(Cars); } } Goods class:
public class Goods { private Car car; private float price; private int count; public Goods() { this.car = new Car(); this.price = 0; this.count = 0; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } } Car class:
public class Car { private String marka; private String model; private String color; private String year; public String getMarka() { return marka; } public void setMarka(String marka) { this.marka = marka; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } } I can not understand what I'm doing wrong.
new PropertyValueFactory<Goods, Integer>("columnCount")should benew PropertyValueFactory<Goods, Integer>("Count")(or gettergetcolumnCount()should be in theGoods), for the rest by analogy. And in the first four lines instead of theGoodswill still beCar. Read this answer here . - post_zeewPropertyValueFactorymust exactly match the name of the corresponding getter (only withoutget). - post_zeewcolumnMarka.setCellValueFactorycolumnMarkamust match the correspondingfx:idinfxml. - post_zeew