Add right-click menu (jpopupmenu) to jlist)

Source: Internet
Author: User
Tags gety

I have not mastered the right-click menu in some components for a long time, and I have always felt quite difficult (it really seems that I am stupid, but I am in a daze spirit, and I succeeded today, but it will be nice to understand it later. So let's take a look at it and share it with you. 1: define variables. Jlist; jpopupmenu; jmenuitem; jsonjradiobuttonmenuitem; jcheckboxmenuitem; jseparator; 2: Initialize member variables. Saving, no writing. 3: Right-click the jpopupmenu menu to add jlist1.add (jpopupmenu) in the list (jlist); 4: add the mouse listener jlist for the jlist. addmouselistener (New myjlistlistener (); 5: implement the mouse listener public class myjlistlistener extends mouseadapter {// E. getbutton () returns 1, 2, 3. 1 indicates the left mouse button, 3 indicates the right mouse button // jlist. getselected () returns the number of items in the selected jlist. // If statement, that is, right-click in jlist and select an item in jlist. The right-click menu is displayed. // E. getx (), E. gety () returns the current cursor position! That is, right-click public void mouseclicked (mouseevent e) {If (E. getbutton () = 3 & jlist. getselected ()> = 0) jpopupmenu. show (jlist, E. getx (), E. gety () ;}} now understands that adding right-clicking other components is the same. It seems much easier to separate things!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.