How can I select multiple cells in tableview with javafx only by mouse?

I have an application with a tableview in javafx and i want to select multiple cells only by mouse (something like the selection which exists in excel).I tried with setOnMouseDragged but i cant'n do something because the selection returns only the cell from where the selection started.Can someone help me?

For mouse drag events to propagate to nodes other than the node in which the drag initiated, you need to activate a "full press-drag-release gesture" by calling startFullDrag(...) on the initial node. (See the Javadocs for MouseEvent and MouseDragEvent for details.) Then you can register for MouseDragEvents on the table cells in order to receive and process those events.
Here's a simple example: the UI is not supposed to be ideal but it will give you the idea.
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
public class DragSelectionTable extends Application {
    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
        FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "[email protected]"),
            new Person("Isabella", "Johnson", "[email protected]"),
            new Person("Ethan", "Williams", "[email protected]"),
            new Person("Emma", "Jones", "[email protected]"),
            new Person("Michael", "Brown", "[email protected]")
    public static void main(String[] args) {
        launch(args);
    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(500);
        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));
        table.setEditable(true);
        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));
        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));
        TableColumn<Person, String> emailCol = new TableColumn<>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));
        final Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory = new DragSelectionCellFactory();
        firstNameCol.setCellFactory(cellFactory);
        lastNameCol.setCellFactory(cellFactory);
        emailCol.setCellFactory(cellFactory);
        table.setItems(data);
        table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol));
        table.getSelectionModel().setCellSelectionEnabled(true);
        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);
        ((Group) scene.getRoot()).getChildren().addAll(vbox);
        stage.setScene(scene);
        stage.show();
    public static class DragSelectionCell extends TableCell<Person, String> {
        public DragSelectionCell() {
            setOnDragDetected(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    startFullDrag();
                    getTableColumn().getTableView().getSelectionModel().select(getIndex(), getTableColumn());
            setOnMouseDragEntered(new EventHandler<MouseDragEvent>() {
                @Override
                public void handle(MouseDragEvent event) {
                    getTableColumn().getTableView().getSelectionModel().select(getIndex(), getTableColumn());
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
            } else {
                setText(item);
    public static class DragSelectionCellFactory implements Callback<TableColumn<Person, String>, TableCell<Person, String>> {
        @Override
        public TableCell<Person, String> call(final TableColumn<Person, String> col) {         
            return new DragSelectionCell();
    public static class Person {
        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;
        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        public String getFirstName() {
            return firstName.get();
        public void setFirstName(String fName) {
            firstName.set(fName);
        public String getLastName() {
            return lastName.get();
        public void setLastName(String fName) {
            lastName.set(fName);
        public String getEmail() {
            return email.get();
        public void setEmail(String fName) {
            email.set(fName);

Similar Messages

  • How can I select multiple photos of files without selecting each individual one

    How can I select multiple files or photos without having to select each individual one? Like you can do on Windows

    Op: without tapping on each one individually
    tap all the photos you want to delete.
    Note what the OP is asking.

  • How can I select multiple photos without tapping on each one individually

    How can I select multiple photos without tapping on each one individually?

    Op: without tapping on each one individually
    tap all the photos you want to delete.
    Note what the OP is asking.

  • How Do I select Multiple songs to Use with Slideshow?

    I am running Lightroom 4 on an iMac with OS - Lion. Love it!
    I am trying to create a slideshow with music in the background but I cannot seem to select more than 1 song. How can I select multiple songs?
    Thanks
    Adrian

    The slide show feature in Lightroom isn't really very flexible or feature packed.  I suspect the design was to provide professional photographers a quick way to show proofs to clients.  You will have to use other software for what you intend to accomplish.  I'm going to be creating a slide show with about 500 images soon.  My intention is to create the slide show with Lightroom and export as video.  Then I will use Premiere Elements and import the video and lay down the music track.  There are easier ways to do it, I know.  But I don't do this kind of thing very often and don't plan to purchase more software that would make it easier.

  • How can I have multiple WINDOWS (NOT tabs) with INDEPENDENT content?

    How can I have multiple '''windows''' (''not ''tabs) with '''independent''' content?
    I used to be able to open separate windows with Firefox, and the content could be completely different in each window. No matter what I did in any window, no OTHER window open at the time, nor any of the content therein, was affected. This is no longer the case and it is extremely frustrating for me.
    I do ''not ''like tabs and do not use them. I prefer multiple windows plus I am so used to using them, for so many years now! But with v9.0.1 I suddenly can no longer do what I've always done with my browser without this aggravating problem constantly reminding me that I can't have what I want in firefox anymore.
    Or can I? Does anyone have a solution I don't realize exists?
    Thanks,
    Sowelu

    AppleScriptObjC can use pretty much everything in the Cocoa API, so yes, it is possible.
    Note that a view is not the same as a window, and a window can have multiple views. There are also many ways to implement "tabs";  take a look at some of Apple's applications - they use various mixtures of toolbars, checkboxes, and radio buttons, for example.  An application such as this will be a lot more involved than what you have done so far though, using custom classes and subclassing existing ones, so be prepared to do a lot of reading and researching.

  • How can I select what bookmark folder syncronize with Firefox sync?

    How can I select what bookmark folder syncronize with Firefox sync?
    Other extension like xmarks already perform this!

    I have the same problem. How can I fix?

  • How can I find my lost ipad-2 with WIFI only. I have only its serial no. icloud says alway the device is offline

    how can I find my lost ipad-2 with WIFI only. I have only its serial no. icloud says alway the
    device is offline

    It will only show if the device is turned on. And if someone else has it, they can turn that feature off. Your best options are to return to the location where you think you lost it, and see if anyone returned it. Also contact your local law enforcement.
    Reporting a lost or stolen product
    http://support.apple.com/kb/HT2526
    Report to police along with serial number. Change all your passwords.
    These links may be helpful.
    How to Track and Report Stolen iPad
    http://www.ipadastic.com/tutorials/how-to-track-and-report-stolen-ipad
    Reporting a lost or stolen Apple product
    http://support.apple.com/kb/ht2526
    Report Stolen iPad Tips and iPad Theft Prevention
    http://www.stolen-property.com/report-stolen-ipad.php
    How to recover a lost or stolen iPad
    http://ipadhelp.com/ipad-help/how-to-recover-a-lost-or-stolen-ipad/
    How to Find a Stolen iPad
    http://www.ehow.com/how_7586429_stolen-ipad.html
    Apple Product Lost or Stolen
    http://sites.google.com/site/appleclubfhs/support/advice-and-articles/lost-or-st olen
    Oops! iForgot My New iPad On the Plane; Now What?
    http://online.wsj.com/article/SB10001424052702303459004577362194012634000.html
    If you don't know your lost/stolen iPad's serial number, use the instructions below. The S/N is also on the iPad's box.
    How to Find Your iPad Serial Number
    http://www.ipadastic.com/tutorials/how-to-find-your-ipad-serial-number

  • How do i select multiple cells in numbers or iPad?

    Hello,
    can you help me? I do not find out how to select multiple cells in a table that do not stand next to each other.
    For example, I want to select cells A1, A3, A9 and so on. Afterwards I want to change the colour.
    Do you have an idea? Perhaps it's quite simple but I don't find the solution
    Sorry, I'm no native english speaker. I hope you'll understand what I am looking for.

    HI Ritscho
    I do not beleive this is possible, I can see other threads here from 2 years ago asking the same thing and none of them have answers.
    You can selesct a range of adjacent cells as Eric said, but not 2 different ranges. I usually use numbers on the iPad for filling in template spreadsheets that I create on my Mac. Most of the formatting I do on the Mac then fill in the blanks whilst i'm out and about.
    Not the answer you were looking for but hope it helps.

  • How can I select multiple clips in the event browser?

    I can't seem to figure out how to do something that should be pretty simple. I want to select multiple clips in the event browser that aren't next to each other. Right now it's only letting me select the two clips with every other clip in between. Is there a keyboard function that will allow me to deselect the clips in between while leaving the two on the outside selected? In Apple's finder you can do this with files by holding down shift-command while clicking on the ones you want. Or is there away to rearrange clips in the event browswer?
    I'd like to be able to create a multicam clip, but there is always going to be other media in between the clips I want to combine, so how can I select only the clips I want?
    Thanks!

    Holding the Command key should let you select non-contiguous items, just as in the Finder.

  • TS2128 How can I select multiple SEPARATE objects/words in Pages (Mavericks version)?

    When I type a document and decide to italicise a couple of separate words throughout the document how can I select them at once? Pressing Shift selects all the words between the first and the second, that does not help me. In Pages 09 it worked with either cmd or ctrl or alt (I don't remember which, but one of them worked) but I tried all of them now and nothing happens.
    Can anyone help me?
    Thank you!

    You can't do it anymore. Pages 5 is a stripped application and has lost about 90 feature that Pages 09 has. Read more about it in other threads in this forum.

  • How can I add multiple folders to iTunes with Windows 8?

    I have windows 8 and the latest version of iTunes on Windows 8. How can I put multiple folders of music onto iTunes

    Enable the menu bar with Ctrl+B. Then use File > Add Folder to Library.
    tt2

  • How do i select multiple cells in dreamweaver cc?

    Can't get cursor to appear to break and merge table cells. Keeps moving entire cell.
    Thanks,
    winifred

    in past version of dreamweaver I could see the cells and borders in design view and easily select multiples with a cursor. Just updated to CC and now only see blue boxes, arrow.
    Any thoughts on how to get a normal table view where I can select back.
    Any help is appreciated.
    winifred

  • How can I select multiple nodes by standard

    I want to ask how can I modify this example code to enable multiple selection, say if I select node1, system out display node1, then ctr+node2 selection. system out display node1&node2.
    The code below, only display node1, then when I do ctr+node2 selection, system out still display node1.
    I am quite new to JTree, so, please if there is anyone can give a solution?
    Thanks
    * A 1.4 application that requires the following additional files:
    *   TreeDemoHelp.html
    *    arnold.html
    *    bloch.html
    *    chan.html
    *    jls.html
    *    swingtutorial.html
    *    tutorial.html
    *    tutorialcont.html
    *    vm.html
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.UIManager;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreeSelectionModel;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import java.net.URL;
    import java.io.IOException;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    public class TreeDemo extends JPanel
                          implements TreeSelectionListener {
        private JEditorPane htmlPane;
        private JTree tree;
        private URL helpURL;
        private static boolean DEBUG = false;
        //Optionally play with line styles.  Possible values are
    //  "Angled" (the default), "Horizontal", and "None".
        private static boolean playWithLineStyle = false;
        private static String lineStyle = "Horizontal";
        //Optionally set the look and feel.
        private static boolean useSystemLookAndFeel = false;
        public TreeDemo() {
            super(new GridLayout(1,0));
            //Create the nodes.
            DefaultMutableTreeNode top =
                new DefaultMutableTreeNode("The Java Series");
            createNodes(top);
            //Create a tree that allows one selection at a time.
            tree = new JTree(top);
            tree.getSelectionModel().setSelectionMode
                    (TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);---->Cause problem, when press Ctr to multiple select, always the first selected node is returned, which is shown in System out
            //Listen for when the selection changes.
            tree.addTreeSelectionListener(this);
            if (playWithLineStyle) {
                System.out.println("line style = " + lineStyle);
                tree.putClientProperty("JTree.lineStyle", lineStyle);
            //Create the scroll pane and add the tree to it.
            JScrollPane treeView = new JScrollPane(tree);
            //Create the HTML viewing pane.
            htmlPane = new JEditorPane();
            htmlPane.setEditable(false);
            initHelp();
            JScrollPane htmlView = new JScrollPane(htmlPane);
            //Add the scroll panes to a split pane.
            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            splitPane.setTopComponent(treeView);
            splitPane.setBottomComponent(htmlView);
            Dimension minimumSize = new Dimension(100, 50);
            htmlView.setMinimumSize(minimumSize);
            treeView.setMinimumSize(minimumSize);
            splitPane.setDividerLocation(100); //XXX: ignored in some releases
                                               //of Swing. bug 4101306
            //workaround for bug 4101306:
            //treeView.setPreferredSize(new Dimension(100, 100));
            splitPane.setPreferredSize(new Dimension(500, 300));
            //Add the split pane to this panel.
            add(splitPane);
        /** Required by TreeSelectionListener interface. */
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                               tree.getLastSelectedPathComponent();
            if (node == null) return;
            Object nodeInfo = node.getUserObject();
            System.out.print("\nMy selection node is"+nodeInfo.toString());
            if (node.isLeaf()) {
                BookInfo book = (BookInfo)nodeInfo;
                displayURL(book.bookURL);
                if (DEBUG) {
                    System.out.print(book.bookURL + ":  \n    ");
            } else {
                displayURL(helpURL);
            if (DEBUG) {
                System.out.println(nodeInfo.toString());
        private class BookInfo {
            public String bookName;
            public URL bookURL;
            public BookInfo(String book, String filename) {
                bookName = book;
                bookURL = TreeDemo.class.getResource(filename);
                if (bookURL == null) {
                    System.err.println("Couldn't find file: "
                                       + filename);
            public String toString() {
                return bookName;
        private void initHelp() {
            String s = "TreeDemoHelp.html";
            helpURL = TreeDemo.class.getResource(s);
            if (helpURL == null) {
                System.err.println("Couldn't open help file: " + s);
            } else if (DEBUG) {
                System.out.println("Help URL is " + helpURL);
            displayURL(helpURL);
        private void displayURL(URL url) {
            try {
                if (url != null) {
                    htmlPane.setPage(url);
                } else { //null url
              htmlPane.setText("File Not Found");
                    if (DEBUG) {
                        System.out.println("Attempted to display a null URL.");
            } catch (IOException e) {
                System.err.println("Attempted to read a bad URL: " + url);
        private void createNodes(DefaultMutableTreeNode top) {
            DefaultMutableTreeNode category = null;
            DefaultMutableTreeNode book = null;
            category = new DefaultMutableTreeNode("Books for Java Programmers");
            top.add(category);
            //original Tutorial
            book = new DefaultMutableTreeNode(new BookInfo
                ("The Java Tutorial: A Short Course on the Basics",
                "tutorial.html"));
            category.add(book);
            //Tutorial Continued
            book = new DefaultMutableTreeNode(new BookInfo
                ("The Java Tutorial Continued: The Rest of the JDK",
                "tutorialcont.html"));
            category.add(book);
            //JFC Swing Tutorial
            book = new DefaultMutableTreeNode(new BookInfo
                ("The JFC Swing Tutorial: A Guide to Constructing GUIs",
                "swingtutorial.html"));
            category.add(book);
            //Bloch
            book = new DefaultMutableTreeNode(new BookInfo
                ("Effective Java Programming Language Guide",
              "bloch.html"));
            category.add(book);
            //Arnold/Gosling
            book = new DefaultMutableTreeNode(new BookInfo
                ("The Java Programming Language", "arnold.html"));
            category.add(book);
            //Chan
            book = new DefaultMutableTreeNode(new BookInfo
                ("The Java Developers Almanac",
                 "chan.html"));
            category.add(book);
            category = new DefaultMutableTreeNode("Books for Java Implementers");
            top.add(category);
            //VM
            book = new DefaultMutableTreeNode(new BookInfo
                ("The Java Virtual Machine Specification",
                 "vm.html"));
            category.add(book);
            //Language Spec
            book = new DefaultMutableTreeNode(new BookInfo
                ("The Java Language Specification",
                 "jls.html"));
            category.add(book);
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
        private static void createAndShowGUI() {
            if (useSystemLookAndFeel) {
                try {
                    UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    System.err.println("Couldn't use system look and feel.");
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
            //Create and set up the window.
            JFrame frame = new JFrame("TreeDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Create and set up the content pane.
            TreeDemo newContentPane = new TreeDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
    }

    you can use the method getSelectionPaths() in the class JTree. It will return an array of all the TreePaths you have selected. Then on each of the TreePath you got use the method getLastPathComponent() to get the nodes which you have selected.
    Hope this solves your problem.

  • How can i select multiple values form select list

    Hi ,
    I have created few items in a page and all items are Lov items and based on these items stacked charts will display. My requirement is I need to select multiple values form LOV's
    Eg: In Interactive report goto filter and select an item and expression as IN and select multiple items and i need to select multiple values like this.
    Please anybody help me how to get multiple values.
    I have one more doubt Can we have a Print preview option in APEX.
    I have to see print preview for stacked charts. is it possible?
    Regards
    Narender B

    HI ,
    Thank you for valuable information and looks good.but Here our client need to select multiple values.... for eg: Open IR report and click on Actions button --->select filter
    select any column , operator as IN and from expression select multiple items.
    I need to select multiple values in that way so, could you please guide me how can i achieve this.
    Regards
    Narender B

  • How can we select multiple rows in table control for module pool program?

    hi guru's
                 i cannt able to select multiple table control rows for deletion of records please give any code to select rows?
    regrards,
    satheesh.

    On the Table Control attributes there is a "Selectability" option where you choose one of: None, Single, or Multiple.  In your code you then need to pick up which rows have the selection column set to 'X'.

Maybe you are looking for

  • MacBook Pro: Minutes to open CD in iTunes

    Hey guys, Just last night my girlfriend informed me that suddenly her MacBook Pro (15" 1.83 stock) machine is going really REALLY slow when she puts a music CD in. Not just 1 CD, but many. It takes literally minutes from when she puts in the CD, to w

  • Start up from 'suspend to ram' no longer starts wireless

    When I finish with my desktop work I usually suspend to ram since it allows for quick startup. After a recent system upgrade, returning from suspend to ram no longer activates the wireless connection. NetworkManager is running in the processes list,

  • Can't find chipset for K9N6PGM2-V

    I go to the page where i can download the driver http://us.msi.com/index.php?func=downloaddetail&type=driver&maincat_no=1&prod_no=1472 and when i try to click the link, it says server not found i can't figure out where else to get the driver... pleas

  • Cannot Scan For Errors or Format iPod

    I see alot of ppl are having the same problems that I am having with my own iPod. This is very discouraging. We are getting ripped off by Apple, big time. I have tried EVERYTHING suggested that I can find, and my iPod still does not work. My drivers

  • Inserting the subtotal at the bottom of the table

    This question was posted in response to the following article: http://help.adobe.com/en_US/livecycle/10.0/DesignerQuickStartTutorials/WS92d06802c76abadb6 fe0558212a19a83673-7ffe.html