/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javafxtableexample; import java.util.Arrays; import java.util.List; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.StackPane; import javafx.stage.Modality; import javafx.stage.Stage; /** * * @author denis */ public class JavaFXTableExample extends Application { @Override public void start(Stage primaryStage) { List personsList = Arrays.asList( new Person("Andrey", 25, "SPb"), new Person("Ivan", 30, "Moscow"), new Person("Olga", 29, "Tula"), new Person("Ekaterina", 35, "Rostov"), new Person("Oleg", 21, "Vologda")); ObservableList obsPerson = FXCollections.observableArrayList(personsList); TableView table = new TableView<>(obsPerson); TableColumn nameColumn = new TableColumn<>("Имя"); nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); table.getColumns().add(nameColumn); TableColumn ageColumn = new TableColumn<>("Возраст"); ageColumn.setCellValueFactory(new PropertyValueFactory<>("age")); table.getColumns().add(ageColumn); TableColumn cityColumn = new TableColumn<>("Город"); cityColumn.setCellValueFactory(new PropertyValueFactory<>("city")); table.getColumns().add(cityColumn); table.setOnMouseClicked(e -> { if(e.getClickCount()==2){ Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setHeaderText("Выбран объект!"); alert.setContentText(table.getSelectionModel().getSelectedItem().toString()); alert.showAndWait(); System.out.println("Диалоговое окно закрыто!"); } }); // Button btn = new Button(); // btn.setText("Say 'Hello World'"); // btn.setOnAction(new EventHandler() { // // @Override // public void handle(ActionEvent event) { // System.out.println("Hello World!"); // } // }); StackPane root = new StackPane(); root.getChildren().add(table); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }