Wednesday, August 5, 2015

JavaFX Table view

I'm writing this post just to remember to myself how did I achieved some goals.
So this first is about JavaFX Table view.
// Allow TableView select multiply rows  
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

//Allow TableView to select single cell     
tableView.getSelectionModel().setCellSelectionEnabled(true);

//TableColumn<Batch, String> codeColumn;
codeColumn.setCellValueFactory(new PropertyValueFactory<Batch, String>(
                "code"));

//Make the codeColumn editable
codeColumn.setCellFactory(TextFieldTableCell.<Batch> forTableColumn());

//Register listner on finish editing cell
codeColumn.setOnEditCommit(value -> {
            System.out.println(value.getRowValue().getCode());
        });

// Make column resizable
codeColumn.prefWidthProperty()
                .bind(tableView.widthProperty().divide(3));



nameColumn.setCellValueFactory(new PropertyValueFactory<Batch, String>(
                "name"));

//If you have some double field than you have to
// use DoubleStringConverter from javafx.util.converters  
nameColumn.setCellFactory(TextFieldTableCell
                .<Batch, String> forTableColumn(new DefaultStringConverter()));

nameColumn.setOnEditCommit(value -> {
            System.out.println(value.getNewValue());
            System.out.println(value.getRowValue().getCode());
        });


nameColumn.prefWidthProperty().bind(tableView.widthProperty().divide(3));


btnCol.setMinWidth(150);


//Creating column which holds buttons
btnCol.setCellValueFactory(new Callback<CellDataFeatures<Batch, Batch>, ObservableValue<Batch>>() {
            @Override
            public ObservableValue<Batch> call(
                    CellDataFeatures<Batch, Batch> features) {
                return new ReadOnlyObjectWrapper(features.getValue());
            }
        });

btnCol.prefWidthProperty().bind(tableView.widthProperty().divide(3));

//Fill column with buttons
btnCol.setCellFactory(new Callback<TableColumn<Batch, Batch>, TableCell<Batch, Batch>>() {
            @Override
            public TableCell<Batch, Batch> call(TableColumn<Batch, Batch> btnCol) {
                return new TableCell<Batch, Batch>() {
                    final Button button = new Button();
                    {
                        button.setMinWidth(130);
                        button.setText("save");
                    }

                    @Override
                    public void updateItem(final Batch person, boolean empty) {
                        super.updateItem(person, empty);
                        //Setting user data to Model
                        button.setUserData(person);
                        setGraphic(button);
                        button.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                              
                                Button btnButton = (Button)event.getSource();
                                //Retrieving user data which is Batch
                                Batch batch = (Batch)btnButton.getUserData();
                                System.out.println(batch.getCode());
                                System.out.println(batch.getName());
                            }
                        });
                    }
                };
            }
        });

//Filling table with data 
tableView.setItems(new BatchService().list());

1 comment:

  1. Casino | Harrah's Lake Tahoe - MapyRO
    Casino. 익산 출장샵 8 김포 출장안마 Casino Lake Tahoe Blvd. Stateline, NV 89449. Directions · 속초 출장안마 (702) 624-7085. Call Now · More Info. Hours, Accepts Credit Cards, 익산 출장안마 Accepts 순천 출장마사지

    ReplyDelete