使用JavaFX,在tableview中显示hashmap的内容

时间:2018-04-17 22:54:47

标签: hashmap javafx-8 observablecollection

我有一个TableView我希望在其中显示存储在HashMap<Integer, CustomClass>中的设备的功能和属性列表,这些内容会在新项目添加到HashMap时更新。
我已经尝试了不同的方法来显示数据但是不成功,并查看其他问题中的示例,但它似乎不起作用。也许我很困惑,因为我的javaFX布局使用的FXML和控制器类方法与大多数示例不匹配 谁能帮助指出我做错了什么?

这是控制器中的代码:

public class MainController implements Initializable {

    @FXML 
    private ToggleButton activateBtn;
    @FXML
    private Label dispTime;
    @FXML
    private Button timeControl;
    @FXML
    private ChoiceBox paceNum;
    @FXML
    private TableView<Map> trainTable;
    @FXML
    private TableColumn trainIdCol;
    @FXML
    private TableColumn speedCol;
    @FXML
    private TableColumn authCol;
    @FXML
    private TableColumn trainTimeCol;
    @FXML
    private TableColumn driverCol;
    @FXML
    private TableColumn driverTimeCol;
    @FXML
    private TableColumn lineCol;
    @FXML
    private TableColumn directionCol;
    @FXML
    private TableColumn locColX;
    @FXML
    private TableColumn locColY;
    @FXML
    private TableColumn destCol;
    @FXML
    private TableColumn timeToCol;
    @FXML
    private TextArea ta;
    @FXML
    private MenuItem closeButton;


    /*
     * get all the trains to display
     */
    static final ObservableList<HashMap<Integer, MboTrain>> trainList = FXCollections.observableArrayList(MboTrain.trainList);



