Здравствуйте!
В последнем методе контроллера возникает ошибка:
'setCellFactory(javafx.util.Callback,javafx.scene.control.TableCell>)' in 'javafx.scene.control.TableColumn' cannot be applied to '(javafx.scene.control.cell.PropertyValueFactory)'
Подскажите, пожалуйста, как её исправить?
package com.example.demo1;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class CRUDController {
@FXML
public TextField tfId;
@FXML
public TextField tfTitle;
@FXML
public TextField tfAuthor;
@FXML
public TextField tfYear;
@FXML
public TextField tfPages;
@FXML
public TableView<Books> tvBooks;
@FXML
public TableColumn<Books, Integer> colId;
@FXML
public TableColumn<Books, String> colTitle;
@FXML
public TableColumn<Books, String> colAuthor;
@FXML
public TableColumn<Books, Integer> colYear;
@FXML
public TableColumn<Books, Integer> colPages;
@FXML
public Button btnInsert;
@FXML
public Button btnUpdate;
@FXML
public Button btnDelete;
@FXML
public void handleButtonInsertAction(ActionEvent actionEvent) {
}
@FXML
public void handleButtonUpdateAction(ActionEvent actionEvent) {
}
@FXML
public void handleButtonDeleteAction(ActionEvent actionEvent) {
}
public Connection getConnection(){
Connection conn;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root","12345");
return conn;
}catch(Exception ex){
System.out.println("Error: "+ex.getMessage());
return null;
}
}
public ObservableList<Books> getBooksList(){
ObservableList<Books> bookList = FXCollections.observableArrayList();
Connection conn = getConnection();
String query = "SELECT * FROM books";
Statement st;
ResultSet rs;
try{
st = conn.createStatement();
rs = st.executeQuery(query);
Books books;
while(rs.next()){
books = new Books(rs.getInt("id"),
rs.getString("title"),
rs.getString("author"),
rs.getInt("year"),
rs.getInt("pages")
);
bookList.add(books);
}
}catch (Exception ex){
ex.printStackTrace();
}
return bookList;
}
public void showBooks(){
ObservableList<Books> list = getBooksList();
colId.setCellFactory(new PropertyValueFactory<Books,Integer>("id")); //ОШИБКА
colTitle.setCellFactory(new PropertyValueFactory<Books,String>("title")); //ОШИБКА
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?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.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:controller="com.example.demo1.CRUDController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="415.0" prefWidth="690.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox layoutX="14.0" layoutY="14.0" prefHeight="178.0" prefWidth="66.0" spacing="15.0">
<children>
<Label text="ID">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Title">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Author">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Year">
<font>
<Font size="14.0" />
</font>
</Label>
<Label text="Pages">
<font>
<Font size="14.0" />
</font>
</Label>
</children>
</VBox>
<VBox layoutX="80.0" layoutY="14.0" prefHeight="200.0" prefWidth="140.0" spacing="9.0">
<children>
<TextField fx:id="tfId" />
<TextField fx:id="tfTitle" />
<TextField fx:id="tfAuthor" />
<TextField fx:id="tfYear" />
<TextField fx:id="tfPages" />
</children>
</VBox>
<TableView fx:id="tvBooks" layoutX="231.0" layoutY="14.0" prefHeight="250.0" prefWidth="378.0">
<columns>
<TableColumn fx:id="colId" prefWidth="75.0" text="ID" />
<TableColumn fx:id="colTitle" prefWidth="75.0" text="TITLE" />
<TableColumn fx:id="colAuthor" prefWidth="75.0" text="Author" />
<TableColumn fx:id="colYear" prefWidth="75.0" text="Year" />
<TableColumn fx:id="colPages" prefWidth="75.0" text="Pages" />
</columns>
</TableView>
<Button fx:id="btnInsert" layoutX="28.0" layoutY="238.0" mnemonicParsing="false" onAction="#handleButtonInsertAction" text="Insert" />
<Button fx:id="btnUpdate" layoutX="98.0" layoutY="238.0" mnemonicParsing="false" onAction="#handleButtonUpdateAction" text="Update" />
<Button fx:id="btnDelete" layoutX="168.0" layoutY="238.0" mnemonicParsing="false" onAction="#handleButtonDeleteAction" text="Delete" />
</children>
</AnchorPane>