Radio Button Change Event JavaFx - events

i have a lot of HBoxes with some different components inside ( RadioButton, Labels, Buttons ...).
The RadioButtons are in a ToggleGroup, so only 1 Radio Button can be selected.
I want to add a OnChange - Event to the Radio Button. If the RadioButton would be unselected there should be a Event-Trigger. How can i add the Event to the Radio-Button?
At the moment i have a code like this, but it doesn't have the function i want to have.
radio.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
if(!radio.isSelected()){
ivTriangleImg.setRotate(iRotateCoord2);
btnTriangle.setGraphic(ivTriangleImg);
}
if(group!=null){
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#Override
public void changed(
ObservableValue<? extends Toggle> arg0,
Toggle arg1, Toggle arg2) {
}
});
}
}
});
I use JavaFX 2.0 and Java 1.7 so i can not use Lambda Functions or the special component functions of JavaFx8 / Java 1.8

The state of JavaFX controls is represented by observable properties. You can access these properties with control.propertyNameProperty() and add ChangeListeners to them:
radioButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> obs, Boolean wasPreviouslySelected, Boolean isNowSelected) {
if (isNowSelected) {
// ...
} else {
// ...
}
}
});

radio.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
if(!radio.isSelected()){
ivTriangleImg.setRotate(iRotateCoord2);
btnTriangle.setGraphic(ivTriangleImg);
}
if(group!=null){
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#Override
public void changed(
ObservableValue<? extends Toggle> arg0,
Toggle arg1, Toggle arg2) {
if(!radio.isSelected()&&ivTriangleImg.getRotate()!=iRotateCoord1){
ivTriangleImg.setRotate(iRotateCoord1);
btnTriangle.setGraphic(ivTriangleImg);
}
}
});
}
}
});
This would work for my Question. I have done a little mistake, so i don't check the Radio-Style. It works fine now... Sorry for this Question.
Is there a possibility to check the Event at the Radio Button and not at his group?

In case you have many radio buttons you dont want have a single EventHandlers for each one of them....
Here is an example with 6 radio buttons but just one ChangeListener where the events are all process in a centralized method.
This detects programatic events, keyboard events and mouse events
Note that the listener is added to the ToggleGroup
public class ReportOptionsPane extends BorderPane implements ChangeListener<Toggle> {
RadioButton radioFoldersOnly ;
RadioButton radioAllItems ;
RadioButton radioArtifactsOnly ;
RadioButton radioStatsOrderByOccurrence ;
RadioButton radioStatsOrderByName ;
public ReportOptionsPane(ReportOptions rep) {
super() ;
ToggleGroup tg = new ToggleGroup();
radioFoldersOnly = new RadioButton("Folders only") ;
radioAllItems = new RadioButton("Files & Folders") ;
radioArtifactsOnly = new RadioButton("Artifacts only") ;
radioFoldersOnly.setToggleGroup(tg);
radioAllItems.setToggleGroup(tg);
radioArtifactsOnly.setToggleGroup(tg);
//You add the listener to the toogle group of your radio buttons
tg.selectedToggleProperty().addListener(this);
ToggleGroup tg2 = new ToggleGroup();
radioStatsOrderByOccurrence = new RadioButton("Order stats by occurrence") ;
radioStatsOrderByName = new RadioButton("Order stats by name") ;
radioStatsOrderByOccurrence.setToggleGroup(tg2);
radioStatsOrderByName.setToggleGroup(tg2);
//You can reuse the listener for the second toogle group of your radio buttons
tg2.selectedToggleProperty().addListener(this);
}
//Then you handle all in a centralized place
#Override
public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
if(newValue.equals(radioFoldersOnly)) {
//react to radio button being selected
} else if(newValue.equals(radioAllItems)) {
//react to radio button being selected
} else if(newValue.equals(radioArtifactsOnly)) {
//react to radio button being selected
} else if(newValue.equals(radioStatsOrderByName)) {
//react to radio button being selected
} else if(newValue.equals(radioStatsOrderByOccurrence) {
//react to radio button being selected
}
}
}

Related

Unity GUI button to Pause and Rewind MovieTexture [duplicate]

