我想通過選擇組合框項目從數據庫顯示數據。 Combobox包含我的數據庫中用戶可以選擇的列。我嘗試了這一點,combobox是好的,代碼工作沒有錯誤,但表不更新。JavaFX:通過選定的組合框更新tableview上下文(綁定)
控制器類:
package application;
import java.io.IOException;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class SortareDupaAngajatiController implements Initializable{
final ObservableList<tableAngajati> data=FXCollections.observableArrayList();
final ObservableList<String> optiuni=FXCollections.observableArrayList();
PreparedStatement intabel=null;
ResultSet rezultate=null;
String prequery;
String postquery;
@FXML
public ComboBox<String> meniu;
@FXML
TableView<tableAngajati> tabelangajati;
@FXML
private TableColumn<tableAngajati,String> numesediucol;
@FXML
private TableColumn<tableAngajati,Integer> angajaticol;
@Override
public void initialize(URL location, ResourceBundle resources) {
comboMeniu();
insertInTabel();
}
public void comboMeniu(){
try {
String query = "SELECT * FROM maginfo";
PreparedStatement dindb = Conexiune.conexiuneBd.prepareStatement(query);
ResultSet rezultate = dindb.executeQuery();
while (rezultate.next()) {
optiuni.add(rezultate.getString("nume"));
meniu.setItems(optiuni);
}
dindb.close();
rezultate.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertInTabel(){
try {
prequery="SELECT * FROM sediu where nume='"+meniu.getValue()+"'ORDER BY numarangajati DESC";
postquery=prequery;
intabel= Conexiune.conexiuneBd.prepareStatement(postquery);
rezultate=intabel.executeQuery();
while(rezultate.next()){
data.add(new tableAngajati(
rezultate.getString(2),
rezultate.getInt(7)
));
}
intabel.close();
rezultate.close();
} catch (Exception e) {
System.err.println(e);
}
numesediucol.setCellValueFactory(new PropertyValueFactory<>("numesediu"));
angajaticol.setCellValueFactory(new PropertyValueFactory<>("numarangajati"));
tabelangajati.setItems(null);
tabelangajati.setItems(data);
}}
表類:
package application; public class tableAngajati { private String numesediu; private int angajati; public tableAngajati(String numesediu, int angajati) { super(); this.numesediu=numesediu; this.angajati=angajati; } public String getNumesediu(){ return numesediu; } public void setNumesediu(String numesediu){ this.numesediu=numesediu; } public int getNumarangajati(){ return angajati; } public void setNumarangajati(int angajati){ this.angajati=angajati; } }
非常感謝!
顯示'fxml'文件 –
@mrmcwolf我下面張貼的FXML。謝謝! – AnoGio