This discussion is archived
2 Replies Latest reply: Nov 28, 2012 1:35 AM by 883466 RSS

Focusing cell with the tab key

883466 Newbie
Currently Being Moderated
Hi!

I set the cellSelectionEnabled of my table view to true and directional keys work fine.

But I would like to use the tab key to focused my cell as most of board/table tools but the current behaviour is to select next node of the scene (see example below)

Is it a bug? If not, how could I override this behaviour?

Thanks in advance!

Kévin
import com.dooapp.fxSpoon.property.BigDecimalProperty;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;


public class TableViewFocusTest extends Application {

    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
            FXCollections.observableArrayList(
                    new Person(10, 10, 10), new Person(10, 10, 10), new Person(10, 10, 10), new Person(10, 10, 10), new Person(10, 10, 10)
            );
    final HBox hb = new HBox();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
     
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(550);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));


        TableColumn firstNameCol = new TableColumn("age");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("age"));

        firstNameCol.setOnEditCommit(
                new EventHandler<TableColumn.CellEditEvent<Person, BigDecimal>>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent<Person, BigDecimal> t) {
                        ((Person) t.getTableView().getItems().get(
                                t.getTablePosition().getRow())
                        ).setAge(t.getNewValue());
                    }
                }
        );


        TableColumn lastNameCol = new TableColumn("qi");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("qi"));

        lastNameCol.setOnEditCommit(
                new EventHandler<TableColumn.CellEditEvent<Person, BigDecimal>>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent<Person, BigDecimal> t) {
                        ((Person) t.getTableView().getItems().get(
                                t.getTablePosition().getRow())
                        ).setQi(t.getNewValue());
                    }
                }
        );

        TableColumn emailCol = new TableColumn("experience");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("experience"));

        emailCol.setOnEditCommit(
                new EventHandler<TableColumn.CellEditEvent<Person, BigDecimal>>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent<Person, BigDecimal> t) {
                        ((Person) t.getTableView().getItems().get(
                                t.getTablePosition().getRow())
                        ).setExperience(t.getNewValue());
                    }
                }
        );


        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        final TextField addFirstName = new TextField();
        addFirstName.setPromptText("Age");
        addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
        final TextField addLastName = new TextField();
        addLastName.setMaxWidth(lastNameCol.getPrefWidth());
        addLastName.setPromptText("qi");
        final TextField addEmail = new TextField();
        addEmail.setMaxWidth(emailCol.getPrefWidth());
        addEmail.setPromptText("experience");
        table.setItems(data);
        table.getSelectionModel().setCellSelectionEnabled(true);
        final Button addButton = new Button("Add");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                data.add(new Person(
                        Integer.parseInt(addFirstName.getText()),
                        Integer.parseInt(addLastName.getText()),
                        Integer.parseInt(addEmail.getText())));
                addFirstName.clear();
                addLastName.clear();
                addEmail.clear();
            }
        });

        hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
        hb.setSpacing(3);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table, hb);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        @NotNull
        private final BigDecimalProperty age;
        @Valid
        private final BigDecimalProperty qi;
        @Valid
        private final BigDecimalProperty experience;

        private Person(int fName, int lName, int email) {
            this.age = new BigDecimalProperty();
            this.age.set(new BigDecimal(fName));
            this.qi = new BigDecimalProperty();
            this.qi.set(new BigDecimal(lName));
            this.experience = new BigDecimalProperty();
            this.experience.set(new BigDecimal(email));

        }

        public BigDecimal getAge() {
            return age.get();
        }

        public void setAge(BigDecimal fName) {
            age.set(fName);
        }

        public BigDecimal getQi() {
            return qi.get();
        }

        public void setQi(BigDecimal fName) {
            qi.set(fName);
        }

        public BigDecimal getExperience() {
            return experience.get();
        }

        public void setExperience(BigDecimal fName) {
            experience.set(fName);
        }
    }
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points