    @Override
    public void initialize(URL url, ResourceBundle rb) {
        reportData(">System initializing...");


        /*
         * Identify which train values to list in the train table
         */
        trainIdCol.setCellValueFactory(new MapValueFactory("trainId"));
        speedCol.setCellValueFactory(new MapValueFactory("speed"));
        authCol.setCellValueFactory(new MapValueFactory("authority"));
        trainTimeCol.setCellValueFactory(new MapValueFactory("trainTime"));
        driverCol.setCellValueFactory(new MapValueFactory("driverId"));
        driverTimeCol.setCellValueFactory(new MapValueFactory("driverTime"));
        lineCol.setCellValueFactory(new MapValueFactory("line"));
        directionCol.setCellValueFactory(new MapValueFactory("direction"));
        locColX.setCellValueFactory(new MapValueFactory("currentLoc"));
        locColY.setCellValueFactory(new MapValueFactory("currentLoc"));
        destCol.setCellValueFactory(new MapValueFactory("nextLoc"));
        timeToCol.setCellValueFactory(new MapValueFactory("timeToNext"));

        /*
         * mark the columns as Map read values
         */
        Callback<TableColumn<Map, String>, TableCell<Map, String>>
                cellFactoryForMap = null;
        trainIdCol.setCellFactory(cellFactoryForMap);
        speedCol.setCellFactory(cellFactoryForMap);
        authCol.setCellFactory(cellFactoryForMap);
        trainTimeCol.setCellFactory(cellFactoryForMap);
        driverCol.setCellFactory(cellFactoryForMap);
        driverTimeCol.setCellFactory(cellFactoryForMap);
        lineCol.setCellFactory(cellFactoryForMap);
        directionCol.setCellFactory(cellFactoryForMap);
        locColX.setCellFactory(cellFactoryForMap);
        locColY.setCellFactory(cellFactoryForMap);
        destCol.setCellFactory(cellFactoryForMap);
        timeToCol.setCellFactory(cellFactoryForMap);
        /*
         * call the update method to have the MBO pull a list of active trains into the table
         */
        try {
            MboTrain.updateTrainList();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*
         * establish table properties and get the data for the columns
         */
        trainTable.setEditable(false);
        trainTable.getSelectionModel().setCellSelectionEnabled(false);
        trainTable.getColumns().setAll(trainIdCol, speedCol, authCol, trainTimeCol, driverCol, driverTimeCol,
                lineCol, directionCol, locColX, locColY, destCol, timeToCol);

        /*
         * run the callback method to have the columns callback to a Map for data
         */
        cellFactoryForMap = new Callback<TableColumn<Map, String>,
                TableCell<Map, String>>() {
            @Override
            public TableCell call(TableColumn p) {
                return new TextFieldTableCell(new StringConverter() {
                    @Override
                    public String toString(Object t) {
                        return t.toString();
                    }

                    @Override
                    public Object fromString(String string) {
                        return string;
                    }
                });
            }
        };
    }

自定义类对象:

public class MboTrain {

    /*
     * list of trains created in the system
     */
    private static ArrayList<Integer> trainListPull;
    public static HashMap<Integer, MboTrain> trainList;

    private Integer trainId;
    private double speed;
    private double authority;
    private long trainStartTime;
    private long trainTime;
    private int driverId;
    private long driverTime;
    private LineId line;
    private Boolean direction;
    private String directionString;
    private double currentLocX;
    private double currentLocY;
    private Block nextBlock;
    private Station nextStation;
    private Block nextStationBlock;
    private long timeToNext;


    private Block[] track;
    private Route route;
    private Block destinationBlock;
    private Block currentBlock;


    /*
     * generates a default train object and places it in the map
     */
    public MboTrain(LineId m_line) throws IOException {
        int pId = createTrain("MboCreatedTrain");
        placeTrainOnLine(pId, m_line);
        this.line = m_line;
        this.trainId = pId;
        this.speed = 0;
        this.authority = 0;
        this.trainStartTime = MboClock.updateTime();
        this.trainTime = 0;
        this.driverId = 0;
        this.driverTime = 0;
        this.line = m_line;
        this.direction = true;
        this.directionString = "outbound";
        this.currentLocX = 0;
        this.currentBlock = MboTrack.getYard(m_line);
        this.nextBlock = getNextBlock();
        this.nextStationBlock = this.nextBlock;
        this.setNextStation();
        this.timeToNext = 0;
        this.route = null;
        if (trainList == null)
        {
            trainList = new HashMap<>();
        }
        trainList.put(pId,this);
    }

    public static boolean createMboTrain(LineId m_line) throws IOException
    {
        MboTrain m_train = new MboTrain(m_line);
        if (m_train == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /*
     * update the list of available trains from train model handler
     */
    public static void updateTrainList() throws IOException {
        trainListPull = getTrainList();
        LineId line;
        MboTrain m_train;
        trainList = new HashMap<>();
        for (Integer tid : trainListPull) {
            System.out.println("well, we're checking the trainlist map for "+tid);
            if (trainList.containsKey(tid))
            {
                // do nothing since train already in map
                System.out.println("doing nothing - already in map");
            }
            else
            {
                if (trainLineMap.containsKey(tid)) {
                    System.out.println("train is on a line - creating train on a line and adding to map");
                    line = trainLineMap.get(tid);
                    m_train = new MboTrain(line);
                    //m_train.getCurrentLoc();
                    trainList.put(tid, m_train);
                    System.out.println("success - we've added train "+tid+" on line "+line.toString()+" to the map");
                } else {
                    System.out.println("train isn't on a line - so creating train with null lineId and adding to map");
                    m_train = new MboTrain(null);
                    trainList.put(tid, m_train);
                    System.out.println("success - we've added train "+tid+" to the map with line=null");
                }
            }
        }
    }

任何帮助表示赞赏!!

1 个答案:

答案 0 :(得分:1)

我意识到我只是对这一切都错了。所以我将遵循James_D的建议,看看只使用地图的ObservableList表示并显示该列表。我打算将其关闭,所以我不会浪费任何人的时间 感谢