JavaFX只有1部分SimpleStringProperty出现在TableView上

时间:2015-01-03 22:13:33

标签: java javafx javafx-8

我试图在TableView上放一个SimpleStringProperty,这是我正在使用的类:

    package com.launch.history;

import java.net.URL;
import java.util.ResourceBundle;

import com.launch.WebController;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

public class HistoryController implements Initializable {

    @FXML
    private TableView<HistoryClient> tableView;

    @FXML
    private TableColumn<HistoryClient, String> tableColumn;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setTableView();
    }

    public void setColumns() {
        tableColumn.setCellValueFactory(new PropertyValueFactory<HistoryClient, String>("history"));
    }

    public void setTableView() {
        tableView.setItems(getHistory());
        setColumns();
        tableView.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @SuppressWarnings("static-access")
            @Override
            public void handle(MouseEvent event) {
                if(tableView.getSelectionModel().getSelectedIndex() >= 0) {
                    HistoryClient link = tableView.getSelectionModel().getSelectedItem();
                    WebController.engine.load(link.getHistory());
                }
            }
        });
    }

    public ObservableList<HistoryClient> getHistory() {
        ObservableList<HistoryClient> data = FXCollections.observableArrayList();
        data.addAll(new HistoryClient());
        return data;
    }
}

这就是关于历史的东西:

public static String getHistory() {
        return history.get();
    }

    public static void setHistory(String history) {
        HistoryClient.history.set(history);
    }

    private static SimpleStringProperty history = new SimpleStringProperty();

但我只在TableView(最后一个链接)上获得1行

这就是我设定历史价值的方式:

public static void readHistory() {
        if(Files.HISTORY_FILE.exists()) {
            try(BufferedReader in = new BufferedReader(new FileReader(Files.HISTORY_FILE))) {
                String line;
                while( (line = in.readLine()) != null) {
                    history.set(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

工作正常我打印出历史记录,它与文件中的相同,所以我真的没有看到问题,因为上次当我使用TableView时,我做了同样的事情。

是的,我确实在场景构建器中设置了fx:id。

0 个答案:

没有答案