javafx set onkeypressed for an editable comboboxtablecell - javafx

I have an editable tableview with editable column with this cell factory
tblColumn.setCellFactory(t -> {
ComboBoxTableCell myComboBoxTableCell = new ComboBoxTableCell();
myComboBoxTableCell.setOnKeyPressed(e -> {
System.out.println("key pressed");
KeyCode code = e.getCode();
System.out.println("code "+code);
});
myComboBoxTableCell.setComboBoxEditable(true);
return myComboBoxTableCell;
});
what i'm trying to do is to get letter typed in the editable comboBoxTableCell, but when i type, the onKeyPressed method is not fired. am i doing something wrong?
PLease help.

Related

How to modify the selected tableview according to the tableview cell

I have customized a Hyperlink cell here. I want the tableview to select the content when I click this link, but after I add Hyperlink, the tableview's selected seems to be invalid.
tb_uGoodUrl.setCellFactory(new Callback<TableColumn<GoodModel, String>, TableCell<GoodModel, String>>() {
#Override
public TableCell<GoodModel, String> call(TableColumn<GoodModel, String> param) {
TableCell<GoodModel, String> cell = new TableCell<GoodModel, String>() {
private final Hyperlink hyperlink = new Hyperlink();
{
hyperlink.setOnMouseClicked(event -> {
if(event.getClickCount() == 2){
String url = getItem();
hostServices.showDocument(url);
}
});
}
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
}else {
hyperlink.setText(getItem());
setGraphic(hyperlink);
}
}
};
return cell;
}
});
Click on the link, the cell is not selected
If the cell is not selected, a null exception will be reported when the following code is used.
TablePosition pos = tableView.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
// Item here is the table view type:
GoodModel item = tableView.getItems().get(row);
TableColumn col = pos.getTableColumn();
// this gives the value in the selected cell:
String data = (String) col.getCellObservableValue(item).getValue();
The effect you want to achieve is as follows
Rendering
You can manually select the cell, using the table's selection model, when the Hyperlink is clicked on.
// Assuming this code is inside a TableCell implementation
hyperlink.setOnAction(event -> {
event.consume();
getTableView().getSelectionModel().select(getIndex(), getTableColumn());
// show your document
});
I used the onAction property which will be fired when the Hyperlink has been clicked once. This is typical behavior for a hyperlink, but if you want to only perform the action on a double-click then you can keep using your onMouseClicked handler.
Note the above does not take into account multiple-selection mode.

How to add error popup for empty TextField input in Scenebuilder

I am making an application using Scenebuilder with JavaFX.
I have three inputs for a TableView:
Two TextField input1, input2.
One DatePicker.
When one or more of the input fields is empty and i click on the addButton, the object is added to the TableView.
How do I show an error popup which appears whenever i click on addButton and at least one field (input1, input2) is empty ?
addButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
if ((input1.getText() != null && !input1.getText().isEmpty()) &&
(input2.getText() != null && !input2.getText().isEmpty())){
//ADD CODE TO ADD THE ITEM HERE!
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Input fields empty");
alert.setContentText("Please fill all input fields");
alert.showAndWait();
}
}
});
PS : Here you can find different Alert Types depending on your needs.

How can I add GridPane(contains 3 Buttons) to the Cell of a JAvaFX TableView

I have a TableView named as "customers" with TableColumns related to customer details.
In the same TableView, I want to add one more TableColum as "Action" in which User should have possibilities to Add,View and delete Product details for particular Customer.
Tried multiple things by googling it but till now didn't get any solution for it.
Just add a TableColumn with a custom cellFactory. BTW: You probably want to use a single column/row for the Buttons, in which case you can simply use HBox or VBox, but you could of course replace the layout in the following code:
TableColumn<Product, Void> buttonColumn = new TableColumn<>("Action");
buttonColumn.setCellFactory(col -> new TableCell<Product, Void>() {
private final VBox container;
{
Button add = new Button("Add");
Button view = new Button("View");
Button delete = new Button("Delete");
delete.setOnAction(evt -> {
// delete this row item from TableView items
getTableView().getItems().remove(getIndex());
});
view.setOnAction(evt -> {
// call some method with the row item as parameter
viewProduct(getTableRow().getItem());
});
add.setOnAction(...);
container = new VBox(5, add, view, delete);
}
#Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : container);
}
});
tableView.getColumns().add(buttonColumn);

Lookup model details when constructing JavaFX context menuitem

Follow-up to this question.
I need to construct one of the MenuItems with some content from the row model.
row.getItem() is null as the row isnt initialized in the rowfactory callback.
Is there a way to defer the creation of the context menu when the row model has been populated?
For example, if the row as a String property name = "foo", the MenuItem should display "Add foo".
In the context of my answer to Determine a JavaFX table row details when reusing tableview context menu you would do
table.setRowFactory(t -> {
TableRow<Item> row = new TableRow<>();
ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem();
// ...
row.itemProperty().addListener((obs, oldItem, newItem) -> {
item1.setText(/* value depending on newItem... */);
});
// ...
}

Get TableView cell data using MouseEvent

I currently have a TableView with a custom CellFactory. I am trying to figure out how to get the core data underlying a cell when the mouse enters that cell.
I will be using this data to populate a label that is defined in another controller.
I've been trying to look at event filters and event handlers, and suspect the answer has something to do with using those appropriately, but I haven't been able to figure this out or find an answer.
Please let me know what code, if any, you would like to see. I am not even sure what would help at this point.
Register a mouse listener with the cell to handle mouseEntered events, and call getItem() on the cell to get the data:
TableView<MyRowType> table = ... ;
TableColumn<MyRowType, MyCellType> column = ... ;
column.setCellFactory ( c-> {
TableCell<MyRowType, MyCellType> cell = new TableCell<MyRowType, MyCellType>() {
#Override
public void updateItem(MyCellType item, boolean empty) {
super.updateItem(item, empty) ;
// your implementation here...
}
};
cell.setOnMouseEntered( e -> {
MyCellType item = cell.getItem();
if (item != null) {
// do whatever you need with item
}
});
return cell ;
});

Resources