Reputation: 51
I am trying to create a TableView in JavaFX where the user can change the value of the column header by double clicking on the table header. A Textbox should appear and after filling in a new value, the value should be assigned to the column header.
Upvotes: 0
Views: 4801
Reputation: 51525
A slightly different approach than outlined in James' comment:
Note that the TableColumnHeader's package is version-dependent, for fx8 it's considered internal api residing in com.xx.skin, for fx9 it's public in javafx.xx.skin.
The example:
public class TableHeaderWithInput extends Application {
TableView<Locale> table;
protected void installHeaderHandler(Observable s) {
table.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
if (e.isPrimaryButtonDown() && e.getClickCount() > 1) {
EventTarget target = e.getTarget();
TableColumnBase<?, ?> column = null;
while (target instanceof Node) {
target = ((Node) target).getParent();
// beware: package of TableColumnHeader is version specific
if (target instanceof TableColumnHeader) {
column = ((TableColumnHeader) target).getTableColumn();
if (column != null) break;
}
}
if (column != null) {
TableColumnBase<?,?> tableColumn = column;
TextField textField = new TextField(column.getText());
textField.setMaxWidth(column.getWidth());
textField.setOnAction(a -> {
tableColumn.setText(textField.getText());
tableColumn.setGraphic(null);
});
textField.focusedProperty().addListener((src, ov, nv) -> {
if (!nv) tableColumn.setGraphic(null);
});
column.setGraphic(textField);
textField.requestFocus();
}
e.consume();
}
});
}
private Parent getContent() {
table = new TableView<>(FXCollections.observableArrayList(
Locale.getAvailableLocales()));
table.setTableMenuButtonVisible(true);
// quick hack: don't let sorting interfere ...
table.setSortPolicy(e -> {return false;});
TableColumn<Locale, String> countryCode = new TableColumn<>("CountryCode");
countryCode.setCellValueFactory(new PropertyValueFactory<>("country"));
TableColumn<Locale, String> language = new TableColumn<>("Language");
language.setCellValueFactory(new PropertyValueFactory<>("language"));
table.getColumns().addAll(countryCode, language);
table.skinProperty().addListener(this::installHeaderHandler);
BorderPane pane = new BorderPane(table);
return pane;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(getContent(), 800, 400));
primaryStage.setTitle(FXUtils.version());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(TableHeaderWithInput.class.getName());
}
Upvotes: 1