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.


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

Results 1 to 2 of 2

Thread: Listening to JPopupMenuListener and CaretListener simultaneously

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Thanked 0 Times in 0 Posts

    Default Listening to JPopupMenuListener and CaretListener simultaneously

    I'm developing a popup menu for cut, copy and paste in a JTextArea. The cut and copy menu items will be enabled only when some text in the JTextArea is selected. Similarly, the paste menuitem will be enabled only if there is something to paste from the clipboard.

    To implement this functionality, I used a PopupMenuListener which will listen to whether the popupmenu has been invoked or not. If it has been invoked, I immediately need to ascertain the dot and mark position of the text caret so as to determine whether any text has been selected or not. Thus, I kind of need to use two listeners simultaneously. I tried nesting CaretListener within PopupMenuListener but to no avail.

    How can one, at the same time, listen to Popupmenu invocation as well as the text caret positioning so as to eventually determine whether the cut or copy menuitem should be enabled or disabled? Thanks.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 827 Times in 770 Posts
    Blog Entries

    Default Re: Listening to JPopupMenuListener and CaretListener simultaneously

    Any reason you can't just check for selected text in the PopupMenuListener? Once invoked, if there is selected text, show the menu. If not, don't show the menu.