Advertisement
Guest User

tableView sample

a guest
Mar 26th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.beans.property.SimpleStringProperty;
  3. import javafx.collections.FXCollections;
  4. import javafx.collections.ObservableList;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TableCell;
  8. import javafx.scene.control.TableColumn;
  9. import javafx.scene.control.TableView;
  10. import javafx.scene.control.cell.PropertyValueFactory;
  11. import javafx.scene.layout.HBox;
  12. import javafx.scene.layout.VBox;
  13. import javafx.scene.text.Font;
  14. import javafx.stage.Stage;
  15. import javafx.util.Callback;
  16.  
  17. public class TableViewSample extends Application {
  18.  
  19.     private TableView<Person> table = new TableView<Person>();
  20.     private final ObservableList<Person> data = FXCollections.observableArrayList(new Person("Jacob", "Smith",
  21.             "jacob.smith@example.com"), new Person("Isabella", "Johnson", "isabella.johnson@example.com"), new Person(
  22.             "Ethan", "Williams", "ethan.williams@example.com"), new Person("Emma", "Jones", "emma.jones@example.com"),
  23.             new Person("Michael", "Brown", "michael.brown@example.com"));
  24.     final HBox hb = new HBox();
  25.  
  26.     public static void main(String[] args) {
  27.         launch(args);
  28.     }
  29.  
  30.     @Override
  31.     public void start(Stage stage) {
  32.         stage.setTitle("Table View Sample");
  33.         stage.setWidth(450);
  34.         stage.setHeight(550);
  35.  
  36.         final Label label = new Label("Address Book");
  37.         label.setFont(new Font("Arial", 20));
  38.  
  39.         table.setEditable(true);
  40.         table.getSelectionModel().setCellSelectionEnabled(true);
  41.  
  42.         for (int i = 0; i < 20; ++i) {
  43.             TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
  44.             firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
  45.             firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
  46.  
  47.                 @Override
  48.                 public TableCell<Person, String> call(TableColumn<Person, String> arg0) {
  49.  
  50.                     TableCell cell = new TableCell<>();
  51.                     cell.setText("This is a test sample");
  52.                     return cell;
  53.                 }
  54.             });
  55.             table.getColumns().add(firstNameCol);
  56.         }
  57.  
  58.         table.setItems(data);
  59.  
  60.         final VBox vbox = new VBox();
  61.         vbox.getChildren().addAll( table);
  62.  
  63.         Scene scene = new Scene(vbox);
  64.  
  65.         stage.setScene(scene);
  66.         stage.show();
  67.     }
  68.  
  69.     public static class Person {
  70.  
  71.         private final SimpleStringProperty firstName;
  72.         private final SimpleStringProperty lastName;
  73.         private final SimpleStringProperty email;
  74.  
  75.         private Person(String fName, String lName, String email) {
  76.             this.firstName = new SimpleStringProperty(fName);
  77.             this.lastName = new SimpleStringProperty(lName);
  78.             this.email = new SimpleStringProperty(email);
  79.         }
  80.  
  81.         public String getFirstName() {
  82.             return firstName.get();
  83.         }
  84.  
  85.         public void setFirstName(String fName) {
  86.             firstName.set(fName);
  87.         }
  88.  
  89.         public String getLastName() {
  90.             return lastName.get();
  91.         }
  92.  
  93.         public void setLastName(String fName) {
  94.             lastName.set(fName);
  95.         }
  96.  
  97.         public String getEmail() {
  98.             return email.get();
  99.         }
  100.  
  101.         public void setEmail(String fName) {
  102.             email.set(fName);
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement