JavaFX RadioButton Event Handling - event-handling

What kind of JavaFX2 event is fired when selecting a radio button from a group and how can I handle it?

Given zodiacSigns is a group which radio buttons belong to,
zodiacSigns.selectedToggleProperty().addListener(new OnToggleHandler());
will the event handler OnToggleHandler to the button group (called toggle group in JavaFX). Below is the code for OnToggleHandler
private class OnToggleHandler implements ChangeListener<Toggle> {
#Override
public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1) {
dailyHoro.editReading(((RadioButton) t1).getText());
dailyHoro.print();
System.out.println("Old: " + ((RadioButton) t).getText() + ", New: " + ((RadioButton) t1).getText());
if (dailyHoro.getText() == null)
textEditor.setText("");
else
textEditor.setText(dailyHoro.getText());
}
}

Related

Swap the type of link depending on model object

I'm at complete loss how to proceed further:
I have panel with a DropDownChoice and a submit button next to it. Depending on the selected value of the DropDownChoice (Obtained upon the firing of a OnChangeAjaxBehavior attached to it, the submit button needs to either replace the whole panel with a different one, OR become an ExternalLink.
Currently, the code looks like that:
public class ReportSelectionPanel extends Panel {
protected OptionItem selectedOption ;
public ReportSelectionPanel(String id) {
super(id);
IModel<List<OptionItem>> choices = new AbstractReadOnlyModel() {
// Create a list of options to be displayed in the DropDownChoice
} ;
final IModel<OptionItem> optionModel =
new PropertyModel<OptionItem>(this,"selectedOption") ;
final DropDownChoice<OptionItem> options =
new DropDownChoice("selectChoice",optionModel,choices) ;
// I don't know what the button should be... Plain Button? A Link?
final Component button = ???
options.add( new OnChangeAjaxBehavior() {
protected void onUpdate(AjaxRequestTarget target) {
if ( selectedOption.getChild() == null ) {
// button becomes an ExternalLink.
// A new window will popup once button is clicked
} else {
// button becomes a Something, and upon clicking,
// this ReportSelectionPanel instance gets replaced by
// an new Panel instance, the type of which is
// selectedOption.getChild()
}
} ) ;
I'm really not quite sure what the commented lines should become to achieve the result. Any suggestions?
Thanks!
Eric
IMHO it's nicer to keep just one button and just react differently depending on the selected option:
final Component button = new AjaxButton("button") {
public void onClick(AjaxRequestTarget target) {
if (selectedOption.getChild() == null) {
PopupSettings popup = new PopupSettings();
popup.setTarget("'" + externalUrl + "'");
target.appendJavascript(popup.getPopupJavaScript());
} else {
ReportSelectionPanel.this.replaceWith(new ReportResultPanel("..."));
}
}
};
// not needed if options and button are inside a form
// options.add( new OnChangeAjaxBehavior() { } ) ;

What is the right usage for the SingleSelectionModel?

we would like to link from a CellTable to a property editor page. We use the SingleSelectionModel to get notified, when a user clicks on an item.
It is initialized like this:
private final SingleSelectionModel<Device> selectionModel = new SingleSelectionModel<Device>();
We then assign the selection change handler:
selectionModel.addSelectionChangeHandler(this);
Our selection change handler looks like this:
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Log.debug("DevicesPresenter: SelectionChangeEvent caught.");
Device selectedDevice = selectionModel.getSelectedObject();
if (selectedDevice != null) {
selectionModel.clear();
if (selectionModel.getSelectedObject() != null){
Log.debug("DevicesPresenter: selected item is " + selectionModel.getSelectedObject());
}
else{
Log.debug("DevicesPresenter: selected item is null");
}
deviceEditorDialog.setCurrentDevice(selectedDevice.getUuid());
// get the container data for this device
clientModelProvider.fetchContainersForDevice(selectedDevice.getUuid());
PlaceRequest request = new PlaceRequest.Builder()
.nameToken(NameTokens.deviceInfo)
.with("uuid", selectedDevice.getUuid())
.build();
Log.debug("Navigating to " + request.toString());
placeManager.revealPlace(request);
}
}
Now there are two issues: There always seem to be two SelectionChangeEvents at once and i really cannot see why. The other thing is: How is the right way do handle selection of items and the related clearing of the selection model? Do we do that the right way?
Thanks!
If you only want to get notified of "clicks" without keeping the "clicked" item selected, use a NoSelectionModel instead; no need to clear the selection model as soon as something is selected.
As for your other issue with being called twice, double-check that you haven't added your selection handler twice (if you can unit-test your DevicesPresenter, introspect the handlers inside the selection model for example)
In your line selectionModel.addSelectionChangeHandler(this); what does this refer?
Here my code how I use SingleSelectionModel
public class MyClass{
private final SingleSelectionModel<CountryDto> selectionModel = new SingleSelectionModel<CountryDto>();
...
public MyClass(){
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
CountryDto selected = selectionModel
.getSelectedObject();
if (selected != null) {
Window.alert("Selected country "+selected.getTitle());
}
}
});
}
}

