2 Replies Latest reply: Nov 18, 2012 9:15 PM by 973901 RSS

    Finding list item, table cell, tree node, or control by location

      One of my favorite features about swing was the ability to get a UI component reference via Container.getComponentAt(int x, int y), or a particular list item via JList.locationToIndex(Point p) (or other similar methods). I haven't been able to find the same or similar functionality in JFX so far... can someone point me in the right direction?

        • 1. Re: Finding list item, table cell, tree node, or control by location
          I don't know a way. When I started with JavaFX I had the same question; however I found I don't actually need to do this. In Swing I always needed this when I was, say, processing mouse clicks on a JList. In JavaFX, you can register event handlers with the cells directly. This doesn't work in swing (at least, not without a huge amount of effort). So whereas in Swing the way to map a mouse click on a JList to the corresponding list item was to register a listener with the JList and then pass the mouse click coordinates to locationToIndex(...), in JavaFX you just register the listener with the cell:
          import javafx.application.Application;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.ListCell;
          import javafx.scene.control.ListView;
          import javafx.scene.input.MouseEvent;
          import javafx.scene.layout.BorderPane;
          import javafx.stage.Stage;
          import javafx.util.Callback;
          public class ListTest extends Application {
            public void start(Stage primaryStage) throws Exception {
              BorderPane root = new BorderPane();
              ListView<String> listView = new ListView<String>();
              listView.getItems().addAll("Apples", "Oranges", "Grapefruit", "Bananas");
              listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
                public ListCell<String> call(ListView<String> param) {
                  return new CustomListCell();
              Scene scene = new Scene(root, 300, 300);
            private class CustomListCell extends ListCell<String> {
              private final EventHandler<MouseEvent> listener = new EventHandler<MouseEvent>() {
                public void handle(MouseEvent event) {
                  System.out.printf("Mouse clicked on list cell with %s (item number %d)",getItem(), getIndex());
                  if (isEmpty()) {
                    System.out.print(" (Empty cell)");
              private CustomListCell() {
                this.addEventHandler(MouseEvent.MOUSE_CLICKED, listener);
              public void updateItem(String item , boolean empty) {
                super.updateItem(item, empty);
                if (! empty) {
                } else {
            public static void main(String[] args) {launch(args);}
          Of course, this might not be your use case, and you may really need this functionality. Someone else may have a more direct response...
          • 2. Re: Finding list item, table cell, tree node, or control by location
            In most cases I like to use as little code as possible, so subclassing the list cells didn't seem very attractive to me. Also, at least from my experience with TableView, much of the underlying API is not public or documented, and I haven't found an easy way yet of enumerating all of the nodes a particular event will touch before reaching its target. So getting an object reference to the component I need to address is sometimes tedious. Getting elements by coordinates just seems natural to me. Having said that, I do love JFX's event model, and with your outline I should have no problem doing what I need to do. Your response was complete and answered my question well; I very much appreciate the time and effort.

            I have a similar question up (Swing functionality missing in JavaFX) that I have been DYING to find info on. If you know anything on that subject as well, I would love you forever.
            Alternative to SpringLayout?

            And I promise I will be active on these forums answering questions and being more helpful than a high maintenance knowledge-sucker as soon as I get a solid handle on this stuff. =P