How to detect UI object on Canvas on Touch in android?
For example, I have a canvas that have 5 objects such as Image, RawImage, Buttons, InputField and so on.
When I touch on Button UI object Then do something. Each button do different process when clicked depending.
The code will look like:
private void Update()
{
if (Input.touches.Length <= 0) return;
for (int i = 0; i < Input.touchCount; i++)
{
if (Button1.touch)
if (Input.GetTouch(i).phase == TouchPhase.Began)
login();
else if (Button2.touch && Input.GetTouch(i).phase == TouchPhase.Began)
LogOut();
}
}
So how to do it?
Second:
How to detect Gameobject get touch? Is it same with that above or not?
You don't use the Input API for the new UI. You subscribe to UI events or implement interface depending on the event.
These are the proper ways to detect events on the new UI components:
1.Image, RawImage and Text Components:
Implement the needed interface and override its function. The example below implements the most used events.
using UnityEngine.EventSystems;
public class ClickDetector : MonoBehaviour, IPointerDownHandler, IPointerClickHandler,
IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler,
IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("Drag Begin");
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragging");
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("Drag Ended");
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Mouse Down: " + eventData.pointerCurrentRaycast.gameObject.name);
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Mouse Enter");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Mouse Exit");
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Mouse Up");
}
}
2.Button Component:
You use events to register to Button clicks:
public class ButtonClickDetector : MonoBehaviour
{
public Button button1;
public Button button2;
public Button button3;
void OnEnable()
{
//Register Button Events
button1.onClick.AddListener(() => buttonCallBack(button1));
button2.onClick.AddListener(() => buttonCallBack(button2));
button3.onClick.AddListener(() => buttonCallBack(button3));
}
private void buttonCallBack(Button buttonPressed)
{
if (buttonPressed == button1)
{
//Your code for button 1
Debug.Log("Clicked: " + button1.name);
}
if (buttonPressed == button2)
{
//Your code for button 2
Debug.Log("Clicked: " + button2.name);
}
if (buttonPressed == button3)
{
//Your code for button 3
Debug.Log("Clicked: " + button3.name);
}
}
void OnDisable()
{
//Un-Register Button Events
button1.onClick.RemoveAllListeners();
button2.onClick.RemoveAllListeners();
button3.onClick.RemoveAllListeners();
}
}
If you are detecting something other than Button Click on the Button then use method 1. For example, Button down and not Button Click, use IPointerDownHandler and its OnPointerDown function from method 1.
3.InputField Component:
You use events to register to register for InputField submit:
public InputField inputField;
void OnEnable()
{
//Register InputField Events
inputField.onEndEdit.AddListener(delegate { inputEndEdit(); });
inputField.onValueChanged.AddListener(delegate { inputValueChanged(); });
}
//Called when Input is submitted
private void inputEndEdit()
{
Debug.Log("Input Submitted");
}
//Called when Input changes
private void inputValueChanged()
{
Debug.Log("Input Changed");
}
void OnDisable()
{
//Un-Register InputField Events
inputField.onEndEdit.RemoveAllListeners();
inputField.onValueChanged.RemoveAllListeners();
}
4.Slider Component:
To detect when slider value changes during drag:
public Slider slider;
void OnEnable()
{
//Subscribe to the Slider Click event
slider.onValueChanged.AddListener(delegate { sliderCallBack(slider.value); });
}
//Will be called when Slider changes
void sliderCallBack(float value)
{
Debug.Log("Slider Changed: " + value);
}
void OnDisable()
{
//Un-Subscribe To Slider Event
slider.onValueChanged.RemoveListener(delegate { sliderCallBack(slider.value); });
}
For other events, use Method 1.
5.Dropdown Component
public Dropdown dropdown;
void OnEnable()
{
//Register to onValueChanged Events
//Callback with parameter
dropdown.onValueChanged.AddListener(delegate { callBack(); });
//Callback without parameter
dropdown.onValueChanged.AddListener(callBackWithParameter);
}
void OnDisable()
{
//Un-Register from onValueChanged Events
dropdown.onValueChanged.RemoveAllListeners();
}
void callBack()
{
}
void callBackWithParameter(int value)
{
}
NON-UI OBJECTS:
6.For 3D Object (Mesh Renderer/any 3D Collider)
Add PhysicsRaycaster to the Camera then use any of the events from Method 1.
The code below will automatically add PhysicsRaycaster to the main Camera.
public class MeshDetector : MonoBehaviour, IPointerDownHandler
{
void Start()
{
addPhysicsRaycaster();
}
void addPhysicsRaycaster()
{
PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
}
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
//Implement Other Events from Method 1
}
7.For 2D Object (Sprite Renderer/any 2D Collider)
Add Physics2DRaycaster to the Camera then use any of the events from Method 1.
The code below will automatically add Physics2DRaycaster to the main Camera.
public class SpriteDetector : MonoBehaviour, IPointerDownHandler
{
void Start()
{
addPhysics2DRaycaster();
}
void addPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
//Implement Other Events from Method 1
}
Troubleshooting the EventSystem:
No clicks detected on UI, 2D Objects (Sprite Renderer/any 2D Collider) and 3D Objects (Mesh Renderer/any 3D Collider):
A.Check that you have EventSystem. Without EventSystem it can't detect clicks at-all. If you don't have have it, create it yourself.
Go to GameObject ---> UI ---> Event System. This will create an EventSystem if it doesn't exist yet. If it already exist, Unity will just ignore it.
B.The UI component or GameObject with the UI component must be under a Canvas. It means that a Canvas must be the parent of the UI component. Without this, EventSystem will not function and clicks will not be detected.
This only applies to UI Objects. It doesn't apply to 2D (Sprite Renderer/any 2D Collider) or 3D Objects (Mesh Renderer/any 3D Collider).
C.If this is a 3D Object, PhysicsRaycaster is not attached to the camera. Make sure that PhysicsRaycaster is attached to the camera. See #6 above for more information.
D.If this is a 2D Object, Physics2DRaycaster is not attached to the camera. Make sure that Physics2DRaycaster is attached to the camera. See #7 above for more information.
E.If this is a UI object you want to detect clicks on with the interface functions such as OnBeginDrag, OnPointerClick, OnPointerEnter and other functions mentioned in #1 then the script with the detection code must be attached to that UI Object you want to detect click on.
F.Also, if this is a UI Object you want to detect clicks on, make sure that no other UI Object is in front of it. If there is another UI in front of the one you want to detect click on, it will be blocking that click.
To verify that this is not the issue, disable every object under the Canvas except the one you want to detect click on then see if clicking it works.
You can add an EventTrigger Componenet to Your UI elements that already have these Events you just have to pass method/Function on specific event.
You could use OnMouseDown as well. OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. This event is sent to all scripts of the Collider or GUIElement.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement; // The new load level needs this
public class ExampleClass : MonoBehaviour
{
void OnMouseDown()
{
// Edit:
// Application.LoadLevel("SomeLevel");
// Application.LoadLevel() is depreciating but still works
SceneManager.LoadScene("SomeLevel"); // The new way to load levels
}
}
Do not use OnMouseDown() for mobile performance and multi-touch issues.
This code works on UI Objects for multi-touches
In my answer, I use Image element with a "Button" tag and it has a ButtonController script with a ButtonDown() public method that should be called when user touches the Image element.
Note: Image element has a 2D Collider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TouchScript : MonoBehaviour
{
void Update()
{
PointerEventData pointer = new PointerEventData(EventSystem.current);
List<RaycastResult> raycastResult = new List<RaycastResult>();
foreach (Touch touch in Input.touches)
{
if(touch.phase.Equals(TouchPhase.Began))
{
pointer.position = touch.position;
EventSystem.current.RaycastAll(pointer, raycastResult);
foreach(RaycastResult result in raycastResult)
{
if(result.gameObject.tag == "Button")
{
result.gameObject.GetComponent<ButtonController>().ButtonDown();
}
}
raycastResult.Clear();
}
}
}
}

