javafx TableView中的TableColumn如何填充的自带类,以及编辑单元格

在javafx中TableView中有TableColumn,即每一列,对于每一列中每个单元怎么填充,javafx的官方文档提供了两个实现类

[java]  view plain  copy
  1. public class MapValueFactory<T>  
  2. extends java.lang.Object  
  3. implements Callback<TableColumn.CellDataFeatures<java.util.Map,T>,ObservableValue<T>>  

[java]  view plain  copy
  1. public class PropertyValueFactory<S,T>  
  2. extends java.lang.Object  
  3. implements Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>>  

以下是两个简单的例子:

[java]  view plain  copy
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. import javafx.application.Application;  
  4. import javafx.collections.FXCollections;  
  5. import javafx.collections.ObservableList;  
  6. import javafx.geometry.Insets;  
  7. import javafx.scene.Group;  
  8. import javafx.scene.Scene;  
  9. import javafx.scene.control.Label;  
  10. import javafx.scene.control.TableCell;  
  11. import javafx.scene.control.TableColumn;  
  12. import javafx.scene.control.TableView;  
  13. import javafx.scene.control.cell.MapValueFactory;  
  14. import javafx.scene.control.cell.TextFieldTableCell;  
  15. import javafx.scene.layout.VBox;  
  16. import javafx.scene.text.Font;  
  17. import javafx.stage.Stage;  
  18. import javafx.util.Callback;  
  19. import javafx.util.StringConverter;  
  20.    
  21. public class TableViewSample5 extends Application {  
  22.    
  23.     public static final String Column1MapKey = "A";  
  24.     public static final String Column2MapKey = "B";  
  25.    
  26.     public static void main(String[] args) {  
  27.         launch(args);  
  28.     }  
  29.    
  30.     @Override  
  31.     public void start(Stage stage) {  
  32.         Scene scene = new Scene(new Group());  
  33.         stage.setTitle("Table View Sample");  
  34.         stage.setWidth(300);  
  35.         stage.setHeight(500);  
  36.           
  37.         final Label label = new Label("Student IDs");  
  38.         label.setFont(new Font("Arial"20));  
  39.    
  40.         TableColumn<Map, String> firstDataColumn = new TableColumn<>("Class A");  
  41.         TableColumn<Map, String> secondDataColumn = new TableColumn<>("Class B");  
  42.    
  43.         firstDataColumn.setCellValueFactory(new MapValueFactory(Column1MapKey));  
  44.         firstDataColumn.setMinWidth(130);  
  45.         secondDataColumn.setCellValueFactory(new MapValueFactory(Column2MapKey));  
  46.         secondDataColumn.setMinWidth(130);  
  47.    
  48.         TableView table_view = new TableView<>(generateDataInMap());  
  49.    
  50.         table_view.setEditable(true);  
  51.         table_view.getSelectionModel().setCellSelectionEnabled(true);  
  52.         table_view.getColumns().setAll(firstDataColumn, secondDataColumn);  
  53.   
  54.    
  55.         final VBox vbox = new VBox();  
  56.    
  57.         vbox.setSpacing(5);  
  58.         vbox.setPadding(new Insets(100010));  
  59.         vbox.getChildren().addAll(label, table_view);  
  60.    
  61.         ((Group) scene.getRoot()).getChildren().addAll(vbox);  
  62.    
  63.         stage.setScene(scene);  
  64.    
  65.         stage.show();  
  66.     }  
  67.    
  68.     private ObservableList<Map> generateDataInMap() {  
  69.         int max = 10;  
  70.         ObservableList<Map> allData = FXCollections.observableArrayList();  
  71.         for (int i = 1; i < max; i++) {  
  72.             Map<String, String> dataRow = new HashMap<>();  
  73.    
  74.             String value1 = "A" + i;  
  75.             String value2 = "B" + i;  
  76.    
  77.             dataRow.put(Column1MapKey, value1);  
  78.             dataRow.put(Column2MapKey, value2);  
  79.    
  80.             allData.add(dataRow);  
  81.         }  
  82.         return allData;  
  83.     }  
  84. }  

