Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Receive only first mouse-click, looking for double click

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Receive only first mouse-click, looking for double click

    Using swing jTable (named resultTbl), I want to have double click select the current row. The canonical suggestion is essentially to add a mouse listener
           resultTbl.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    resultTblMouseClicked(evt);
                }
            });
    pointing to
       private void resultTblMouseClicked(java.awt.event.MouseEvent evt) {                                       
             if (evt.getClickCount() == 2 && !evt.isConsumed()) {
                evt.consume();
               /* perform select action */
            }
        }
    Much trial (both with and without the evt.consume() line and lots of printlns) has proven to me that this code sees one mouse click but never a second one. What can I do to get the second click?

    Thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Receive only first mouse-click, looking for double click

    Can you post a small, complete program for testing?
    Include the debug print statements that show when a mouse, press, release and click were received.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Receive only first mouse-click, looking for double click

    I rather doubt it. I should have mentioned that Netbeans is the framework for this is program, and I don't think it's possible for Netbeans to create a small enough program to post. I'll give it a try when I get a chance [later today or tomorrow: life gets in the way], and maybe put the program into a dropbox file -- or is there a generally accepted repository for placing such programs (similar to the golang playground)?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Receive only first mouse-click, looking for double click

    possible for Netbeans to create a small enough program to post
    That is interesting.
    Strip out everything but a component that can receive a mouse click and display it. Add the listener.
    It shouldn't be over 50 lines.

    I don't care to download code from 3rd party sites.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Receive only first mouse-click, looking for double click

    I started to create a sample, but for a jForm containing an empty jTable Netbeans generates over 100 lines of code. Now what should I do? I could continue trying to create an example, but it'd be uncomfortably long.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Receive only first mouse-click, looking for double click

    An sample program does not need a JTable. Just a frame and maybe a JPanel and a listener should be enough.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2021
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Receive only first mouse-click, looking for double click

    I finally found the answer: disable the default editing caused by double clicking in a column of a jTable. Then, the MouseListener added as in the original post does work.

    Details -- two parts:

    1) Embedded in the solutions to a query about how to make a jTable non-editable at
    https://stackoverflow.com/questions/...e-non-editable
    is the comment
    "This is also the solution for making double click events work correctly with JTable. A double click is normally consumed by a cell as it goes into edit mode, and this will keep a cell from doing that and instead send the double click to the JTable itself. Thanks for the solution, Nelson! – " Mar 15 '12 at 15:39

    Ah ha! And how to prevent editing of a jTable in Netbeans?

    2) Answer is at
    https://stackoverflow.com/questions/...edit-in-jtable
    Specifically, "Well on netbeans you can right click on the table and click on table contents, then you go to the column tab and uncheck the "Editable" checkbox." I would add that this should be done for each column.

    So, problem solved.

  8. The Following User Says Thank You to ThisEndUp For This Useful Post:

    Norm (December 19th, 2021)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Receive only first mouse-click, looking for double click

    Thanks for letting us all know what the solution is.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Running simple terminal java app in Mac Os on double click
    By hamon in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2018, 04:43 AM
  2. Mouse Click Action Listener for JTextField?
    By Catlitter68 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2013, 10:11 AM
  3. Replies: 4
    Last Post: August 30th, 2013, 12:57 PM
  4. [SOLVED] trying to change image by mouse click
    By leonne in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 5th, 2013, 08:16 PM
  5. trying to draw a circle at the location of every mouse click
    By jopeters in forum AWT / Java Swing
    Replies: 0
    Last Post: October 18th, 2011, 05:24 PM