How to place popup menu on clicking a Component

Hi everybody.
I have a swing application where I have JFrame and so many components in that. I want to open Popup menu on clicking a button. The menu should have three menu items into that. It looks very simple but I am not sure how should I do that.
If possible, try to explain it with small piece of code.
Thanks in advance.

yourJbutton.addMouseListener(new MouseAdapter() { //Listen for mouse events
  public void mousePressed(MouseEvent e) {
    if (SwingUtilities.isRightMouseButton(e) == true && e.isMetaDown() == true) {
      JPopupMenu popup = new JPopupMenu();
      JMenuItem mItem = new JMenuItem("Something");
      mItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          // do your thing
      popup.add(mItem);
      popup.addSeparator();
      mItem = new JMenuItem("Close Popup");
      mItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {}});
      popup.add(mItem);
      popup.show(e.getComponent(), e.getX(), e.getY());
});

Similar Messages

  • How i create popup menu when button onclick

    how i create popup menu when button onclick

    You try adding a scrip tag in your jsp code, and calling the function in the onclick attribute.
    Example:
    <script><![CDATA[
                    function confirmDelete(){
                    if(confirm("Are you sure you want to delete selected user")){
                    return true;
                    } else{
    return false;
    ]]></script>
    <ui:button action="#{View.delete_action}" binding="#{View.delete}" id="delete" onclick="confirmDelete"/>
    Cheers
    Daniel
    null

  • Press Alt to popup Menu Toolbar, Click on a Custom Icon and the item does not come up AND the Tool Bar closes

    Options-->Menu Bar is Unchecked(off) (so, TABS are at the top of the screen):
    Press Alt to popup Menu Bar, Click on a Custom Icon and the item clicked on does not come up -and- the Menu Bar closes.
    So, Alt->Popup Menu Bar->Click a Custom Icon does not function properly.
    see Troubleshooting Info below-->

    First off if you have Firefox4 you probably also have an earlier version such as firefox 3.6.15 installed. By default firefox installs these in slightly different locations, but it leaves it up to you to create a separate profile for each version. The profile is where firefox stores data and settings. I am not sure that firefox explains this clearly, but you will run into problems if you do not have a separate profile for each version of firefox. See http://kb.mozillazine.org/Testing_pre-release_versions
    To try to get the toolbars working correctly, after you have ensured you have separate profiles in use for each version ( the following action will make changes)
    # use the restore default set from the customise / customize toolbars dialogue
    # if that does not work then
    #* use [safe mode] and the option [X] reset toolbars and controls

  • Popup menu appears behind canvas component

    Has anybody encountered this problem?
    I attach the popup menu to the canvas component, like this:
    public void mousePressed(MouseEvent e)
         if (e.getSource() == psuCanvas && e.getButton() == MouseEvent.BUTTON3)
              pmenu.show((Component)psuCanvas, e.getX(), e.getY());
              contextText.setText("Right Click!!!");
    The popup menu is only visible when I click the edge of the Canvas component, that is how I know that the popup is actually showing but behind the Canvas component!!!
    Please help

    You mean your 1 step of calling "JPopupMenu.setDefaultLightWeightPopupEnabled(false);"?
    That method is meant specifically for using popup menus when you are mixing AWT and Swing components. The reason is because of the problem you first posted about. It's a way to get around that problem. But really, it's more of a hack that is used when dealing with old AWT-based components which you can't (or won't), for whatever reason, "update" to Swing versions. (Say from a 3rd-party library).
    It is not recommended to mix AWT and Swing components, as already mentioned. The provided link above is an article that explains the issues with this. The fact that you can doesn't mean you should.
    If you are writing this Canvas subclass yourself, and it's going to be typically used in a Swing application, you will have problems later, most likely. For example, why don't you try puting your Canvas class in a JScrollPane so you can make it bigger then the window and scrollable? You'll have scrollbar visibility issues. Fine, you can use java.awt.ScrollPane, I guess. Then I can think of several things one might do with JLayeredPanes which would create problems as well. And there is no simple "setDefaultLightWeightEnabled()" option to fix those.
    So eventually, as your app, or library, matures, you start running into these problems cuz you are using AWT components where you should be using Swing components, and you start making more hack-like fixes for all these problems like using setDefaultLightWeightPopupEnabled() and explicit sizing instead of layout managers and limiting yourself to what you can do with your components.
    That's why.

  • JMenu:How to keep popup menu open after clicking disabled JMenuItem

    A menu usually shows all options, even those that are for some reason disabled. When clicking on a disabled option, nothing should happen. Unfortunately, the JMenu does not follow this philosophy: clicking on a disabled option fires no action, but the popup menu closes. It is a minor problem but I prefer the standard behaviour. Any ideas ?
    Thanks
    Cyril Mrazek

    I am also having the same problem, if any one find out solution for this mail me . My mail Id is [email protected]

  • HOW I CREATE POPUP MENU ON MOUSE OVER

    hello every body
    my question is how to create popupmenu on mouse over on table column data in studio creator.
    please help me
    thanks

    ok this examples runs great on image component, in button or hyperlynk dont work finee or i can find how, paste this code in the head of the jsp code:
    <script type="text/javascript"><![CDATA[
    var opt=0;
    * AnyLink Drop Down Menu- � Dynamic Drive (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for full source code
    //Contents for menu 1
    var menu1=new Array()
    menu1[0]='Information<\/a>'
    menu1[1]='Del<\/a>'
    var menuwidth='165px' //default menu width
    var menubgcolor='lightyellow' //menu bgcolor
    var disappeardelay=250 //menu disappear speed onMouseout (in miliseconds)
    var hidemenu_onclick="yes" //hide menu when user clicks within menu?
    /////No further editting needed
    var ie4=document.all
    var ns6=document.getElementById&&!document.all
    if (ie4||ns6)
    document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+';background-color:'+menubgcolor+'" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"><\/div>')
    function getposOffset(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
    return totaloffset;
    function showhide(obj, e, visible, hidden, menuwidth){
    if (ie4||ns6)
    dropmenuobj.style.left=dropmenuobj.style.top="-500px"
    if (menuwidth!=""){
    dropmenuobj.widthobj=dropmenuobj.style
    dropmenuobj.widthobj.width=menuwidth
    if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
    obj.visibility=visible
    else if (e.type=="click")
    obj.visibility=hidden
    function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
    function clearbrowseredge(obj, whichedge){
    var edgeoffset=0
    if (whichedge=="rightedge"){
    var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
    dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
    if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
    edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
    else{
    var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
    var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
    dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
    if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
    edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
    if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
    edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
    return edgeoffset
    function populatemenu(what){
    if (ie4||ns6)
    dropmenuobj.innerHTML=what.join("")
    function dropdownmenu(obj, e, menucontents, menuwidth){
    if (window.event) event.cancelBubble=true
    else if (e.stopPropagation) e.stopPropagation()
    clearhidemenu()
    dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
    populatemenu(menucontents)
    if (ie4||ns6){
    showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
    dropmenuobj.x=getposOffset(obj, "left")
    dropmenuobj.y=getposOffset(obj, "top")
    dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
    dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
    return clickreturnvalue()
    function clickreturnvalue(){
    if (ie4||ns6) return false
    else return true
    function contains_ns6(a, b) {
    while (b.parentNode)
    if ((b = b.parentNode) == a)
    return true;
    return false;
    function dynamichide(e){
    if (ie4&&!dropmenuobj.contains(e.toElement))
    delayhidemenu()
    else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
    delayhidemenu()
    function hidemenu(e){
    if (typeof dropmenuobj!="undefined"){
    if (ie4||ns6)
    dropmenuobj.style.visibility="hidden"
    function delayhidemenu(){
    if (ie4||ns6)
    delayhide=setTimeout("hidemenu()",disappeardelay)
    function clearhidemenu(){
    if (typeof delayhide!="undefined")
    clearTimeout(delayhide)
    if (hidemenu_onclick=="yes")
    document.onclick=hidemenu
    ]]></script>
    Next, paste this in css stylesheet:
    #dropmenudiv{
    position:absolute;
    border:1px solid black;
    border-bottom-width: 0;
    font:normal 12px Verdana;
    line-height:18px;
    z-index:100;
    #dropmenudiv a{
    width: 100%;
    display: block;
    text-indent: 3px;
    border-bottom: 1px solid black;
    padding: 1px 0;
    text-decoration: none;
    font-weight: bold;
    #dropmenudiv a:hover{
    /*hover background color*/
    background-color: #faebeb
    and in the image componen in event onClick paste this
    dropdownmenu(this, event, menu1, '150px')
    onMouseOut event paste this:
    delayhidemenu()
    this should works, and as u can see, u can get more examples like this in the DHTML page. I hope it helps
    Plz tell me if this is what ur looking for, oor if u need help, post again
    Beltazor

  • How to display popup, if we click on Create Confirmation icon

    Hi Friends,
    Can any one help me
    I have requirement. In "Confirm Goods Receipt/Services Performed" Screen all the purchase Orders are listed out. and we can see the create confirmation icon at the end of each line of purchase order. If we click on that icon to create confirmation for that PO, it should check whether that PO exists in z-table or not. If it exists, it should give popup like "This PO cannot be confirmed manually".
    can anybody help me regarding this

    Hi
    Which SRM version are you using ?
    Till the time your Functional Person suggests any other alternative, Please try this approach ->
    Use BBP_PD_SC_GETDETAIL -> to get the Shopping cart data - SC number,  SC GUID, POrg ID and Vendor(Supplier Number)
    Use BBP_PO_GET_DETAIL -> to get the Local SRM Purchase Order details, PO Number, PO GUID, POrg ID and Vendor(Supplier Number)
    Try to use BBP_ITEM_CHECK_BADI / BBP_DOC_CHECK_BADI -> incase the POrg ID and Supplier number does not match with the Z-table entry.
    BBP_WFL_LIST_BADI also might help.
    Regards
    - Atul

  • Mouse right click button, popup menu, PC frozen

    Hello all,
    It has been while I've been noticing this problem.
    Almost every time I click with the right mouse button on an email from the list of emails or a word inside the content of an email to see which words are suggested by the spell checker, the usual popup menu appear but immediately after
    my PC gets frozen. I need to restart it.
    I don't think TB causes that crash, but something indirectly related.
    How can I debug this? (I'm software developer)
    Thank you so much.
    Roberto

    Update1:
    I put my attention only in the main TB menu.
    I disabled the TB extensions (only Lighting and EnigMail) and tried again to play with the main TB menu.
    With this new TB GUI, you don't see directly the main menu. I usually click the ALT key and the main menu will be displayed. After that I play with it a bit. That means, I clicked on on Tools or File or View etc items and just moved the mouse cursor over the popup menu, without clicking on a specific submenu item. After a while, some seconds, the PC gets frozen and I need to restard the PC.
    I started TB in safe mode also (-safe-mode). In this mode the main menu appears always so I don't need to click on the ALT key.
    I repeat the same steps of moving the mouse cursor of popup main menu items. Up to now, all is normal, without crashing or freezing.
    PC: Win7 (full updated), x64, TB last version.
    Any idea how can I debug this problem?
    Thanks

  • Custom popup menu + graphic ring

    Hi,
    I have two questions.
    1) Could anyone give me an example how to create my own popup menu by
    clicking the right mouse button over an item.
    2) Does anybody know how to disable the border an the background of a
    graphic ring item. I what to display different transparent gifs and now
    the border is very disturbing and there should also bo no visible
    background(-color).
    thanxs,
    Max

    1. An article on this was in LTR some time ago, check it out at:
    http://ltrpub.com/insideltr/articles/article_27/article_27.htm
    There has been some discussions and examples of it here as well though, check out the last entry (I think there are others here too, just search for contextual menu or po-up menu):
    http://exchange.ni.com/servlet/ProcessRequest?RHIVEID=101&RPAGEID=135&HOID=50650000000800000084640000&UCATEGORY_0=_49_%24_6_&UCATEGORY_S=0&USEARCHCONTEXT_TIER_0=0&USEARCHCONTEXT_TIER_S=0&USEARCHCONTEXT_QUESTION_0=contextual+menu&USEARCHCONTEXT_QUESTION_S=0
    2. Color the border and background transparent. Just choosing background color and foreground color to be transparent and then using the paint tool does not work though, instead - hold the paint too
    l over the border, right click to get the color palette, move the cursor above the T (transparent) then press F...and then B, now the border should become transparent. Paint the background transparent...After doing everything transparent this way - import the pictures into it.
    MTO

  • Focus lost when a popup menu is activated...

    It seems that when a popup menu is activated the component invoker lost focus. this is ugly because a focus lost can trigger unexpected events (formatting, validating, etc). how can I avoid this?
    thanx

    I must seem a bit weird being the only one to reply to my posts, but I found what is occuring, is that when I do the mouse click the InputVerifier for that JTextField is firing. This is causing a problem, because this input verifier pops up a dialog saying please enter correct values ... and then highlights the entire text field. Problem is if the input is currently invalid I can't use the mouse.
    Any ideas?
    Tom

  • Popup menue problem

    when adding popup menues on text pane component then try to appeare it ,the textpane component appeare in front of the popup menue
    i want fix this problem!!!!!

    Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup]How to Use Menus for a working example of using popup menus.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • DW CS3 popup menu

    Hi
    How to activate popup menu form DW 2004 MX? bexouse i have
    not active

    Hi
    How to activate popup menu form DW 2004 MX? bexouse i have
    not active

  • How do I disable the brushes popup menu when I left click in Photoshop CS5?

    How do I disable the brushes popup menu when I left click in Photoshop CS5? I am trying to use the clone stamp and I am not able to get a sample because of this popup menu.

    LOL,  I can't even begin to complain with the number if times I misread questions, and then try to break my own record for the number of typos in a single post.
    The image above was taken on a photgraphic weekend where the group (of all young ladies ) I was travelling with, set us a photography challenge.  The one above was my response to a 'faceless portrait'.  The only other one I attempted was this response for a picture of a 'wild animal', featuring camera club president Liz.
    Oh what fun we had, laughing and laughing until our faces fell off.
    [EDIT]  Both taken at Karamea, which some visitors to NZ might recognise as the western end of the Heaphy Track.

  • How to add click popup menu to a row of a table.

    hi,
    I have a JTable with say 10 rows of data.
    On selecting a particular row and user right clicks then a popup menu should come.
    I dont want to attach popup to the JTable,instead i want it to be attached to the rows of the JTable.
    How to do it??
    Thanks & regards
    Neel

    You have no choice but to "attach" it to the JTable; it is the JTable which is
    receiving the events that you would use to trigger the popup.
    That said, there is nothing stopping you from displaying a different popup
    depending on which row was clicked on.
    : jay

  • How to get system popup menu in java?

    I want to know how to get windows xp or any operating systems default popupmenu in java.
    And i also have problem for using mnemonic for JPopupMenu, it works well when i first press right click of my mouse and use the specified mnemonic for that the action specifed for that is triggered but when is use mnemonic without the using my mouse it doesn't trigger why.

    Please how to get native(e.g windows explorer) popup menu in jfilechooser

Maybe you are looking for

  • URGENT HELP: Need to reformat an external hard drive for Mac/Win!

    We got a Desk Hammer external hard drive and my hubby chose to format it as Mac Only when it first initialized. We needed it to be formatted for Mac/Win usage. Now we cannot figure otu how to reinitialize and reformat it. It's been years since I mess

  • Firewire part 2 - Need Help Refreshing My PMU

    Recap: At some point a few months back, my FCP HD lost contact with my Canopus Datavideo. Under ABOUT MY MAC in HARDWARE>FIREWIRE it only states FIRE WIRE BUS and its speed. I have reloaded Quicktime and tried to REFRESH my AV connectons but to no av

  • Unresponsive / laggy ipad

    I am not sure what is going on, but my iPad 3G (16 gig) has become at times completely unresponsive. It gets so bad that pushing wake/sleep or home has no response for minutes at a time. I have updated to iOS 3.2.2 a while ago. I noticed instances of

  • Maximum number of members in a dimension (BSO)

    we are designing a new planning application and the requirement for one of the dimensions may force us to have a large number of members. I needed a ballpark figrue on what the maximum number of members should be in planning for it to work in an acce

  • I am using EclispeIDE,so please tell me step by step procedure how to install buttons in firefox ToolBar using EclipseIDE.

    I have downloaded XulBooster.I have xul in EclipseIDE as follows: <pre><nowiki> <?xml version="1.0"?><?xml-stylesheet type="text/css" href="chrome://test_ext/content/about.css" ?> <overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.o