Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite JavaFX使用组合框选择填充TableView_Sqlite_Javafx_Combobox_Tableview - Fatal编程技术网

Sqlite JavaFX使用组合框选择填充TableView

Sqlite JavaFX使用组合框选择填充TableView,sqlite,javafx,combobox,tableview,Sqlite,Javafx,Combobox,Tableview,我正在尝试使用名为ComboBoxSelectCustomer的组合框填充名为CustomerTableView的TableView。基本上,用户从组合框内的客户列表中选择一个客户,然后TableView将填充与该客户名称匹配的数据。在一个SQL文件中,每个客户都有多个表,但由于某种原因,当我从组合框中选择一个客户名称时,TableView上什么也没有发生,它只是保持为空。代码没有错误或任何问题,我只是认为它的设置方式是导致问题的原因。请检查我的代码,并建议我如何更好地设置这些方法,使Combo

我正在尝试使用名为ComboBoxSelectCustomer的组合框填充名为CustomerTableView的TableView。基本上,用户从组合框内的客户列表中选择一个客户,然后TableView将填充与该客户名称匹配的数据。在一个SQL文件中,每个客户都有多个表,但由于某种原因,当我从组合框中选择一个客户名称时,TableView上什么也没有发生,它只是保持为空。代码没有错误或任何问题,我只是认为它的设置方式是导致问题的原因。请检查我的代码,并建议我如何更好地设置这些方法,使ComboBox触发SQL语句来填充TableView

//主控制器

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package supremeinkcalcmk2;

import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javax.swing.DefaultComboBoxModel;

/**
 * FXML Controller class
 *
 */
public class MainController implements Initializable {

    @FXML
    public ComboBox<String> ComboBoxSelectCustomer;
    @FXML
    private TableView CustomerTableView;
    @FXML
    private TableColumn<BaseColor, String> BaseColor;
    @FXML
    private TableColumn<BaseColor, String> Price;
    Connection connection;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO

        //Customer combo box
        ComboBoxSelectCustomer.setOnAction(e -> System.out.println(ComboBoxSelectCustomer.getValue()));
        buildDataComboBox();
        buildDataTableView();

    }

    public void buildDataTableView() {
        //viewtable db connect
        ObservableList<BaseColor> dataCustomerViewTable = FXCollections.observableArrayList();
        BaseColor.setCellValueFactory(new PropertyValueFactory<BaseColor, String>("BaseColor"));
        Price.setCellValueFactory(new PropertyValueFactory<BaseColor, String>("Price"));
        connection = SqlConnection.CustomerConnection();
        try {
//problem
            String SQL = "Select BaseColor, Price FROM " + ComboBoxSelectCustomer.getValue();
            connection = SqlConnection.CustomerConnection();
            ResultSet rs = connection.createStatement().executeQuery(SQL);
            while (rs.next()) {
                BaseColor BS = new BaseColor();
                BS.BaseColor.set(rs.getString("BaseColor"));
                BS.Price.set(rs.getString("Price"));
                dataCustomerViewTable.add(BS);
            }
            CustomerTableView.setItems(dataCustomerViewTable);
        } catch (Exception e) {
        }
    }

    //combobox sql connection and fill data
    public void buildDataComboBox() {
        ObservableList<String> dataComboBox = FXCollections.observableArrayList();
        connection = SqlConnection.CustomerConnection();
        try {
            String SQL = "Select Name From CustomerList";
            ResultSet rs = connection.createStatement().executeQuery(SQL);
            while (rs.next()) {
                dataComboBox.add(rs.getString("Name"));
            }
            ComboBoxSelectCustomer.setItems(dataComboBox);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error Building ComboBox Data");
        }
        if (connection == null) {
            System.exit(1);
            System.out.println("Connection failed");
        }
    }

    public boolean isDbConnected() {
        try {
            return connection.isClosed();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

}

在ComboBox SelectionCustomer上创建了一个setOnAction事件,该事件允许用户在更改ComboBox上的选项时填充TableView

    ComboBoxSelectCustomer.setOnAction((event) -> {
});

您的意思是将对buildDataTableView的调用放入事件处理程序中吗?另外,不要抑制buildDataTableView中的异常:如果出现问题,您无法知道发生了什么。您尝试对ComboBoxSelectCustomer中的更改执行某些操作,但您添加的唯一侦听器是onAction侦听器,这不符合您的要求,可能会替换从fxml添加的侦听器。此外,代码中显然建立的与数据库的唯一连接似乎是通过initialize方法完成的。从数据库加载数据时,您对任何用户交互的实际反应是什么?也可以直接访问BaseColor的字段修改属性让我头疼。您是否实现了PropertyValueFactory工作所必需的方法?