This is code mainController for my fxml form. Image in tableview work, but some strangeable.
Add first row - ok.
Add second: watch image in second from the start and first from the end.
Add third: watching in third row from the start and fourth from the end... etc..
What is this? data no add in those rows, but image adding. Where is problem?
package cardiler;
import java.io.IOException;
import java.net.URL;
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.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;
import viewpages.AddFilterWindowController;
public class MainWindowController {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
// Фильтры ------------------------------------------------ //
@FXML
private TableView<CarFilters> filtersTable;
@FXML
private TableColumn<CarFilters, String> firmColumn;
@FXML
private TableColumn<CarFilters, String> modelColumn;
private ObservableList<CarFilters> filtersData = FXCollections.observableArrayList();
// Результаты поиска -------------------------------------- //
@FXML
private TableView<CarResult> scanResultsTable;
@FXML
private TableColumn<CarResult, String> firmRTColumn;
@FXML
private TableColumn<CarResult, String> modelRTColumn;
@FXML
private TableColumn<CarResult, CarPhoto> photoRTColumn;
private ObservableList<CarResult> resultsData = FXCollections.observableArrayList();
@FXML
void handleAddFilterButton(ActionEvent event) {
try {
// Load the fxml file and create a new stage for the popup
FXMLLoader loader = new FXMLLoader(CarDiler.class.getResource("/viewpages/AddFilterWindow.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Person");
dialogStage.initModality(Modality.WINDOW_MODAL);
// dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the person into the controller
AddFilterWindowController controller = loader.getController();
controller.setDialogStage(dialogStage);
CarFilters carFilter = new CarFilters();
controller.setCarFilterLink(carFilter);
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
if (controller.isOkClicked())
{
filtersData.add(carFilter);
}
// return controller.isOkClicked();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
// return false;
}
}
@FXML
void handleSearchBuyers(ActionEvent event) {
}
@FXML
void handleSearchSellers(ActionEvent event) {
}
@FXML
void handleStartButton(MouseEvent event) {
resultsData.add(new CarResult("ddd", "sss", new CarPhoto("ss", "dd", "/resources/images/start_button.png")));
scanResultsTable.setItems(resultsData);
}
@FXML
void initialize() {
// Результат --------------------------------------------------------------------- //
assert scanResultsTable != null : "fx:id=\"scanResultsTable\" was not injected: check your FXML file 'MainWindow.fxml'.";
firmRTColumn.setCellValueFactory(new PropertyValueFactory<CarResult, String>("firm"));
modelRTColumn.setCellValueFactory(new PropertyValueFactory<CarResult, String>("model"));
photoRTColumn.setCellValueFactory(new PropertyValueFactory<CarResult, CarPhoto>("photo"));
// SETTING THE CELL FACTORY FOR THE ALBUM ART
photoRTColumn.setCellFactory(new Callback<TableColumn<CarResult,CarPhoto>,
TableCell<CarResult,CarPhoto>>(){
@Override
public TableCell<CarResult, CarPhoto> call(TableColumn<CarResult, CarPhoto> param) {
return new TableCell<CarResult, CarPhoto>(){
ImageView imageview;
{
imageview = new ImageView();
imageview.setFitHeight(20);
imageview.setFitWidth(20);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(imageview);
}
@Override
public void updateItem(CarPhoto item, boolean empty) {
super.updateItem(item, empty);
if(!empty && !item.equals(null)){
imageview.setImage(new Image(item.getPhoto()));
}
System.out.println(item);
}
};
}
});
scanResultsTable.setItems(resultsData);
// Филтры ------------------------------------------------------------------------ //
assert filtersTable != null : "fx:id=\"filtersTable\" was not injected: check your FXML file 'MainWindow.fxml'.";
// Маппинг колонок с полями класса потомка
firmColumn.setCellValueFactory(new PropertyValueFactory<CarFilters, String>("firm"));
modelColumn.setCellValueFactory(new PropertyValueFactory<CarFilters, String>("model"));
filtersTable.setItems(filtersData);
}
}
The tableview and listview cells are reused to render different row data of the source list. Your problem probably lays on the updating of the cell item. Try this: