JavaFX Split Menu Button Arrow Trigger Event save clicked item in string - javafx

I have a SplitMenuButton, and I can't seem to find a way to trigger an event when the user clicks the arrow next to the button.
I would like to select item from dropdown(when mouse clicked on that item), display that in splitmenubutton and store value of that item in string to send to database.
I am not sure which event can do that, and I can not find any info on this either.

You can do something like.
for (final MenuItem item : smb.getItems()) {
item.setOnAction((event) -> {
System.out.println("Selected!");
});
}

Related

Xamarin forms iOS CollectionView steals focus after ItemsSource property is changed

I have a page with a collection view and a search bar that filters its contents.
I want the filtering to happen as the user types in the search bar, so I bind the TextChanged event of the SearchBar to a command in the view model, like so
var eventToCommandBehavior = new EventToCommandBehavior()
{
EventName = nameof(searchBar.TextChanged),
};
eventToCommandBehavior.SetBinding(EventToCommandBehavior.CommandProperty, nameof(MyViewModel.StartOrResetSearchTimerCommand));
searchBar.Behaviors.Add(eventToCommandBehavior);
In the view model:
public ICommand StartOrResetSearchTimerCommand => new Command(() =>
{
StartOrResetSearchTimer();
});
private void StartOrResetSearchTimer()
{
if (!timerStarted)
{
searchTimer = new Timer(_ => PerformSearch(), null, searchTimeout, searchTimeout);
timerStarted = true;
}
else
ResetTimer();
}
private void PerformSearch()
{
//my code
OnPropertyChanged(collectionViewItemsSourceBinding);
}
The StartOrResetSearchTimerCommand filters the ItemsSource binding, and calls OnPropertyChanged(itemsSourceBinding) to update the UI.
On Android and UWP everything works as expected. However on iOS, when OnPropertyChanged is called, the focus moves out of the search bar, resulting to the soft keyboard being closed after each keyboard input.
Has anyone else encountered this? Any suggestions?
I have already tried not using this approach, and only filter the ItemsSource when the search button is pressed, which works, when there is something to search for (ie, there is some input in the search bar)
When the search bar text is empty (ie after Backspace) then the search button is greyed out.
Update
For now, I am using this workaround:
Perfom the search only when the search button is pressed, and on TextChanged, check if the text is empty and reset the ItemsSourceworka
TextChanged is called anytime the text in the query box is changed. You can use this event to update your ItemsSource when the Text of the searchbar changes. You can refer to this part of the official example. First, given your ItemsSource, when you enter text in the searchbar, call the OnTextChanged event to update your ItemsSource, so that the real-time search keyboard will not lose focus.

run time error opening a new window by selecting a table content

I have the following slot from a push-button click that opens a new window.
void StageTwoPatients::on_pushButton_Open_clicked()
{
QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0);
this-> close();
StageOneMain *newPatient = new StageOneMain(selection[0].data().toString(),this);
newPatient-> show();
}
Here user needs to select a row from a table and click pushbutton to open a new window.
If the user dont select a row accidentally and click the push button I get runtime error as shown here. How do I tell user using a QMessageBox that they should select a row before click push button?
Thanks in advance.
If the user dont select a row accidentally and click the push button I get runtime error
Simply do nothing if there is no selection.
void StageTwoPatients::on_pushButton_Open_clicked()
{
QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0);
if (selection.empty()){
return;
}
...
}

ZKOSS: onClick() working differently for mouse click vs mouse tap

