Advertisement
jewelsea

ProspectTableViewSample

Jul 25th, 2013
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.31 KB | None | 0 0
  1. /** ProspectTableViewSample.java **/
  2.  
  3. import javafx.application.Application;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.event.ActionEvent;
  7. import javafx.event.EventHandler;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.TableView;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.layout.VBox;
  13. import javafx.stage.Stage;
  14.  
  15. import java.util.Calendar;
  16. import java.util.Date;
  17. import java.util.GregorianCalendar;
  18.  
  19. public class ProspectTableViewSample extends Application {
  20.     @Override public void start(Stage stage) {
  21.         final Calendar calendar = GregorianCalendar.getInstance();
  22.         calendar.set(1959, Calendar.APRIL, 9);
  23.         final Date APR_9_1959 = calendar.getTime();
  24.  
  25.         final ObservableList<Prospect> data = FXCollections.observableArrayList();
  26.         data.setAll(
  27.                 new Prospect("Alan",  "Shepard",   APR_9_1959, "Navy"),
  28.                 new Prospect("John",  "Glenn",     APR_9_1959, "Navy"),
  29.                 new Prospect("Gus",   "Grissom",   APR_9_1959, "Navy"),
  30.                 new Prospect("Wally", "Schirra",   APR_9_1959, "Navy"),
  31.                 new Prospect("Scott", "Carpenter", APR_9_1959, "Navy"),
  32.                 new Prospect("Gordon","Cooper",    APR_9_1959, "Navy"),
  33.                 new Prospect("Deke",  "Slayton",   APR_9_1959, "Navy")
  34.         );
  35.  
  36.         final VBox layout = new VBox(10);
  37.  
  38.         final TextField nameField = new TextField();
  39.         nameField.setPromptText("Enter Transfer Last Name");
  40.  
  41.         final Button transfer = new Button("Transfer");
  42.         transfer.setOnAction(new EventHandler<ActionEvent>() {
  43.             @Override public void handle(ActionEvent actionEvent) {
  44.                 for (Prospect prospect: data) {
  45.                     if (prospect.getName().equalsIgnoreCase(nameField.getText().trim())) {
  46.                         prospect.setDpt("Nasa");
  47.                     }
  48.                 }
  49.             }
  50.         });
  51.         transfer.setDefaultButton(true);
  52.  
  53.         TableView<Prospect> prospectTable = new ProspectTableView(null);
  54.         prospectTable.getItems().setAll(data);
  55.         prospectTable.setPrefHeight(250);
  56.  
  57.         layout.getChildren().setAll(
  58.                 nameField,
  59.                 transfer,
  60.                 prospectTable
  61.         );
  62.         layout.setStyle("-fx-padding: 10px; -fx-background-color: cornsilk;");
  63.  
  64.         stage.setScene(new Scene(layout));
  65.         stage.show();
  66.  
  67.         prospectTable.requestFocus();
  68.     }
  69.  
  70.     public static void main(String[] args) { launch(args); }
  71. }
  72.  
  73.  
  74.  
  75. /** Prospect.java **/
  76.  
  77. import javafx.beans.property.ObjectProperty;
  78. import javafx.beans.property.SimpleObjectProperty;
  79. import javafx.beans.property.SimpleStringProperty;
  80. import javafx.beans.property.StringProperty;
  81.  
  82. import java.util.Date;
  83.  
  84. public class Prospect {
  85.     private final StringProperty fornameProperty = new SimpleStringProperty();
  86.     private final StringProperty nameProperty = new SimpleStringProperty();
  87.     private final ObjectProperty<Date> dateProperty = new SimpleObjectProperty<>();
  88.     private final StringProperty dptProperty = new SimpleStringProperty();
  89.  
  90.     public Prospect(String forname, String name, Date date, String dpt) {
  91.         setForname(forname);
  92.         setName(name);
  93.         setDate(date);
  94.         setDpt(dpt);
  95.     }
  96.  
  97.     public StringProperty fornameProperty() {
  98.         return fornameProperty;
  99.     }
  100.  
  101.     public String getForname() {
  102.         return fornameProperty.get();
  103.     }
  104.  
  105.     public void setForname(String forname) {
  106.         fornameProperty.set(forname);
  107.     }
  108.  
  109.     public StringProperty nameProperty() {
  110.         return nameProperty;
  111.     }
  112.  
  113.     public String getName() {
  114.         return nameProperty.get();
  115.     }
  116.  
  117.     public void setName(String forname) {
  118.         nameProperty.set(forname);
  119.     }
  120.  
  121.     public ObjectProperty<Date> dateProperty() {
  122.         return dateProperty;
  123.     }
  124.  
  125.     public Date getDate() {
  126.         return dateProperty.get();
  127.     }
  128.  
  129.     public void setDate(Date date) {
  130.         dateProperty.set(date);
  131.     }
  132.  
  133.     public StringProperty dptProperty() {
  134.         return dptProperty;
  135.     }
  136.  
  137.     public String getDpt() {
  138.         return dptProperty.get();
  139.     }
  140.  
  141.     public void setDpt(String dpt) {
  142.         dptProperty.set(dpt);
  143.     }
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /** ProspectTableView.java **/
  150.  
  151. import javafx.application.Platform;
  152. import javafx.collections.ListChangeListener;
  153. import javafx.collections.ObservableList;
  154. import javafx.scene.control.TableCell;
  155. import javafx.scene.control.TableColumn;
  156. import javafx.scene.control.TableView;
  157. import javafx.scene.control.cell.PropertyValueFactory;
  158. import javafx.util.Callback;
  159.  
  160. import java.text.SimpleDateFormat;
  161. import java.util.Date;
  162.  
  163. public class ProspectTableView extends TableView< Prospect > {
  164.  
  165.    protected static final SimpleDateFormat _dateFmt = new SimpleDateFormat( "dd/MM/yyyy" );
  166.  
  167.    public ProspectTableView( final Callback< Prospect, Void > listener ) {
  168.       final ObservableList< TableColumn< Prospect, ? >> tblColumns = getColumns();
  169.       final TableColumn< Prospect, String  > forname = new TableColumn<>( "Prénom" );
  170.       final TableColumn< Prospect, String  > name    = new TableColumn<>( "Nom"    );
  171.       final TableColumn< Prospect, Date> date    = new TableColumn<>( "Date"   );
  172.       final TableColumn< Prospect, Integer > dpt     = new TableColumn<>( "Dpt"    );
  173.       forname.setPrefWidth( 80 );
  174.       name   .setPrefWidth( 80 );
  175.       date   .setPrefWidth( 70 );
  176.       dpt    .setPrefWidth( 50 );
  177.       setPrefWidth( 80+80+70+50+20 );
  178.       forname.setCellValueFactory( new PropertyValueFactory< Prospect, String  >( "forname" ));
  179.       name   .setCellValueFactory( new PropertyValueFactory< Prospect, String  >( "name" ));
  180.       date   .setCellValueFactory( new PropertyValueFactory< Prospect, Date    >( "date" ));
  181.       dpt    .setCellValueFactory( new PropertyValueFactory< Prospect, Integer >( "dpt" ));
  182.       date.setCellFactory(
  183.          new Callback<TableColumn< Prospect, Date >, TableCell< Prospect, Date >>() {
  184.             @Override public TableCell< Prospect, Date > call( TableColumn< Prospect, Date > param) {
  185.                final TableCell< Prospect, Date > cell = new TableCell< Prospect, Date >() {
  186.                   @Override public void updateItem( final Date item, boolean empty ) {
  187.                      if( item != null ) {
  188.                         setText( _dateFmt.format( item ));
  189.                      }}};
  190.                return cell;
  191.             }});
  192.       tblColumns.add( forname );
  193.       tblColumns.add( name );
  194.       tblColumns.add( date );
  195.       tblColumns.add( dpt );
  196.       getSelectionModel().getSelectedIndices().addListener(
  197.          new ListChangeListener< Integer >(){
  198.             @Override public void onChanged( final Change< ? extends Integer > evt ) {
  199.                while( evt.next()) {
  200.                   if( ! evt.getList().isEmpty() && listener != null ) {
  201.                      listener.call( getItems().get( evt.getList().get( 0 )));
  202.                   }
  203.                }}});
  204.       Platform.runLater(new Runnable() {
  205.           @Override
  206.           public void run() {
  207.               getSortOrder().add(dpt);
  208.               getSortOrder().add(date);
  209.               getSortOrder().add(name);
  210.           }
  211.       });
  212.    }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement