0

这是我的 fxml 表单的代码 mainController。表格视图中的图像工作,但有些奇怪。
添加第一行 - 好的。
添加第二个:从头到尾的第二个观看图像。
添加第三个:从头开始看第三排,从最后看第四排……等等。
这是什么?这些行中没有添加数据,但添加了图像。问题出在哪里?

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);
    }

}
4

1 回答 1

1

重用 tableview 和 listview 单元格来呈现源列表的不同行数据。您的问题可能在于单元格项目的更新。尝试这个:

@Override
public void updateItem(CarPhoto item, boolean empty) {
    super.updateItem(item, empty);
    if (!empty && !item.equals(null)) {
        imageview.setImage(new Image(item.getPhoto()));
    }
    else {
        imageview.setImage(null);
    }
    System.out.println(item);
}
于 2013-08-23T12:24:32.230 回答