Android action bar tabs

Currently I'm working with action bar tabs they are shown perfectly with 4.1 device but when i run on same screen size in lower version 4.0 then the action bar tabs are shown as "ActionBar spinner navigation"
I want them to be shown like this one:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpened(getIntent());
ParseUser currentUser = ParseUser.getCurrentUser();
if(currentUser == null) {
navigateToLogin();
} else {
Log.i(TAG, currentUser.getUsername());
}
final ActionBar actionBar = getActionBar();
//actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionPargerAdapter = new SectionPargerAdapter(this, getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionPargerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
for (int i = 0; i < mSectionPargerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText( mSectionPargerAdapter.getPageTitle(i))
.setTabListener(MainActivity.this));}
}
});
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
This is optimization done by system. The second view is called stacked action bar and first is called action bar with spinner. You can achieve what you want by first adding all tabs and then setting the mode of action bar as
setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)

Prevent selection of Tree Cell from right-click on Tree View Cell

I'm developing a custom TreeView object.
I'm using a custom cellFactory to provide the TreeCell objects of my TreeView.
This allows me to install custom Context Menu on the various cells, depending on the Item they are displaying.
But I'm not entirely satisfied with the behaviour.
When left-clicking on cell, it gets selected (OK)
But when right-clicking a cell, the context menu is displayed (OK) but the cell is also selected. (NOK)
How can I change this behaviour ?
I tried to implement an eventFilter on the tree view, to consume the event if it is a right-click but this doesn't change anything, the above behaviour still applies.
addEventFilter(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.SECONDARY) {
event.consume();
}
}
});
setCellFactory(new Callback<TreeView<TreeDisplayable>, TreeCell<TreeDisplayable>>() {
#Override
public TreeCell<TreeDisplayable> call(
final TreeView<TreeDisplayable> treeView) {
return new TreeDisplayableTreeCell(owner, javaModel);
}
});
public class TreeDisplayableTreeCell extends TreeCell<TreeDisplayable> {
...
#Override
public void updateItem(TreeDisplayable item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setText(getItem().treeViewString());
setGraphic(item.getPic());
if (getTreeItem().getParent() == null) {
// it means it's the root node
setContextMenu(new RootItemContextMenu(javaModel, owner));
} else {
setContextMenu(new TreeItemContextMenu(javaModel, owner,getTreeItem().getValue()));
}
}
}
}
Reacting on Tony's comment
Creating a custom EventDispatcher does the trick.
public class TreeEventDispatcher implements EventDispatcher {
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if (mouseEvent.getButton() == MouseButton.SECONDARY) {
event.consume();
} else {
event = tail.dispatchEvent(event);
}
} else {
event = tail.dispatchEvent(event);
}
return event;
}
}
The behaviour is identical for all events, except the right click event, which is consumed, thus preventing the right-click selection of any TreeCell.
Luckily enough, the context menu is still displayed on right click (although I don't understand why ...) Does anybody have a clue ?
Previous Facewindu answer is actually working, but there is another way to achieve that behavior and still have context menu appearing on right click:
treeView.addEventFilter(MOUSE_PRESSED, event -> {
if (event.isSecondaryButtonDown()) {
Node text = (Node) event.getTarget();
TreeCell<...> treeCell = (TreeCell<...>) text.getParent();
treeCell.getContextMenu().show(treeCell, 0, 0);
event.consume();
}
});

GWT hold mouse button

I'm working with GWT and want to do an action when the user holds the left mouse button on a GWT button. But I can't find the right event handler or another solution for that problem.
Is there a way in GWT to klick on a button, hold the mouse button and do the same action again and again till the mouse button is released?
Button scrollUpBtn = new Button("Top");
scrollUpBtn.setWidth("66px");
scrollUpBtn.addMouseDownHandler(new MouseDownHandler() {
#Override
public void onMouseDown(MouseDownEvent event) {
//handCards.setVerticalScrollPosition(handCards.getVerticalScrollPosition() - 10);
mouseUp = true;
}
});
scrollUpBtn.addMouseUpHandler(new MouseUpHandler() {
#Override
public void onMouseUp(MouseUpEvent event) {
mouseUp = false;
}
});
scrollUpBtn.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if (mouseUp == true) {
handCards.setVerticalScrollPosition(handCards.getVerticalScrollPosition() - 10);
}
}
});
Step 3 in Andrei's answer assumes that the KeyDownEvent will keep firing. I'm not sure if that's so..
An alternative would be to use the Down & Up handlers to start/stop a repeating timer which carries out your action. You can then set the repeat interval based on how often you want your action to be carried out. Remember that this runs as single threaded JavaScript, so if you carry out lengthy processing things will slow down and the scheduled intervals will not be on time.
Button btn= new Button("Button");
final Timer actionTimer = new Timer() {
#Override
public void run() {
// Your action here
System.out.println("Doing something!");
}
};
btn.addMouseDownHandler(new MouseDownHandler() {
#Override
public void onMouseDown(MouseDownEvent event) {
// Choose the appropriate delay
actionTimer.scheduleRepeating(1000);
}
});
btn.addMouseUpHandler(new MouseUpHandler() {
#Override
public void onMouseUp(MouseUpEvent event) {
actionTimer.cancel();
}
});
Add MouseDownHandler to your widget. When it fires, it should set a flag to true, e.g. mouseDown = true;
Add MouseUpHandler to your widget. When it fires, it should set a flag to false, e.g. mouseDown = false;
Add KeyDownHandler to your widget. When if fires, check if flag is true - then do something. If flag is false, don't do it.

shows system menu when user click in each row of a verticalfield manager

My application using a 'vertical field' manager and i add some text to it using a custonField class.The vertical manager constructor is:
scrollingRegion = new VerticalFieldManager(
USE_ALL_HEIGHT| VERTICAL_SCROLL | VERTICAL_SCROLLBAR|USE_ALL_WIDTH);
I also craete menu using:
protected void makeMenu(Menu menu, int context) {
menu.add(_imageMenuItem);
//super.makeMenu(menu, context);
}
class ImageMenuItem extends MenuItem {
/**
* Creates a new MenuDemoMenuItem object
*/
ImageMenuItem() {
super("Login Screen", 0, 0);
}
public boolean onMenu(int i) {
return false;
}
public void run() {
UiApplication app = (UiApplication) getApplication();
app.pushScreen(new LoginScreen());
}
}
My problem is when I click in the vertical field(which uses a table row manager), the menu is displayed. How can I avoid it? Can any one suggest a solution
if the item is ButtonField you can consume click by define it in the constructor
ButtonField btn = new ButtonField("Login", ButtonField.CONSUME_CLICK);

Resources