Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle ListView item clicked action?

I have my JavaFX 2.0 application, where i need to make some action, after user clicked an item in ListView element. To construct user GUI i'm using FXML, in which i have something like this:

        <children>             <ListView fx:id="listView" GridPane.columnIndex="0"              GridPane.rowIndex="1" labelFor="$pane"              onPropertyChange="#handleListViewAction"/>         </children> 

And here is what i have in a Controller for this event:

        @FXML protected void handleListViewAction(ActionEvent event) {            System.out.println("OK");         } 

And here is an error, i recieve, when the scene, which is for this gui is constructed:

javafx.fxml.LoadException: java.lang.String does not define a property model for "property". at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(Unknown Source) at javafx.fxml.FXMLLoader$ValueElement.processEndElement(Unknown Source) at javafx.fxml.FXMLLoader.processEndElement(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at fxmlexample.FXMLExampleController.handleSubmitButtonAction(FXMLExampleController.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) at javafx.event.Event.fireEvent(Unknown Source) at javafx.scene.Node.fireEvent(Unknown Source) at javafx.scene.control.Button.fire(Unknown Source) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) at com.sun.javafx.scene.control.skin.SkinBase$5.handle(Unknown Source) at com.sun.javafx.scene.control.skin.SkinBase$5.handle(Unknown Source) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) at javafx.event.Event.fireEvent(Unknown Source) at javafx.scene.Scene$MouseHandler.process(Unknown Source) at javafx.scene.Scene$MouseHandler.process(Unknown Source) at javafx.scene.Scene$MouseHandler.access$1300(Unknown Source) at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) at com.sun.glass.ui.View.notifyMouse(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source) at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source) at java.lang.Thread.run(Thread.java:722) java.lang.NullPointerException at javafx.scene.Scene.doCSSPass(Unknown Source) at javafx.scene.Scene.access$2900(Unknown Source) at javafx.scene.Scene$ScenePulseListener.pulse(Unknown Source) at com.sun.javafx.tk.Toolkit.firePulse(Unknown Source) at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source) at com.sun.javafx.tk.quantum.QuantumToolkit$8.run(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source) at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source) at java.lang.Thread.run(Thread.java:722) 

And the last block of this exception (from here java.lang.NullPointerException) is looped.

like image 952
Victoria Agafonova Avatar asked Mar 15 '12 15:03

Victoria Agafonova


People also ask

How can you react to click events on an item of a ListView?

One potential way to achieve is it set your ListView to height="wrap_content" If you do that then you can have override onTouchEvent() for your activity to get the events from when the user touches the empty space.

How we can define what should happen when a list item gets clicked on?

This example demonstrates how do I handle the click event in ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which is better ListView or RecyclerView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.

How do you populate a ListView?

You need to do it through an ArrayAdapter which will adapt your ArrayList (or any other collection) to your items in your layout (ListView, Spinner etc.). This is what the Android developer guide says: A ListAdapter that manages a ListView backed by an array of arbitrary objects.


1 Answers

FXML attributes and values are directly mapped to FX API. So to find out how to write handler you can first create required entities by API.

It seems you want to add action on ListView element on mouse click, so you need to add mouse click handler. In API it looks next way:

    final ListView lv = new ListView(FXCollections.observableList(Arrays.asList("one", "2", "3")));     lv.setOnMouseClicked(new EventHandler<MouseEvent>() {          @Override         public void handle(MouseEvent event) {             System.out.println("clicked on " + lv.getSelectionModel().getSelectedItem());         }     }); 

Looking at that code you can find out what in FXML you need to overrided attribute onMouseClicked:

<ListView fx:id="listView" onMouseClicked="#handleMouseClick"/> 

And in controller you need to provide handler with MouseEvent parameter:

@FXML private ListView listView;  @FXML public void handleMouseClick(MouseEvent arg0) {     System.out.println("clicked on " + listView.getSelectionModel().getSelectedItem()); } 
like image 175
Sergey Grinev Avatar answered Oct 11 '22 02:10

Sergey Grinev