私はJavaFXの新機能です。開発しようとしているアプリケーションには、ProductOverviewController
とCartOverviewController
の2つのコントローラがあります。Java FXのテーブルビューにデータを取り込めません
ProductOverviewController
はCartOverviewController
の表に表示されることになるCartOverviewController
ObservableList
(トラフUser
オブジェクト)に送信します。
CartOverviewController
はObservablelist
(私はオブザーバブルリストに含まれているオブジェクトの名前を良好な結果で印刷しようとしました)を受け取っているようですが、プログラムを実行するとテーブルが空です。
私はすでにこの問題についてこれまでのすべての議論を読みましたが、私はこの問題を解決できませんでした。 これはコードです:
1)ProductOverviewController
public class ProductOverviewController {
public static User user;
public static Product product;
private MainApp mainApp;
//FXML tag is used to link view controller to the view
@FXML
private TableView<Product> productTable;
@FXML
private TableColumn<Product, String> productNameColumn;
@FXML
private TableColumn<Product, Double> productPriceColumn;
@FXML
private Label idLabel;
@FXML
private Label productNameLabel;
@FXML
private Label productPriceLabel;
@FXML
private Label productInfoLabel;
@FXML
private Label userLabel;
/*
* Reference to the main app
*/
//constructor
public ProductOverviewController(){
}
//initializer. automatically called after fxml has been loaded
@FXML
private void initialize() throws SQLException, IOException{
//Initialize Product table with 2 column
productNameColumn.setCellValueFactory(cellData -> cellData.getValue().productNameProperty());
productPriceColumn.setCellValueFactory(cellData -> cellData.getValue().productPriceProperty().asObject());
/*THIS CODE IS FOR SYNCRONIZING
*
*/
//Clear person details
showProductDetails(null);
//Listen for changes and show details
productTable.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue)-> {
try {
showProductDetails(newValue);
product = newValue;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
//is called by the main app to give a reference back to itself
public void setMainApp(MainApp mainApp){
this.mainApp = mainApp;
//add observable list to the table
productTable.setItems(mainApp.getProductData());
}
//fill all text field with products'data in the right table
private void showProductDetails(Product product) throws SQLException, IOException{
if(product != null){
//fill table's labels
idLabel.setText(Integer.toString(product.getIdProduct()));
productNameLabel.setText(product.getProductName());
productPriceLabel.setText(Double.toString(product.getProductPrice()));
productInfoLabel.setText(product.getProductInfo());
if(user != null && user.isLogin() == true){
userLabel.setText(user.getUsername());
}else if(user.isLogin() == false){
userLabel.setText("Guest");
}
}else{
idLabel.setText("");
productNameLabel.setText("");
productPriceLabel.setText("");
productInfoLabel.setText("");
}
}
@FXML
public void handleViewChart() throws IOException, SQLException{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CartOverview.fxml"));
Parent root;
try{
root = fxmlLoader.load();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
((CartOverviewController)fxmlLoader.getController()).setUser(user);
stage.show();
}catch(IOException e){
e.printStackTrace();
}
}
@FXML
private void handleAddToCart(){
//send to CartOverviewControler user with the dispaly list and print for debug
user.addToCart(product);
System.out.println(user.getCartList().get(0).getProductName());
}
}
2)CartOverviewController
public class CartOverviewController implements Initializable {
public User user;
private ObservableList<Product> cart = FXCollections.observableArrayList();
@FXML
private TableView<Product> productTable;
@FXML
private TableColumn<Product, String> productNameColumn;
@FXML
private TableColumn<Product, Double> productPriceColumn;
@FXML
private TableColumn<Product, Double> productAmountColumn;
public void setUser(User user) throws SQLException, IOException{
this.user = user;
this.cart = this.user.getCartList();
System.out.println(this.cart.get(0).getProductName());
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
//Method 1
/*
productNameColumn.setCellValueFactory(new PropertyValueFactory<Product, String> ("productName"));
productPriceColumn.setCellValueFactory(new PropertyValueFactory<Product, Double> ("productPrice"));
productAmountColumn.setCellValueFactory(new PropertyValueFactory<Product, Double> ("productAmount"));
*/
//Method 2
productNameColumn.setCellValueFactory(cellData -> cellData.getValue().productNameProperty());
productPriceColumn.setCellValueFactory(cellData -> cellData.getValue().productPriceProperty().asObject());
productAmountColumn.setCellValueFactory(cellData -> cellData.getValue().productPriceProperty().asObject());
//added this for debug
if(cart != null && productTable != null)
productTable.setItems(cart);
else if(cart == null)
System.out.println("cart is null");
else if(productTable == null)
System.out.println("table is null");
else
System.out.println("else is null");
}
}
誰も私を助けることができますか?
ああありがとう!今それは動作します:D – Knospfer
完了、申し訳ありません私もスタックオーバーフローで新しいです:) – Knospfer