TableView data selection by mouse press

Well It has been quiet long time but I found it would be time for me to blog small post about making the table cell selection in JavaFX. Normally TableView has facility to select the cell or row by using Ctrl,Command (Shortcut Key) and Shift button. You can easily select the data using your keyboards. But when it comes for ease to user interaction using mouse is vital. So today we will go through how to create selection of data by mouse. Lets categorize them in two parts.

TableView has the default selection mode set to SelectionMode.SINGLE and we will need to change this mode to multiple by using

For making the TableRow selection in sequence we are going to use simple Mouse Press action. Actually the Table Data (Cells,Rows) has the property of mouse like drag,press,click. So to make the selection of data possible we will have to make use of draggable event of TableRow or TableCell. Lets get in with the TableRow selection by mouse.

TableRow Selection

For the demo we are going to use the Person as an Object data with few properties.

By using the rowFactory implementation we can easily make the selection of the Row by simple using only Mouse Movement. Similar to Row selection we can work on the Cell Selection as well.

TableCell Selection

Regarding this multi-selection we can press the “Ctrl” Key for selecting more rows/cell with existing data selected.

A sample demo is given in the youtube you can check that out : http://youtu.be/lf41ccYPToI
Source Code of the Demo is here : https://github.com/privatejava/javafx-tableview-data-selection

Enjoy Coding. 🙂

10 thoughts on “TableView data selection by mouse press”

  1. Pingback: JavaFX links of the week, February 17 // JavaFX News, Demos and Insight // FX Experience

  2. Hi, can you help me to load data using a tableview that was created on an fxml file. Please help me, it`s wery important to me.

  3. Hi,
    If you like to load data inside the fxml created Tableview firstly you will have to manipulate them in Controller class of that specific FXML. For eg.

    And in Controller class create the attribute

    and inside initialize() you can populate data by using tbl.setItems(YOUR-DATA-IN-OBSERVABLE-LIST-INSTANCE);

  4. Hi,

    This is selecting the cells which is visible in the tableview. Is there a way to auto scroll the table when the selection reaches the last visible row?

  5. The example works excellent, but if the table has more rows than only the visible rows will get selected, and scrollbar stay put. How can I make the drag to select the rows while scrolling down? Pls le tme know

  6. I initiated the selection of the rows by Drag gesture like you mentioned here and i can select all the visible rows, once you reach the end, the table won’t scroll down to select the rest of the rows as you are still draging. How can i achieve that. Thanks for your reply earlier

  7. Pingback: Java desktop links of the week, February 17 – Jonathan Giles

Leave a Reply