People Table - TableView ExampleJavaFX: как распознать текущую строку в TableView?
Я построил TableView в JavaFX, как показано на рисунке (Таблица людей), состоящий из списка имен. Как я могу напечатать первое и фамильное имя на консоли в соответствующей строке при каждом нажатии кнопки «details»?
(Для справки моя конечная цель и исходный неотвеченный вопрос - для кнопки сведений, чтобы открыть сцену шаблона и заполнить ее данными для этого конкретного человека).
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.util.Callback;
public class cutExample extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(final Stage stage) {
stage.setTitle("People");
// create a table.
final TableView<Person> table = new TableView<>(
FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams"),
new Person("Emma", "Jones"),
new Person("Michael", "Brown")
)
);
// define the table columns.
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
TableColumn<Person, Boolean> actionCol = new TableColumn<>("Action");
actionCol.setSortable(false);
// create a cell value factory with a details button for each row in the table.
actionCol.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {
@Override public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> personBooleanTableColumn) {
return new AddPersonCell(stage, table);
}
});
table.getColumns().setAll(firstNameCol, lastNameCol, actionCol);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
stage.setScene(new Scene(table));
stage.show();
}
/** A table cell containing a button for details on each person. */
private class AddPersonCell extends TableCell<Person, Boolean> {
// a button for a specific person's details
final Button details = new Button("Details");
// pads and centers the button in the cell.
final StackPane paddedButton = new StackPane();
/**
* AddPersonCell constructor
* @param stage the stage in which the table is placed.
* @param table the table to which a new person can be added.
*/
AddPersonCell(final Stage stage, final TableView table) {
paddedButton.setPadding(new Insets(3));
paddedButton.getChildren().add(details);
}
/** places an details button in the row only if the row is not empty. */
@Override protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(paddedButton);
} else {
setGraphic(null);
}
}
}
}
Отлично, спасибо большое за помощь! – Hans