[java]  view plain  copy
  1. import javafx.application.Application;  
  2. import javafx.beans.property.SimpleStringProperty;  
  3. import javafx.collections.FXCollections;  
  4. import javafx.collections.ObservableList;  
  5. import javafx.geometry.Insets;  
  6. import javafx.scene.Group;  
  7. import javafx.scene.Scene;  
  8. import javafx.scene.control.Label;  
  9. import javafx.scene.control.TableColumn;  
  10. import javafx.scene.control.TableView;  
  11. import javafx.scene.control.TextField;  
  12. import javafx.scene.control.cell.PropertyValueFactory;  
  13. import javafx.scene.layout.VBox;  
  14. import javafx.scene.text.Font;  
  15. import javafx.stage.Stage;  
  16.    
  17. public class TableViewSample7 extends Application {  
  18.    
  19.     private TableView<Person> table = new TableView<Person>();  
  20.     private final ObservableList<Person> data =  
  21.         FXCollections.observableArrayList(  
  22.             new Person("Jacob""Smith""jacob.smith@example.com"),  
  23.             new Person("Isabella""Johnson""isabella.johnson@example.com"),  
  24.             new Person("Ethan""Williams""ethan.williams@example.com"),  
  25.             new Person("Emma""Jones""emma.jones@example.com"),  
  26.             new Person("Michael""Brown""michael.brown@example.com")  
  27.         );  
  28.      
  29.     public static void main(String[] args) {  
  30.         launch(args);  
  31.     }  
  32.    
  33.     @Override  
  34.     public void start(Stage stage) {  
  35.         Scene scene = new Scene(new Group());  
  36.         stage.setTitle("Table View Sample");  
  37.         stage.setWidth(450);  
  38.         stage.setHeight(500);  
  39.    
  40.         final Label label = new Label("Address Book");  
  41.         label.setFont(new Font("Arial"20));  
  42.    
  43.         table.setEditable(true);  
  44.    
  45.         TableColumn firstNameCol = new TableColumn("First Name");  
  46.         firstNameCol.setMinWidth(100);  
  47.         firstNameCol.setCellValueFactory(  
  48.                 new PropertyValueFactory<Person, String>("firstName"));  
  49.    
  50.         TableColumn lastNameCol = new TableColumn("Last Name");  
  51.         lastNameCol.setMinWidth(100);  
  52.         lastNameCol.setCellValueFactory(  
  53.                 new PropertyValueFactory<Person, String>("lastName"));  
  54.    
  55.         TableColumn emailCol = new TableColumn("Email");  
  56.         emailCol.setMinWidth(200);  
  57.         emailCol.setCellValueFactory(  
  58.                 new PropertyValueFactory<Person, String>("email"));  
  59.    
  60.         table.setItems(data);  
  61.         table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);  
  62.    
  63.         final VBox vbox = new VBox();  
  64.         vbox.setSpacing(5);  
  65.         vbox.setPadding(new Insets(100010));  
  66.         vbox.getChildren().addAll(label, table);  
  67.    
  68.         ((Group) scene.getRoot()).getChildren().addAll(vbox);  
  69.    
  70.         stage.setScene(scene);  
  71.         stage.show();  
  72.     }  
  73.    
  74.     public static class Person {  
  75.    
  76.         private final SimpleStringProperty firstName;  
  77.         private final SimpleStringProperty lastName;  
  78.         private final SimpleStringProperty email;  
  79.    
  80.         private Person(String fName, String lName, String email) {  
  81.             this.firstName = new SimpleStringProperty(fName);  
  82.             this.lastName = new SimpleStringProperty(lName);  
  83.             this.email = new SimpleStringProperty(email);  
  84.         }  
  85.    
  86.         public String getFirstName() {  
  87.             return firstName.get();  
  88.         }  
  89.    
  90.         public void setFirstName(String fName) {  
  91.             firstName.set(fName);  
  92.         }  
  93.    
  94.         public String getLastName() {  
  95.             return lastName.get();  
  96.         }  
  97.    
  98.         public void setLastName(String fName) {  
  99.             lastName.set(fName);  
  100.         }  
  101.    
  102.         public String getEmail() {  
  103.             return email.get();  
  104.         }  
  105.    
  106.         public void setEmail(String fName) {  
  107.             email.set(fName);  
  108.         }  
  109.     }  
  110. }   

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值