JavaFX通过使用组合框选择填充TableView
问题描述:
我想用名为ComboBoxSelectCustomer
的组合框填充名为CustomerTableView
的TableView。基本上,用户从ComboBox内部的客户列表中选择一个客户,并且TableView将填充与该客户名称匹配的数据。我在每个客户的SQL文件中有多个表,但由于某种原因,当我从组合框中选择一个客户名时,TableView上没有任何反应,它只是空着。代码没有错误或任何问题,我只是认为它的设置方式是导致问题的原因。请检阅我的代码,并告诉我我如何能以更好的方式设置这些方法后会有组合框触发一个SQL语句来填充的TableViewJavaFX通过使用组合框选择填充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;
}
}
}
答
创建一个setOnAction事件上ComboBoxSelectionCustomer
只要用户更改ComboBox上的选项,就允许TableView填充。
ComboBoxSelectCustomer.setOnAction((event) -> {
});
您是不是打算在事件处理程序中调用'buildDataTableView'?另外,不要在'buildDataTableView'中抑制异常:如果出现问题,你无法知道什么。 –
您尝试在ComboBoxSelectCustomer中进行更改,但添加的唯一侦听器是onAction侦听器,它不会执行您想要的操作,并可能会替换从fxml添加的侦听器。此外,显然在代码中建立的数据库似乎是在'initialize'方法中完成的。你在哪里实际上对任何用户与从数据库加载数据的交互作出反应?还直接访问'BaseColor'的字段来修改属性给出我headaches.Have你实现了'PropertyValueFactory'的工作需要的方法? – f*