-1
我想用名爲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;
}
}
}
您是不是打算在事件處理程序中調用'buildDataTableView'?另外,不要在'buildDataTableView'中抑制異常:如果出現問題,你無法知道什麼。 –
您嘗試在ComboBoxSelectCustomer中進行更改,但添加的唯一偵聽器是onAction偵聽器,它不會執行您想要的操作,並可能會替換從fxml添加的偵聽器。此外,顯然在代碼中建立的數據庫似乎是在'initialize'方法中完成的。你在哪裏實際上對任何用戶與從數據庫加載數據的交互作出反應?還直接訪問'BaseColor'的字段來修改屬性給出我headaches.Have你實現了'PropertyValueFactory'的工作需要的方法? – fabian