I am trying to disable double clicking a Help label which is an anchor to open the Help window.
<p:a id="helpClick" onClick="help()">
<label value="Help" style="color:#FFFFFF;" />
</p:a>
When onClick() event is triggered once, either by Mouse click or Tapping the touchpad once, the help() method is being invoked.
void help() {
flag = true;
this.helpClick.setDisabled(true);
Window popupWindow = null;
popupWindow = (Window) Executions.createComponents("/zul/mainHelp.zul",
null, null);
this.popupWindow.setClosable(true);
popupWindow.addEventListener("onClose", new EventListener() {
void onEvent(Event event) throws Exception {
this.helpClick.setDisabled(false);
}
});
}
is the code which i added to handle the anchor tag with the id helpClick.
This is working perfectly fine when i use mouse clicks. For the first click, the window gets opened and simultaneously the Label is not taking any more click events.
When i try the same with mouse tap(using the touchpad), two single clicks are being triggered.
I have used onClick() to capture the event.
I am trying to disable the Label once it is clicked and the window is opened. Only after the window gets closed, i am enabling the label.
This is working totally fine when i use mouse clicks but not when i use tap.
With tapping, the label is taking multiple clicks which isnt the case with Mouse Click.
Without seeing code it's difficult to provide advice but maybe you can capture the onDoubleClick event and ignore it or forward it to the to the same listener as your onClick event.
... forward="onClick=onHelpClick,onDoubleClick=onHelpClick" ...
After question edit:
It sounds like a bug if you can double click a disabled component. One thing you could try is set your link to autodisable <p:a id="helpClick" onClick="help()" autodisable="self"> as per A component documentation

I have a table with records. No matter which View button I click, the bottom record is displayed

I have a table with a list of records. Each record has a View button. But no matter which View button I click, the bottom record (Acme6) gets opened?
enter image description here
It looks like you are sharing datasource for your list and details pages. If my assumption is correct, then you need to modify your view button event handler as follow:
// onClick event handler for View button
var rowItem = widget.datasource.item;
var listDatasource = widget.parent.parent.datasource;
listDatasource.selectKey(rowItem._key);
app.showPage(app.pages.DetailsPage);

How do I create a JavaFX Alert with a check box for "Do not ask again"?

I would like to use the standard JavaFX Alert class for a confirmation dialog that includes a check box for "Do not ask again". Is this possible, or do I have to create a custom Dialog from scratch?
I tried using the DialogPane.setExpandableContent() method, but that's not really what I want - this adds a Hide/Show button in the button bar, and the check box appears in the main body of the dialog, whereas I want the check box to appear in the button bar.
Yes, it is possible, with a little bit of work. You can override DialogPane.createDetailsButton() to return any node you want in place of the Hide/Show button. The trick is that you need to reconstruct the Alert after that, because you will have got rid of the standard contents created by the Alert. You also need to fool the DialogPane into thinking there is expanded content so that it shows your checkbox. Here's an example of a factory method to create an Alert with an opt-out check box. The text and action of the check box are customizable.
public static Alert createAlertWithOptOut(AlertType type, String title, String headerText,
String message, String optOutMessage, Consumer<Boolean> optOutAction,
ButtonType... buttonTypes) {
Alert alert = new Alert(type);
// Need to force the alert to layout in order to grab the graphic,
// as we are replacing the dialog pane with a custom pane
alert.getDialogPane().applyCss();
Node graphic = alert.getDialogPane().getGraphic();
// Create a new dialog pane that has a checkbox instead of the hide/show details button
// Use the supplied callback for the action of the checkbox
alert.setDialogPane(new DialogPane() {
#Override
protected Node createDetailsButton() {
CheckBox optOut = new CheckBox();
optOut.setText(optOutMessage);
optOut.setOnAction(e -> optOutAction.accept(optOut.isSelected()));
return optOut;
}
});
alert.getDialogPane().getButtonTypes().addAll(buttonTypes);
alert.getDialogPane().setContentText(message);
// Fool the dialog into thinking there is some expandable content
// a Group won't take up any space if it has no children
alert.getDialogPane().setExpandableContent(new Group());
alert.getDialogPane().setExpanded(true);
// Reset the dialog graphic using the default style
alert.getDialogPane().setGraphic(graphic);
alert.setTitle(title);
alert.setHeaderText(headerText);
return alert;
}
And here is an example of the factory method being used, where prefs is some preference store that saves the user's choice
Alert alert = createAlertWithOptOut(AlertType.CONFIRMATION, "Exit", null,
"Are you sure you wish to exit?", "Do not ask again",
param -> prefs.put(KEY_AUTO_EXIT, param ? "Always" : "Never"), ButtonType.YES, ButtonType.NO);
if (alert.showAndWait().filter(t -> t == ButtonType.YES).isPresent()) {
System.exit();
}
And here's what the dialog looks like:

Resources