Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** ProspectTableViewSample.java **/
- 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.Button;
- import javafx.scene.control.TableView;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- public class ProspectTableViewSample extends Application {
- @Override public void start(Stage stage) {
- final Calendar calendar = GregorianCalendar.getInstance();
- calendar.set(1959, Calendar.APRIL, 9);
- final Date APR_9_1959 = calendar.getTime();
- final ObservableList<Prospect> data = FXCollections.observableArrayList();
- data.setAll(
- new Prospect("Alan", "Shepard", APR_9_1959, "Navy"),
- new Prospect("John", "Glenn", APR_9_1959, "Navy"),
- new Prospect("Gus", "Grissom", APR_9_1959, "Navy"),
- new Prospect("Wally", "Schirra", APR_9_1959, "Navy"),
- new Prospect("Scott", "Carpenter", APR_9_1959, "Navy"),
- new Prospect("Gordon","Cooper", APR_9_1959, "Navy"),
- new Prospect("Deke", "Slayton", APR_9_1959, "Navy")
- );
- final VBox layout = new VBox(10);
- final TextField nameField = new TextField();
- nameField.setPromptText("Enter Transfer Last Name");
- final Button transfer = new Button("Transfer");
- transfer.setOnAction(new EventHandler<ActionEvent>() {
- @Override public void handle(ActionEvent actionEvent) {
- for (Prospect prospect: data) {
- if (prospect.getName().equalsIgnoreCase(nameField.getText().trim())) {
- prospect.setDpt("Nasa");
- }
- }
- }
- });
- transfer.setDefaultButton(true);
- TableView<Prospect> prospectTable = new ProspectTableView(null);
- prospectTable.getItems().setAll(data);
- prospectTable.setPrefHeight(250);
- layout.getChildren().setAll(
- nameField,
- transfer,
- prospectTable
- );
- layout.setStyle("-fx-padding: 10px; -fx-background-color: cornsilk;");
- stage.setScene(new Scene(layout));
- stage.show();
- prospectTable.requestFocus();
- }
- public static void main(String[] args) { launch(args); }
- }
- /** Prospect.java **/
- import javafx.beans.property.ObjectProperty;
- import javafx.beans.property.SimpleObjectProperty;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.beans.property.StringProperty;
- import java.util.Date;
- public class Prospect {
- private final StringProperty fornameProperty = new SimpleStringProperty();
- private final StringProperty nameProperty = new SimpleStringProperty();
- private final ObjectProperty<Date> dateProperty = new SimpleObjectProperty<>();
- private final StringProperty dptProperty = new SimpleStringProperty();
- public Prospect(String forname, String name, Date date, String dpt) {
- setForname(forname);
- setName(name);
- setDate(date);
- setDpt(dpt);
- }
- public StringProperty fornameProperty() {
- return fornameProperty;
- }
- public String getForname() {
- return fornameProperty.get();
- }
- public void setForname(String forname) {
- fornameProperty.set(forname);
- }
- public StringProperty nameProperty() {
- return nameProperty;
- }
- public String getName() {
- return nameProperty.get();
- }
- public void setName(String forname) {
- nameProperty.set(forname);
- }
- public ObjectProperty<Date> dateProperty() {
- return dateProperty;
- }
- public Date getDate() {
- return dateProperty.get();
- }
- public void setDate(Date date) {
- dateProperty.set(date);
- }
- public StringProperty dptProperty() {
- return dptProperty;
- }
- public String getDpt() {
- return dptProperty.get();
- }
- public void setDpt(String dpt) {
- dptProperty.set(dpt);
- }
- }
- /** ProspectTableView.java **/
- import javafx.application.Platform;
- import javafx.collections.ListChangeListener;
- import javafx.collections.ObservableList;
- import javafx.scene.control.TableCell;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.util.Callback;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class ProspectTableView extends TableView< Prospect > {
- protected static final SimpleDateFormat _dateFmt = new SimpleDateFormat( "dd/MM/yyyy" );
- public ProspectTableView( final Callback< Prospect, Void > listener ) {
- final ObservableList< TableColumn< Prospect, ? >> tblColumns = getColumns();
- final TableColumn< Prospect, String > forname = new TableColumn<>( "Prénom" );
- final TableColumn< Prospect, String > name = new TableColumn<>( "Nom" );
- final TableColumn< Prospect, Date> date = new TableColumn<>( "Date" );
- final TableColumn< Prospect, Integer > dpt = new TableColumn<>( "Dpt" );
- forname.setPrefWidth( 80 );
- name .setPrefWidth( 80 );
- date .setPrefWidth( 70 );
- dpt .setPrefWidth( 50 );
- setPrefWidth( 80+80+70+50+20 );
- forname.setCellValueFactory( new PropertyValueFactory< Prospect, String >( "forname" ));
- name .setCellValueFactory( new PropertyValueFactory< Prospect, String >( "name" ));
- date .setCellValueFactory( new PropertyValueFactory< Prospect, Date >( "date" ));
- dpt .setCellValueFactory( new PropertyValueFactory< Prospect, Integer >( "dpt" ));
- date.setCellFactory(
- new Callback<TableColumn< Prospect, Date >, TableCell< Prospect, Date >>() {
- @Override public TableCell< Prospect, Date > call( TableColumn< Prospect, Date > param) {
- final TableCell< Prospect, Date > cell = new TableCell< Prospect, Date >() {
- @Override public void updateItem( final Date item, boolean empty ) {
- if( item != null ) {
- setText( _dateFmt.format( item ));
- }}};
- return cell;
- }});
- tblColumns.add( forname );
- tblColumns.add( name );
- tblColumns.add( date );
- tblColumns.add( dpt );
- getSelectionModel().getSelectedIndices().addListener(
- new ListChangeListener< Integer >(){
- @Override public void onChanged( final Change< ? extends Integer > evt ) {
- while( evt.next()) {
- if( ! evt.getList().isEmpty() && listener != null ) {
- listener.call( getItems().get( evt.getList().get( 0 )));
- }
- }}});
- Platform.runLater(new Runnable() {
- @Override
- public void run() {
- getSortOrder().add(dpt);
- getSortOrder().add(date);
- getSortOrder().add(name);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement