Chinaunix首页 | 论坛 | 博客
  • 博客访问: 499695
  • 博文数量: 109
  • 博客积分: 2331
  • 博客等级: 大尉
  • 技术积分: 1062
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-24 21:36
文章分类

全部博文(109)

文章存档

2015年(2)

2013年(1)

2012年(78)

2011年(28)

我的朋友

分类: Java

2011-12-27 17:27:09

  1. import javafx.application.Application;
  2. import javafx.beans.property.SimpleStringProperty;
  3. import javafx.beans.property.StringProperty;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.event.ActionEvent;
  7. import javafx.event.EventHandler;
  8. import javafx.geometry.Insets;
  9. import javafx.scene.Group;
  10. import javafx.scene.Scene;
  11. import javafx.scene.control.Button;
  12. import javafx.scene.control.Label;
  13. import javafx.scene.control.TableColumn;
  14. import javafx.scene.control.TableView;
  15. import javafx.scene.control.TextField;
  16. import javafx.scene.control.cell.PropertyValueFactory;
  17. import javafx.scene.layout.HBox;
  18. import javafx.scene.layout.VBox;
  19. import javafx.scene.text.Font;
  20. import javafx.stage.Stage;
  21.  
  22. public class Main extends Application {
  23.  
  24.     public static class Person {
  25.  
  26.         private final StringProperty firstName;
  27.         private final StringProperty lastName;
  28.         private final StringProperty email;
  29.  
  30.         private Person(String fName, String lName, String email) {
  31.             this.firstName = new SimpleStringProperty(fName);
  32.             this.lastName = new SimpleStringProperty(lName);
  33.             this.email = new SimpleStringProperty(email);
  34.         }
  35.  
  36.         public String getFirstName() {
  37.             return firstName.get();
  38.         }
  39.  
  40.         public void setFirstName(String fName) {
  41.             firstName.set(fName);
  42.         }
  43.  
  44.         public String getLastName() {
  45.             return lastName.get();
  46.         }
  47.  
  48.         public void setLastName(String fName) {
  49.             lastName.set(fName);
  50.         }
  51.  
  52.         public String getEmail() {
  53.             return email.get();
  54.         }
  55.  
  56.         public void setEmail(String fName) {
  57.             email.set(fName);
  58.         }
  59.  
  60.     }
  61.     private TableView<Person> table = new TableView<Person>();
  62.     private final ObservableList<Person> data =
  63.         FXCollections.observableArrayList(
  64.             new Person("Jacob", "Smith", "jacob.smith@example.com"),
  65.             new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
  66.             new Person("Ethan", "Williams", "ethan.williams@example.com"),
  67.             new Person("Emma", "Jones", "emma.jones@example.com"),
  68.             new Person("Michael", "Brown", "michael.brown@example.com")
  69.         );
  70.  
  71.     private HBox hb = new HBox();
  72.  
  73.     public static void main(String[] args) {
  74.         launch(args);
  75.     }
  76.  
  77.     @Override
  78.     public void start(Stage stage) {
  79.         Scene scene = new Scene(new Group());
  80.         stage.setTitle("Table View Sample");
  81.         stage.setWidth(400);
  82.         stage.setHeight(500);
  83.  
  84.         final Label label = new Label("Address Book");
  85.         label.setFont(new Font("Arial", 20));
  86.  
  87.         TableColumn firstNameCol = new TableColumn("First");
  88.         firstNameCol.setCellValueFactory(
  89.             new PropertyValueFactory<Person,String>("firstName")
  90.         );
  91.  
  92.         TableColumn lastNameCol = new TableColumn("Last");
  93.         lastNameCol.setCellValueFactory(
  94.             new PropertyValueFactory<Person,String>("lastName")
  95.         );
  96.  
  97.         TableColumn emailCol = new TableColumn("Email");
  98.         emailCol.setMinWidth(200);
  99.         emailCol.setCellValueFactory(
  100.                 new PropertyValueFactory<Person,String>("email")
  101.         );
  102.  
  103.         table.setItems(data);
  104.         table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
  105.  
  106.         final TextField addFirstName = new TextField();
  107.         addFirstName.setPromptText("Last Name");
  108.         addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
  109.         final TextField addLastName = new TextField();
  110.         addLastName.setMaxWidth(lastNameCol.getPrefWidth());
  111.         addLastName.setPromptText("Last Name");
  112.         final TextField addEmail = new TextField();
  113.         addEmail.setMaxWidth(emailCol.getPrefWidth());
  114.         addEmail.setPromptText("Email");
  115.  
  116.         final Button addButton = new Button("Add");
  117.         addButton.setOnAction(new EventHandler<ActionEvent>() {
  118.             @Override public void handle(ActionEvent e) {
  119.                 data.add(new Person(
  120.                         addFirstName.getText(),
  121.                         addLastName.getText(),
  122.                         addEmail.getText()
  123.                         ));
  124.                 addFirstName.setText("");
  125.                 addLastName.setText("");
  126.                 addEmail.setText("");
  127.             }
  128.         });
  129.  
  130.         hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
  131.         hb.setSpacing(3);
  132.  
  133.         final VBox vbox = new VBox();
  134.         vbox.setSpacing(5);
  135.         vbox.getChildren().addAll(label, table, hb);
  136.         vbox.setPadding(new Insets(10, 0, 0, 10));
  137.  
  138.         ((Group) scene.getRoot()).getChildren().addAll(vbox);
  139.  
  140.         stage.setScene(scene);
  141.         stage.show();
  142.     }
  143. }
  144. 来源网址:
阅读(5867) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~