JavaFX:如何将TextField与ObservableList绑定并将这些数据更新到tableview中?

时间:2016-06-24 19:39:29

标签: java javafx java-8 javafx-2 javafx-8

尝试使用JavaFx制作字典。 这是我的Controller类 -

public class MainUIController implements Initializable {

    DatabaseManager db=new DatabaseManager();
    ObservableList<OvidhanMeaning> MeaningList;

    @FXML
    private TextField searchField;

    @FXML
    private ImageView searchIcon;

    @FXML
    private ImageView aboutIcon;

    @FXML
    private TableView<OvidhanMeaning> ovidhanTable;

    @FXML
    private TableColumn<OvidhanMeaning, String> englishCol;

    @FXML
    private TableColumn<OvidhanMeaning, String> banglaCol;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Image search = new Image(getClass().getResource("/images/search.png").toString(), true);
        Image about = new Image(getClass().getResource("/images/about.png").toString(), true);
        //Font bnFont = Font.loadFont(getClass().getResource("/fonts/Siyamrupali.ttf").toExternalForm(), 12);
        Font bnFont = Font.loadFont(getClass().getResourceAsStream("/fonts/Siyamrupali.ttf"), 12);

        searchIcon.setImage(search);
        aboutIcon.setImage(about);

        db.Connect("jdbc:sqlite::resource:ankurdb/bn_words.db");
        ResultSet rs = db.GetResult("select en_word,bn_word from words");
        MeaningList=FXCollections.observableArrayList();
        try {
            while(rs.next())
            {
                String enword = rs.getString("en_word");
                String bnword = rs.getString("bn_word");

                MeaningList.add(new OvidhanMeaning(enword,bnword));

                englishCol.setCellValueFactory(new PropertyValueFactory<OvidhanMeaning, String>("enword"));
                banglaCol.setCellValueFactory(new PropertyValueFactory<OvidhanMeaning, String>("bnword"));
                ovidhanTable.setItems(MeaningList);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println(MeaningList.size());

    }
}

这是我加载数据的模型类 -

public class OvidhanMeaning {
    private SimpleStringProperty enword;
    private SimpleStringProperty bnword;

    public OvidhanMeaning(String enword, String bnword) {
        this.enword = new SimpleStringProperty(enword);
        this.bnword = new SimpleStringProperty(bnword);
    }

    public String getenword() {
        return enword.get();
    }

    public SimpleStringProperty enwordProperty() {
        return enword;
    }

    public String getbnword() {
        return bnword.get();
    }

    public SimpleStringProperty bnwordProperty() {
        return bnword;
    }
}

现在我正在尝试将searchField与加载的MeaningList绑定,并将绑定的数据填充到ovidhanTable。 我怎么能这样做?

0 个答案:

没有答案