1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package test.tabledetails;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.util.Objects;
public final class Main extends Application {
private TableView<String> tableView;
private Pane infoPane;
private String lastID;
@Override
public void start(final Stage stage) throws Exception {
final var idColumn = new TableColumn<String, String>("ID");
idColumn.setCellValueFactory(feature -> new SimpleStringProperty(feature.getValue()));
final var valueColumn = new TableColumn<String, String>("Value");
tableView = new TableView<String>();
tableView.getItems().setAll("qqq", "www", "eee", "rrr", "ttt", "yyy");
tableView.setRowFactory(stringTableView -> new TableRow<>() {
{
addEventFilter(MouseEvent.MOUSE_CLICKED, this::mouseHandler);
}
private void mouseHandler(final MouseEvent event) {
if (event.getClickCount() >= 2) {
final var item = getItem();
showSidePane(item);
}
}
});
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY_ALL_COLUMNS);
tableView.getColumns().setAll(idColumn, valueColumn);
infoPane = new Pane();
infoPane.setMinWidth(200);
final var root = new SplitPane();
root.getItems().setAll(tableView, infoPane);
final var scene = new Scene(root);
stage.setTitle("Test");
stage.setWidth(800);
stage.setHeight(600);
stage.setScene(scene);
stage.show();
Platform.runLater(() -> root.setDividerPositions(0.75));
}
private void showSidePane(final String id) {
if (Objects.isNull(lastID) || !lastID.equals(id)) {
lastID = id;
System.out.printf("showSidePane(%s)%n", id);
if (Objects.isNull(id)) {
infoPane.getChildren().clear();
} else {
final var label = new Label(id);
infoPane.getChildren().setAll(label);
}
}
}
} |
Partager