I need to click on the button that is already in a column to automatically select the row to be able to get and set the selected item. Otherwise, generate nullpointer. I was thinking of adding a listener that when I click the button select the row directly, but I do not know.
ScreenShot :
This is the code:
botonVisitar.setOnAction((ActionEvent event) -> {
TextInputDialog dialog1 = new TextInputDialog();
Stage stage1 = (Stage) dialog1.getDialogPane().getScene().getWindow();
stage1.getIcons().add(new Image(this.getClass().getResource("icono.jpg").toString()));
dialog1.setTitle("Visita:");
dialog1.setContentText("Ingresar Tipo de visita: (por ejemplo: llamada, mail, mensaje, etc)");
Optional<String> result1 = dialog1.showAndWait();
if (result1.isPresent()) {
TablaVisita.getSelectionModel().getSelectedItem().setTipoVisita(result1.get());
TablaVisita.getSelectionModel().getSelectedItem().setFechaVisita(LocalDate.now());
//If it is not selected i get nullPointer.
}
I assume you're doing this in a custom TableCell used with a cellFactory, which means you can use the TableRow to get the table item:
new TableCell<MyItem, Void>() {
private final Button botonVisitar = new Button("Visitar");
{
botonVisitar.setOnAction((ActionEvent event) -> {
...
if (result1.isPresent()) {
MyItem item = (MyItem) getTableRow().getItem();
item.setTipoVisita(result1.get());
item.setFechaVisita(LocalDate.now());
}
});
}
#Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : botonVisitar);
}
}
Related
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 change color of selected item in listview on button click permanently?
apsentButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
}
});
So, i have apsentButton and listview, and I want to when I select some item from listview and click on button to that selected view become green permanenetly, after click on some other item from listview I want that item that I seleceted first stay green????
Save the selected items in a observable collection and use a cellFactory returning cells that update on an item change or an update of the list. To choose different colors based on the selection/focus state, it's best to use a CSS stylesheet and PseudoClass to do the styling:
final PseudoClass marked = PseudoClass.getPseudoClass("marked");
final ListView<String> listView = new ListView<>();
for (int i = 1; i <= 100; i++) {
listView.getItems().add("item " + i);
}
final ObservableSet<String> markedItems = FXCollections.observableSet(new HashSet<>());
Button button = new Button("mark");
button.setOnAction(evt -> markedItems.addAll(listView.getSelectionModel().getSelectedItems()));
listView.setCellFactory(lv -> new ListCell<String>() {
final InvalidationListener l = o -> {
String item = getItem();
pseudoClassStateChanged(marked, item != null && markedItems.contains(item));
};
final WeakInvalidationListener listener = new WeakInvalidationListener(l);
{
markedItems.addListener(listener);
}
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item);
l.invalidated(null);
}
});
Stylesheet applied to ancestor/the scene:
.list-cell:marked {
-fx-base: green;
-fx-selection-bar: #00FFFF;
-fx-selection-bar-non-focused: #228822;
}
I have following problem in javafx tableview, my table view is connected to certain model, and normal CRUD operations work with no issues, I also added a column which is not connected to ANY data model, and it just contains a hyperlink on which user can click, and he is prompted with a pop up.
All of this work, what it does not work is, when I click on hyperlink I want to also pass the row value, and normally it works like
tableview.getSelectionModel().getSelectedItem();
But now it does not work, since i am not clicking directly a cell, but I click a hyperlink, and if I click first some row and then hyperlink, I get that row that I highlighted. Is there any way to select row when clicking hyperlink,so I don't have to first click row then hyperlink in same row.
public class RemoveCell<T> extends TableCell<T, Void> {
private final Hyperlink link;
private final Hyperlink link1;
private final HBox pane = new HBox();
public RemoveCell() {
link = new Hyperlink("Remove");
link1 = new Hyperlink("Edit");
pane.getChildren().addAll(link,link1);
link1.setOnAction(evt -> {
//lagerRet();
if(tableView.getSelectionModel().getSelectedItem()!=null) {
System.out.println("not null");
}
else {
System.out.println("null");
}
// remove row item from tableview
// ap.getChildren().removeAll();
//ap.getChildren().setAll(mcon.loadParent(FxmlView.CHART));
PopOver popsy = new PopOver();
try {
popsy.setContentNode(control.loadUni(FxmlView.POP));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//popsy.setContentNode(panemain);
popsy.headerAlwaysVisibleProperty().set(true);;
popsy.show(link);
});
link.setOnAction(evt -> {
// remove row item from tableview
System.out.println("a quick test");
});
}
#Override
protected void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
setGraphic(empty ? null : pane );
//setGraphic(empty ? null : link1 );
}
}
and lastly this how I populate column
testColumn.setCellFactory(tc -> new RemoveCell<>());
It's possible to access the row item via TableRow containing the TableCell.
T item = getTableRow().getItem();
It's also possible to get the index in TableView.items using TableCell.getIndex which allows for removal without searching for the item in the list first.
int itemIndex = getIndex();
getTableView().getItems().remove(itemIndex);
I have a TableView in my JavaFX application.
I would like to style differently row when it is double-clicked on it, and differently when it is single-clicked.
Here is what I achieve:
final PseudoClass doubleClickPseudoClass = PseudoClass.getPseudoClass("new");
setRowFactory(tableView -> {
final TableRow<Bean> row = new TableRow<Bean>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (! row.isEmpty())) {
row.pseudoClassStateChanged(doubleClickPseudoClass, true);
});
return row;
});
However, when the user doubles click on every new row, I want all previously double-clicked rows to be styled without applying "new" class:
row.pseudoClassStateChanged(doubleClickPseudoClass, false);
How can I do that?
Now I have cumulative styled all rows as they are double-clicked.
You shouldn't use TableRows to store the state themselves since new items may be assigned to a TableRow instance. Instead use a property to store the item double-clicked item and use a listener for styling the rows:
final ObjectProperty<Bean> doubleClickedObject = new SimpleObjectProperty<>();
setRowFactory(tableView -> new TableRow<Bean>() {
private void updateStyle() {
pseudoClassStateChanged(doubleClickPseudoClass, !isEmpty() && doubleClickedObject.get() == getItem());
}
private final InvalidationListener listener;
{
listener = o -> updateStyle();
doubleClickedObject.addListener(new WeakInvalidationListener(listener));
setOnMouseClicked(event -> {
if (!isEmpty() && event.getClickCount() == 2) {
doubleClickedObject.set(getItem());
}
});
}
#Override
protected void updateItem(Bean item, boolean empty) {
super.updateItem(item, empty);
updateStyle();
}
});
I have made treeview. I want to make treeview like this.
when I enter mouse to item, that item should change image.
when I clcick mouse to item, that item should chnage image.
I know how the way getSelectionMode()... but I don't know hover event.
Please help me.
Not sure if I understand you correct.
But to change your image as soon as you click an image use a selectedItemProperty listener:
treeView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue,
Object newValue) {
TreeItem<String> selectedItem = (TreeItem<String>) newValue;
// do something
}
});
If you want it as soon as you hover over the item you can use a hoverProperty on the row:
treeView.setRowFactory(tableView -> {
final TableRow<Person> row = new TableRow<>();
row.hoverProperty().addListener((observable) -> {
final YourItem yourItem = row.getItem();
if (yourItem.isHover() ) {
// do something
} else {
// do something
}
});
return row;
});
(this code is from the answer here)
I missread, its about a TreeView. You can try an onMouseEntered or similiar within a cellfactory:
treeView.setCellFactory(tv -> {
final Tooltip tooltip = new Tooltip();
TreeCell<Path> cell = new TreeCell<Path>() {
#Override
public void updateItem(Path item, boolean empty) {
super.updateItem(item, empty);
}
};
cell.setOnMouseClicked(e -> {
// do something
});
cell.setOnMouseEntered(e -> {
// do something
});
return cell ;
});