JavaFX使用ComboBox选择填充TableView

时间:2016-08-18 12:20:13

标签: sqlite javafx combobox tableview

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

// MainController

/*
 * 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;
        }
    }

}

1 个答案:

答案 0 :(得分:-1)

new WebDriverWait(driver, TimeSpan.FromSeconds(10)). Until(ExpectedConditions.PresenceOfAllElementsLocatedBy((By.Id("toast-container")))); 上创建了一个setOnAction事件,只要用户更改了ComboBox上的选项,就可以填充TableView。

ComboBoxSelectionCustomer