JavaFX ObservableArrayList没有输出

时间:2018-04-30 04:30:34

标签: javafx tableview

我很难将observableArrayList打印到TableView for JavaFX上。代码上没有错误,它没有给我任何具体的工作。代码将运行但不会出现输出。我使用outprint进行检查,它似乎在try语句之前就停止了。这是我第一次使用FX,所以任何帮助都会受到赞赏。

申请类:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main extends Application {

    Stage window;
    TableView<Product> table;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("List");
        Connection connection = getConnection();

        //Name column
        TableColumn<Product, String> code = new TableColumn<>("code");
        code.setMinWidth(200);
        code.setCellValueFactory(new PropertyValueFactory<>("code"));

        //Price column
        TableColumn<Product, Double> price = new TableColumn<>("Price");
        price.setMinWidth(100);
        price.setCellValueFactory(new PropertyValueFactory<>("price"));

        //Quantity column
        TableColumn<Product, String> description = new TableColumn<>("description");
        description.setMinWidth(100);
        description.setCellValueFactory(new PropertyValueFactory<>("description"));
        getConnection(); 
        table = new TableView<>();
        table.setItems(getProduct());
        table.getColumns().addAll(code, description, price);

        VBox vBox = new VBox();
        vBox.getChildren().addAll(table);

        Scene scene = new Scene(vBox);
        window.setScene(scene);
        window.show();
    }

    private Connection getConnection() throws SQLException {
        String dbUrl = "jdbc:sqlite:products.sqlite";
        Connection connection = DriverManager.getConnection(dbUrl);
        return connection;
    }

    //Get all of the products
    public ObservableList<Product> getProduct(){
        String sql = "SELECT ProductCode, Description, Price "
                  + "FROM Products ORDER BY ProductCode ASC";
        ObservableList<Product> products = FXCollections.observableArrayList();
        try (Connection connection = getConnection();
             PreparedStatement ps = connection.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {          
            while (rs.next()) {
                String code = rs.getString("ProductCode");
                String description = rs.getString("Description");
                double price = rs.getDouble("Price");

                Product p = new Product(code, description, price);
                products.add(p);
            }

        } catch (SQLException e) { 
        }
        return products;
    }
}

产品类别:

import java.text.NumberFormat;

public class Product {

    private String name;
    private double price;
    private int quantity;
    private String code;
    private String description;

    public Product(){
        this.code = "";
        this.description = "";
        this.price = 0;
    }

    public Product(String code, double price, String description){
        this.code = code;
        this.price = price;
        this.description = description;
    }

    public Product(String code, String description, double price) {
        this.code = code;
        this.description = description;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public String getPriceFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(price);
    } 
}

0 个答案:

没有答案