How to fix the conflict between mySuggestBox.addKeyDownHandler & mySuggestBox.addSelectionHandler in GWT?

I have a SuggestBox. I want 2 things:
-1st, when user Type in a word if the SuggestOracle suggest any word & then if i select that word by hitting the Enter key on the selected word or clicking on that word it will call a methodX
-2st, suppose I typed a word into a suggest & SuggestOracle suggests NO word, then I want that when I hit Enter key it will fire methodX
This below code met the 1st requirement.
getView().getSuggestBox().addSelectionHandler(new SelectionHandler(){
#Override
public void onSelection(SelectionEvent event) {
methodX();
}
});
This below code met the 2nd requirement.
getView().getSuggestBox().addKeyDownHandler(new KeyDownHandler(){
#Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
methodX();
}
}
});
However, if i use both of them, then I got this problem.
FOr example, When I type "car" the suggest oracle show "car insurance" "car buy" & then when I use arrow down key to select "car buy" & hit the Enter key, then the system call methodX("car"); not "methodX("car buy")"
WHat is the problem? I think they got conflict or something
How to fix it?
I think you simply need to add a ValueChangeHandler to the ValueBox
getView().getSuggestBox()getValueBox().addValueChangehandler( ... );
As far as I remember, this event is triggered, if you hit enter in the Box.
There is no need to register two handlers. This can be achieved by single handler as shown below that will be fired event value is selected via mouse click or by pressing enter key.
suggestionBox.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {
#Override
public void onSelection(SelectionEvent<Suggestion> event) {
System.out.println("selection changed :" + event.getSelectedItem().getDisplayString());
methodX();
}
});
If nothing works then try with below handler.
suggestionBox.getValueBox().addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
System.out.println("value changed :" + event.getValue());
}
});
I think you should try to use a custom SelectionHandler class, and play with
getDisplayString and getReplacementString
here is a quick copy/paste of my code:
public class SearchStuff Composite implements SelectionHandler<ItemSuggestion> {
#Override
public void onSelection(SelectionEvent<ItemSuggestion> event) {
System.out.println(" getDisplayString( ) = " + event.getSelectedItem().getDisplayString()) ;
System.out.println("getReplacementString( ) = " + event.getSelectedItem().getReplacementString());
}
public class ItemSuggestion implements IsSerializable, com.google.gwt.user.client.ui.SuggestOracle.Suggestion{
#Override
public String getReplacementString() {
....
}
}

JavaFx how to hide popup when mouse is clicked on owner window?

Hi i have javafx app which has only one stage.On tab key press event of text field, a popup showed on primary stage of application. like below
private void tripNoKeyPressEventAction(KeyEvent event){
if(event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER) {
popup.show(GateIn.primaryStage);
}
}
popup.requestFocus();
popup.focusedProperty().addListener(new ChangeListener<Boolean>
() {
#Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
if(t1==false)
{
System.out.println("focus lost");
popup.hide();
}
}
});
I don't click on the popup and don't select anything in popup. I will just click on the stage behind it.I expect popup to be closed but It gives me IllegalArgumentException before executing popup's focusedProperty Listener.
If popup is on a different stage (other than primary stage of aaplication),based on stage focusedProperty() i can hide popup.
How to hide popup in case popup is shown on primary stage?
With FX 8, you can simply do
popup.setAutoHide(true)
You should set a event dispatcher for most top level window then all event will cross it.
In the popup window:
getScene().getWindow().setEventDispatcher((event, tail) -> {
if (event.getEventType() == RedirectedEvent.REDIRECTED) {
// RedirectedEvent is a box that contains original event from other target
RedirectedEvent ev = (RedirectedEvent) event;
if (ev.getOriginalEvent().getEventType() == MouseEvent.MOUSE_PRESSED) {
hide();
}
}else {
// if click in the popup window. handle the event by default
tail.dispatchEvent(event);
}
return null;
});
More information please see javafx.event.EventDispatcher

Is it possible to expand GWT droplist(ListBox) by keyboard?

I have a flextable full of listboxes set to listBox.setVisibleItemCount(1), so they act as droplists.
When clicked on with the left mouse button they expand and let the user select an item.
Is it possible to mimic the mouse click with a keyboard key?
I've already tried to add keypress handler to the listbox that will fire a mousedown native event, but that did nothing.
Anyone have any idead?
Thanks in advance
I haven't found a solution yet for my problem but I have this workaround that works for now:
listBox.addBlurHandler(new BlurHandler() {
public void onBlur(BlurEvent event) {
ListBox listBox = ((ListBox)event.getSource());
SelectElement.as(listBox.getElement()).setSize(1);
}
});
listBox.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event ) {
if (event.getCharCode() == 32) {
ListBox listBox = ((ListBox)event.getSource());
SelectElement.as(listBox.getElement()).setSize(listBox.getItemCount());
}
}
});