Last active
January 27, 2021 01:52
-
-
Save abhinayagarwal/9383881 to your computer and use it in GitHub Desktop.
EditableTableView With Tab Functionality Only for same Row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.ContentDisplay; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TableCell; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableColumn.CellEditEvent; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.input.KeyCode; | |
import javafx.scene.input.KeyEvent; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.Font; | |
import javafx.stage.Stage; | |
import javafx.util.Callback; | |
public class EditableTableViewWithTab extends Application { | |
private TableView<Person> table = new TableView<Person>(); | |
private final ObservableList<Person> data = FXCollections | |
.observableArrayList(new Person("Jacob", "Smith", | |
"jacob.smith@example.com"), new Person("Isabella", | |
"Johnson", "isabella.johnson@example.com"), new Person( | |
"Ethan", "Williams", "ethan.williams@example.com"), | |
new Person("Emma", "Jones", "emma.jones@example.com"), | |
new Person("Michael", "Brown", "michael.brown@example.com")); | |
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)); | |
table.setEditable(true); | |
Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() { | |
public TableCell call(TableColumn p) { | |
return new EditingCell(); | |
} | |
}; | |
TableColumn firstNameCol = new TableColumn("First Name"); | |
firstNameCol.setMinWidth(100); | |
firstNameCol | |
.setCellValueFactory(new PropertyValueFactory<Person, String>( | |
"firstName")); | |
firstNameCol.setCellFactory(cellFactory); | |
firstNameCol | |
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
((Person) t.getTableView().getItems() | |
.get(t.getTablePosition().getRow())) | |
.setFirstName(t.getNewValue()); | |
} | |
}); | |
TableColumn lastNameCol = new TableColumn("Last Name"); | |
lastNameCol.setMinWidth(100); | |
lastNameCol | |
.setCellValueFactory(new PropertyValueFactory<Person, String>( | |
"lastName")); | |
lastNameCol.setCellFactory(cellFactory); | |
lastNameCol | |
.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
((Person) t.getTableView().getItems() | |
.get(t.getTablePosition().getRow())) | |
.setLastName(t.getNewValue()); | |
} | |
}); | |
TableColumn emailCol = new TableColumn("Email"); | |
emailCol.setMinWidth(200); | |
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>( | |
"email")); | |
emailCol.setCellFactory(cellFactory); | |
emailCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() { | |
@Override | |
public void handle(CellEditEvent<Person, String> t) { | |
((Person) t.getTableView().getItems() | |
.get(t.getTablePosition().getRow())).setEmail(t | |
.getNewValue()); | |
} | |
}); | |
table.setItems(data); | |
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol); | |
final TextField addFirstName = new TextField(); | |
addFirstName.setPromptText("First Name"); | |
addFirstName.setMaxWidth(firstNameCol.getPrefWidth()); | |
final TextField addLastName = new TextField(); | |
addLastName.setMaxWidth(lastNameCol.getPrefWidth()); | |
addLastName.setPromptText("Last Name"); | |
final TextField addEmail = new TextField(); | |
addEmail.setMaxWidth(emailCol.getPrefWidth()); | |
addEmail.setPromptText("Email"); | |
final VBox vbox = new VBox(); | |
vbox.setSpacing(5); | |
vbox.setPadding(new Insets(10, 0, 0, 10)); | |
vbox.getChildren().addAll(label, table); | |
((Group) scene.getRoot()).getChildren().addAll(vbox); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
public static class Person { | |
private final SimpleStringProperty firstName; | |
private final SimpleStringProperty lastName; | |
private final SimpleStringProperty email; | |
private Person(String fName, String lName, String email) { | |
this.firstName = new SimpleStringProperty(fName); | |
this.lastName = new SimpleStringProperty(lName); | |
this.email = new SimpleStringProperty(email); | |
} | |
public String getFirstName() { | |
return firstName.get(); | |
} | |
public void setFirstName(String fName) { | |
firstName.set(fName); | |
} | |
public String getLastName() { | |
return lastName.get(); | |
} | |
public void setLastName(String fName) { | |
lastName.set(fName); | |
} | |
public String getEmail() { | |
return email.get(); | |
} | |
public void setEmail(String fName) { | |
email.set(fName); | |
} | |
} | |
class EditingCell extends TableCell<Person, String> { | |
private TextField textField; | |
public EditingCell() { | |
} | |
@Override | |
public void startEdit() { | |
if (!isEmpty()) { | |
super.startEdit(); | |
if (textField == null) { | |
createTextField(); | |
} | |
setGraphic(textField); | |
setContentDisplay(ContentDisplay.GRAPHIC_ONLY); | |
// textField.selectAll(); | |
Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
textField.requestFocus(); | |
textField.selectAll(); | |
} | |
}); | |
} | |
} | |
@Override | |
public void cancelEdit() { | |
super.cancelEdit(); | |
setText((String) getItem()); | |
setGraphic(null); | |
} | |
@Override | |
public void updateItem(String item, boolean empty) { | |
super.updateItem(item, empty); | |
if (empty) { | |
setText(null); | |
setGraphic(null); | |
} else { | |
if (isEditing()) { | |
if (textField != null) { | |
textField.setText(getString()); | |
} | |
setGraphic(textField); | |
setContentDisplay(ContentDisplay.GRAPHIC_ONLY); | |
} else { | |
setText(getString()); | |
setContentDisplay(ContentDisplay.TEXT_ONLY); | |
} | |
} | |
} | |
private void createTextField() { | |
textField = new TextField(getString()); | |
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() | |
* 2); | |
textField.focusedProperty().addListener( | |
new ChangeListener<Boolean>() { | |
@Override | |
public void changed( | |
ObservableValue<? extends Boolean> arg0, | |
Boolean arg1, Boolean arg2) { | |
if (!arg2) { | |
commitEdit(textField.getText()); | |
} | |
} | |
}); | |
textField.setOnKeyPressed(new EventHandler<KeyEvent>() { | |
@Override | |
public void handle(KeyEvent t) { | |
if (t.getCode() == KeyCode.ENTER) { | |
commitEdit(textField.getText()); | |
} else if (t.getCode() == KeyCode.ESCAPE) { | |
cancelEdit(); | |
} else if (t.getCode() == KeyCode.TAB) { | |
commitEdit(textField.getText()); | |
TableColumn nextColumn = getNextColumn(!t.isShiftDown()); | |
if (nextColumn != null) { | |
getTableView().edit(getTableRow().getIndex(), | |
nextColumn); | |
} | |
} | |
} | |
}); | |
} | |
private String getString() { | |
return getItem() == null ? "" : getItem().toString(); | |
} | |
private TableColumn<Person, ?> getNextColumn(boolean forward) { | |
List<TableColumn<Person, ?>> columns = new ArrayList<>(); | |
for (TableColumn<Person, ?> column : getTableView().getColumns()) { | |
columns.addAll(getLeaves(column)); | |
} | |
// There is no other column that supports editing. | |
if (columns.size() < 2) { | |
return null; | |
} | |
int currentIndex = columns.indexOf(getTableColumn()); | |
int nextIndex = currentIndex; | |
if (forward) { | |
nextIndex++; | |
if (nextIndex > columns.size() - 1) { | |
nextIndex = 0; | |
} | |
} else { | |
nextIndex--; | |
if (nextIndex < 0) { | |
nextIndex = columns.size() - 1; | |
} | |
} | |
return columns.get(nextIndex); | |
} | |
private List<TableColumn<Person, ?>> getLeaves( | |
TableColumn<Person, ?> root) { | |
List<TableColumn<Person, ?>> columns = new ArrayList<>(); | |
if (root.getColumns().isEmpty()) { | |
// We only want the leaves that are editable. | |
if (root.isEditable()) { | |
columns.add(root); | |
} | |
return columns; | |
} else { | |
for (TableColumn<Person, ?> column : root.getColumns()) { | |
columns.addAll(getLeaves(column)); | |
} | |
return columns; | |
} | |
} | |
} | |
} |
I run into the following issue
- Double click any cell to edit
- Click on any other row
- The cell that was previously selected will disappear
This can be resolved by adding the following line to the end of cancelEdit:
setContentDisplay(ContentDisplay.TEXT_ONLY);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a big problem is, if i scroll this table with vertical scrollbar, then it will select the wrong cell by mouse clicking on