шпаргалки
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.3 KiB

2 years ago
/*
* 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<Person> 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<Person> obsPerson = FXCollections.observableArrayList(personsList);
TableView<Person> table = new TableView<>(obsPerson);
TableColumn<Person, String> nameColumn = new TableColumn<>("Имя");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
table.getColumns().add(nameColumn);
TableColumn<Person, Integer> ageColumn = new TableColumn<>("Возраст");
ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
table.getColumns().add(ageColumn);
TableColumn<Person, String> 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<ActionEvent>() {
//
// @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);
}
}