Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - JavaFX: TableView: get selected row cell values

I'm working on a project where the TableView looks like this: The TableView

The datas are inside are a MySQL database, and I use a modell and an observableArrayList to get them.

I want to make an initialize method which read the whole selected Line and save it into the TextFields (like Name-Name, Address - Address etc). I tried with this, and now I can Select the Whole Line data, but can't split it into Strings to set the TextField texts:

ListViewerModell details = (ListViewerModell) table.getSelectionModel().getSelectedItem();

//Checking if any row selected
if (details != null) {
//??? - split
editName.setText("*Name*");
editAddress.setText("*Address*");
}

Tried with the modell getName(), getAdress() methods but I got NullPointerException and TablePos methods.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
// create table
TableView<Person> table = new TableView<Person>();

// create columns
TableColumn<Person, String> nameCol = new TableColumn<Person, String>("Name");
nameCol.setCellValueFactory(new PropertyValueFactory("name"));

TableColumn<Person, Address> addressCol = new TableColumn<Person, String>("Address");
addressCol.setCellValueFactory(new PropertyValueFactory("address"));

// add columns 
table.getColumns().setAll(nameCol, addressCol);

// get data from db, return object List<Person> from DB 
ObservableList<Person> persons = getPersonsFromDB();
table.setItems(persons);

tableView.setOnMouseClicked((MouseEvent event) -> {
    if (event.getClickCount() > 1) {
        onEdit();
    }
});

public void onEdit() {
    // check the table's selected item and get selected item
    if (table.getSelectionModel().getSelectedItem() != null) {
        Person selectedPerson = table.getSelectionModel().getSelectedItem();
        nameTextField.setText(selectedPerson.getName());
        addressTextField.setText(selectedPerson.getAddress());
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...