Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.beans.property.SimpleIntegerProperty;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import org.w3c.dom.events.MouseEvent;
- import java.io.IOException;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ResourceBundle;
- import static sample.Main.getConnection;
- public class BookScreenController implements Initializable{
- public static Connection conn;
- public static ResultSet rs;
- private static ObservableList<Book> bookList;
- @FXML
- public static TableView<Book> bookTable = new TableView<>();
- public static TableColumn<Book, String> title = new TableColumn<>();
- public static TableColumn<Book, String> authorFirstName = new TableColumn<>();
- public static TableColumn<Book, String> authorLastName = new TableColumn<>();
- public static TableColumn<Book, Integer> isbn = new TableColumn<>();
- public static TableColumn<Book, Integer> numberOfPages = new TableColumn<>();
- @FXML
- private void handleDisplayTables(ActionEvent event) throws NullPointerException {
- try {
- conn = getConnection();
- PreparedStatement statement = conn.prepareStatement("SELECT * FROM BOOKS");
- rs = statement.executeQuery();
- bookList = FXCollections.observableArrayList();
- while (rs.next()) {
- String title = rs.getString("title");
- String authorFirstName = rs.getString("authorFirstName");
- String authorLastName = rs.getString("authorLastName");
- int isbn = rs.getInt("isbn");
- int numberOfPages = rs.getInt("numberOfPages");
- bookList.add(new Book(title, authorFirstName, authorLastName,isbn, numberOfPages));
- System.out.println(title + " " + authorFirstName + " " + authorLastName + " " + isbn + " " + numberOfPages);
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- // title.setCellValueFactory(new PropertyValueFactory<>("title"));
- // isbn.setCellValueFactory(new PropertyValueFactory<>("isbn"));
- // authorFirstName.setCellValueFactory(new PropertyValueFactory<>("authorFirstName"));
- // authorLastName.setCellValueFactory(new PropertyValueFactory<>("authorLastName"));
- // numberOfPages.setCellValueFactory(new PropertyValueFactory<>("numberOfPages"));
- // title.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getTitle()));
- // authorFirstName.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAuthorFirstName()));
- // authorLastName.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAuthorLastName()));
- // isbn.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getIsbn()).asObject());
- // numberOfPages.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getNumberOfPages()).asObject());
- title.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Book)cellData.getValue()).getTitle()));});
- authorFirstName.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Book)cellData.getValue()).getAuthorFirstName()));});
- authorLastName.setCellValueFactory(cellData -> { return (new SimpleStringProperty(((Book)cellData.getValue()).getAuthorLastName()));});
- isbn.setCellValueFactory(cellData -> { return (new SimpleIntegerProperty(((Book)cellData.getValue()).getIsbn())).asObject();});
- numberOfPages.setCellValueFactory(cellData -> { return (new SimpleIntegerProperty(((Book)cellData.getValue()).getNumberOfPages())).asObject();});
- bookTable.setItems(bookList);
- bookTable.getColumns().addAll(title,authorFirstName,authorLastName,isbn,numberOfPages);
- bookTable.refresh();
- }
- public void closeApplication(ActionEvent event) throws Exception{
- System.exit(0);
- }
- @Override
- public void initialize(URL url, ResourceBundle resourceBundle) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement