Popup menus do not pop up on JFrame

I don't seem to be able to get the pop-up menus work anywhere over this JFrame:
The running JFrame I would like to add the pop-up menus
The code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Edit_XML_File2 extends JFrame {
    
    private JMenuBar menuBar1;
    private JMenu jmnuFile;
    private JMenuItem jmnuItemOpen;
    private JMenuItem jmnuItemSave;
    private JMenuItem jmnuItemSaveAs;
    private JMenuItem jmnuItemExit;
    private JTextField jtxtFieldProjectFolderName;
    private JLabel jlblProjectFolderName;
    private JComboBox jcmboLocalNetwork;
    private JLabel jlblLocalNetwork;
    private JTextField jtxtFieldProjectSpecificTitle;
    private JLabel jlblProjectSpecificTitle;
    private JComboBox jcmboDateAutoManual;
    private JLabel jlblDateCodeAutoManual;
    private JScrollPane scrlPaneStandardMenuItems;
    private JTextArea jtxtAreaStandardMenuItems;
    private JLabel jlblStandardMenuItems;
    
    // Create popup menus for Insert Row, Delete Selection, Copy Selection, Paste Selection
        private JPopupMenu editTextAreaPopupMenu;
        private JMenuItem insertRowPopup;
        private JMenuItem deleteSelectionPopup;
        private JMenuItem copySelectionPopup;
        private JMenuItem pasteSelectionPopup;
    
    
	public Edit_XML_File2() {
		initComponents();
	}

	

// Mouse listeners for popup menus:
	private void insertRowPopup_MouseClicked(MouseEvent e) {
		System.out.println("Insert Row Clicked");
	}
	private void deleteSelectionPopup_MouseClicked(MouseEvent e) {
		System.out.println("Delete Selection Clicked");
	}
        private void copySelectionPopup_MouseClicked(MouseEvent e) {
            System.out.println("Copy Selection Clicked");
        }
        private void pasteSelectionPopup_MouseClicked(MouseEvent e) {
            System.out.println("Paste Selection Clicked");
        }

    // End of Mouse listeners for popup menus:


    private void jcmboLocalNetworkItemStateChanged(ItemEvent e) {
            
    }

    private void jcmboDateAutoManualItemStateChanged(ItemEvent e) {
            
    }

    private void jbtnSaveFileActionPerformed(ActionEvent e) {
            
    }


	private void initComponents() {
		
		menuBar1 = new JMenuBar();
		jmnuFile = new JMenu();
		jmnuItemOpen = new JMenuItem();
		jmnuItemSave = new JMenuItem();
		jmnuItemSaveAs = new JMenuItem();
		jmnuItemExit = new JMenuItem();
		jtxtFieldProjectFolderName = new JTextField();
		jlblProjectFolderName = new JLabel();
		jcmboLocalNetwork = new JComboBox();
		jlblLocalNetwork = new JLabel();
		jtxtFieldProjectSpecificTitle = new JTextField();
		jlblProjectSpecificTitle = new JLabel();
		jcmboDateAutoManual = new JComboBox();
		jlblDateCodeAutoManual = new JLabel();
		scrlPaneStandardMenuItems = new JScrollPane();
		jtxtAreaStandardMenuItems = new JTextArea();
		jlblStandardMenuItems = new JLabel();
                
	    // Popup menu:
	        editTextAreaPopupMenu = new JPopupMenu();
	        insertRowPopup = new JMenuItem();
	        editTextAreaPopupMenu.add(insertRowPopup);
	        deleteSelectionPopup = new JMenuItem();
	        editTextAreaPopupMenu.add(deleteSelectionPopup);
	        copySelectionPopup = new JMenuItem();
	        editTextAreaPopupMenu.add(copySelectionPopup);
	        pasteSelectionPopup = new JMenuItem(); 
	        editTextAreaPopupMenu.add(pasteSelectionPopup);
                
                

		//======== this ========
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//======== menuBar1 ========
		{
			//======== jmnuFile ========
			{
				jmnuFile.setText("File");

				//---- jmnuItemOpen ----
				jmnuItemOpen.setText("Open");
				jmnuFile.add(jmnuItemOpen);

				//---- jmnuItemSave ----
				jmnuItemSave.setText("Save");
				jmnuFile.add(jmnuItemSave);

				//---- jmnuItemSaveAs ----
				jmnuItemSaveAs.setText("Save As");
				jmnuFile.add(jmnuItemSaveAs);

				//---- jmnuItemExit ----
				jmnuItemExit.setText("Exit");
				jmnuFile.add(jmnuItemExit);
			}
			menuBar1.add(jmnuFile);
		}
		setJMenuBar(menuBar1);
		contentPane.add(jtxtFieldProjectFolderName);
		jtxtFieldProjectFolderName.setBounds(25, 30, 205, jtxtFieldProjectFolderName.getPreferredSize().height);

		//---- jlblProjectFolderName ----
		jlblProjectFolderName.setText("Project Folder Name:");
		contentPane.add(jlblProjectFolderName);
		jlblProjectFolderName.setBounds(25, 10, 160, jlblProjectFolderName.getPreferredSize().height);

		//---- jcmboLocalNetwork ----
		jcmboLocalNetwork.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				jcmboLocalNetworkItemStateChanged(e);
			}
		});
		contentPane.add(jcmboLocalNetwork);
		jcmboLocalNetwork.setBounds(675, 30, 85, 20);

		//---- jlblLocalNetwork ----
		jlblLocalNetwork.setText("Local / Network:");
		contentPane.add(jlblLocalNetwork);
		jlblLocalNetwork.setBounds(675, 10, 85, jlblLocalNetwork.getPreferredSize().height);
		
		contentPane.add(jtxtFieldProjectSpecificTitle);
		jtxtFieldProjectSpecificTitle.setBounds(25, 75, 205, 20);

		//---- jlblProjectSpecificTitle ----
		jlblProjectSpecificTitle.setText("Project Specific Title:");
		contentPane.add(jlblProjectSpecificTitle);
		jlblProjectSpecificTitle.setBounds(25, 55, 135, jlblProjectSpecificTitle.getPreferredSize().height);
		
		//---- jcmboDateAutoManual ----
		jcmboDateAutoManual.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				jcmboDateAutoManualItemStateChanged(e);
			}
		});
		contentPane.add(jcmboDateAutoManual);
		jcmboDateAutoManual.setBounds(775, 30, 110, jcmboDateAutoManual.getPreferredSize().height);

		//---- jlblDateCodeAutoManual ----
		jlblDateCodeAutoManual.setText("Date Code Auto / Manual:");
		contentPane.add(jlblDateCodeAutoManual);
		jlblDateCodeAutoManual.setBounds(775, 10, 140, jlblDateCodeAutoManual.getPreferredSize().height);

		//======== scrlPaneStandardMenuItems ========
		{
			scrlPaneStandardMenuItems.setViewportView(jtxtAreaStandardMenuItems);
		}
		contentPane.add(scrlPaneStandardMenuItems);
		scrlPaneStandardMenuItems.setBounds(35, 130, 185, 65);

		//---- jlblStandardMenuItems ----
		jlblStandardMenuItems.setText("Standard Menu Items:");
		contentPane.add(jlblStandardMenuItems);
		jlblStandardMenuItems.setBounds(30, 110, 110, 15);

		//======== editTextAreaPopup ========
		{
			//---- insertRowPopup ----
			insertRowPopup.setText("Insert Row");
			insertRowPopup.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					insertRowPopup_MouseClicked(e);
				}
			});
                        
			editTextAreaPopupMenu.add(insertRowPopup);
                        
			//---- deleteSelectionPopup ----
			deleteSelectionPopup.setText("Delete Selection");
			deleteSelectionPopup.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					deleteSelectionPopup_MouseClicked(e);
				}
			});
			editTextAreaPopupMenu.add(deleteSelectionPopup);
                        
		    //---- copySelectionPopup ----
		    copySelectionPopup.setText("Copy Selection");
		    copySelectionPopup.addMouseListener(new MouseAdapter() {
		            public void mouseClicked(MouseEvent e) {
		                    copySelectionPopup_MouseClicked(e);
		            }
		    });
		    
		    editTextAreaPopupMenu.add(deleteSelectionPopup);
                    
		    //---- pasteSelectionPopup ----
		    pasteSelectionPopup.setText("Paste Selection");
		    pasteSelectionPopup.addMouseListener(new MouseAdapter() {
		            public void mouseClicked(MouseEvent e) {
		                    pasteSelectionPopup_MouseClicked(e);
		            }
		    });
		    editTextAreaPopupMenu.add(deleteSelectionPopup);
                    
		}
		
                // Add popup menu to contentpane
                contentPane.add(editTextAreaPopupMenu);

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(getOwner());
		// End of component initialization 
	}
        
    public static void main(String[] args) {
                  
                          // The static utility method invokeLater(Runnable) is intended to execute a new runnable
                          // thread from a Swing application without disturbing the normal sequence of event dispatching
                          // from the GUI.
                  
               SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                         try {
                                             // Set the look and feel for the current OS (Windows) Scheme and
                                             // all subsequent jFrames will also have the same look and feel
                                             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                             
                                             Edit_XML_File2 edit_XML_File = new Edit_XML_File2();
                                             
                                             edit_XML_File.setLocationRelativeTo(null);
                                             edit_XML_File.setSize(930,275);
                                             edit_XML_File.setResizable(false);
                                             edit_XML_File.setVisible(true);
                                             
                                             
                                         } catch (Exception e) {
                                             e.printStackTrace();
                                         }
                                    }
                                });
                          }  

}

Open in new window


Thank you for looking through this.
AttilaBAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Have you included a call to the JPopup's setInvoker() method?
0
CEHJCommented:
contentPane.add(editTextAreaPopupMenu);

Open in new window

Why do that when it's meant to be a popup menu? Also, where do you ever call setVisible or show on it?
0
AttilaBAuthor Commented:
No, I did not. I did not know that I need to do that. I did an Internet search, and this is what I came up with:

void setInvoker(Component invoker)
Sets the invoker of this popup menu -- the component in which the popup menu menu is to be displayed.


I added it to the code, per your suggestion:

        
                // Sets the invoker of this popup menu -- 
                // the component in which the popup menu menu is to be displayed
                editTextAreaPopupMenu.setInvoker(this);

Open in new window


I don't know what exactly this is supposed to do, but doesn't do anything for me.
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

CEHJCommented:
See my comments above
0
CEHJCommented:
When you've got rid of that add call

editTextAreaPopupMenu.show(this, 0, 0);

Open in new window


should work
0
AttilaBAuthor Commented:
Hi CEHJ,

I thought that I need to add it to the  contentPane to be available.
It doesn't make a difference if I set it visible as you suggested. I don't have to set other menus visible either.
0
krakatoaCommented:
Well, you can see from the lit that setInvoker() doesn't do nothing, so perhaps it still lacks visibility as CEHJ has said ?

All I know is that I needed the method the last time I used a popup . . .
0
krakatoaCommented:
This is the code that worked in my case:

public class mouseadapter extends MouseAdapter{

        private JPopupMenu jpop;


        public mouseadapter(JPopupMenu jpop){

          this.jpop = xcomms.this.jpop;
          jpop.setInvoker(txtArea);

        }


	public boolean popup(MouseEvent ev)
	{
	   if (ev.isPopupTrigger())
	   {
      
      		jpop.show(ev.getComponent(),ev.getX(),ev.getY());
		jpop.requestFocus();
		ev.consume();	
	        return true;
	   }

           else return false;
	}

Open in new window


(the refs are of course nothing to do with your code . . . this is just for the notional relationships)
0
AttilaBAuthor Commented:
Well,

editTextAreaPopupMenu.show(this, 0, 0);

Open in new window


will generate:

java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location

By the way, original code i posted compiles and runs fine. Just the pop-up menu is not showing up. Did you try to edit and run my original file I posted?
0
CEHJCommented:
Please post ALL the code you just tried
0
CEHJCommented:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Edit_XML_File2 extends JFrame {
    private JMenuBar menuBar1;
    private JMenu jmnuFile;
    private JMenuItem jmnuItemOpen;
    private JMenuItem jmnuItemSave;
    private JMenuItem jmnuItemSaveAs;
    private JMenuItem jmnuItemExit;
    private JTextField jtxtFieldProjectFolderName;
    private JLabel jlblProjectFolderName;
    private JComboBox jcmboLocalNetwork;
    private JLabel jlblLocalNetwork;
    private JTextField jtxtFieldProjectSpecificTitle;
    private JLabel jlblProjectSpecificTitle;
    private JComboBox jcmboDateAutoManual;
    private JLabel jlblDateCodeAutoManual;
    private JScrollPane scrlPaneStandardMenuItems;
    private JTextArea jtxtAreaStandardMenuItems;
    private JLabel jlblStandardMenuItems;

    // Create popup menus for Insert Row, Delete Selection, Copy Selection, Paste Selection
    private JPopupMenu editTextAreaPopupMenu;
    private JMenuItem insertRowPopup;
    private JMenuItem deleteSelectionPopup;
    private JMenuItem copySelectionPopup;
    private JMenuItem pasteSelectionPopup;

    public Edit_XML_File2() {
        initComponents();
    }

    // Mouse listeners for popup menus:
    private void insertRowPopup_MouseClicked(MouseEvent e) {
        System.out.println("Insert Row Clicked");
    }

    private void deleteSelectionPopup_MouseClicked(MouseEvent e) {
        System.out.println("Delete Selection Clicked");
    }

    private void copySelectionPopup_MouseClicked(MouseEvent e) {
        System.out.println("Copy Selection Clicked");
    }

    private void pasteSelectionPopup_MouseClicked(MouseEvent e) {
        System.out.println("Paste Selection Clicked");
    }

    // End of Mouse listeners for popup menus:
    private void jcmboLocalNetworkItemStateChanged(ItemEvent e) {
	editTextAreaPopupMenu.show(this, 0, 0);
    }

    private void jcmboDateAutoManualItemStateChanged(ItemEvent e) {
    }

    private void jbtnSaveFileActionPerformed(ActionEvent e) {
    }

    private void initComponents() {
        menuBar1 = new JMenuBar();
        jmnuFile = new JMenu();
        jmnuItemOpen = new JMenuItem();
        jmnuItemSave = new JMenuItem();
        jmnuItemSaveAs = new JMenuItem();
        jmnuItemExit = new JMenuItem();
        jtxtFieldProjectFolderName = new JTextField();
        jlblProjectFolderName = new JLabel();
        jcmboLocalNetwork = new JComboBox();
	jcmboLocalNetwork.addItem("FOO");
	jcmboLocalNetwork.addItem("BAR");
        jlblLocalNetwork = new JLabel();
        jtxtFieldProjectSpecificTitle = new JTextField();
        jlblProjectSpecificTitle = new JLabel();
        jcmboDateAutoManual = new JComboBox();
        jlblDateCodeAutoManual = new JLabel();
        scrlPaneStandardMenuItems = new JScrollPane();
        jtxtAreaStandardMenuItems = new JTextArea();
        jlblStandardMenuItems = new JLabel();

        // Popup menu:
        editTextAreaPopupMenu = new JPopupMenu();
        insertRowPopup = new JMenuItem();
        editTextAreaPopupMenu.add(insertRowPopup);
        deleteSelectionPopup = new JMenuItem();
        editTextAreaPopupMenu.add(deleteSelectionPopup);
        copySelectionPopup = new JMenuItem();
        editTextAreaPopupMenu.add(copySelectionPopup);
        pasteSelectionPopup = new JMenuItem();
        editTextAreaPopupMenu.add(pasteSelectionPopup);

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        //======== menuBar1 ========
        {
            //======== jmnuFile ========
            {
                jmnuFile.setText("File");

                //---- jmnuItemOpen ----
                jmnuItemOpen.setText("Open");
                jmnuFile.add(jmnuItemOpen);

                //---- jmnuItemSave ----
                jmnuItemSave.setText("Save");
                jmnuFile.add(jmnuItemSave);

                //---- jmnuItemSaveAs ----
                jmnuItemSaveAs.setText("Save As");
                jmnuFile.add(jmnuItemSaveAs);

                //---- jmnuItemExit ----
                jmnuItemExit.setText("Exit");
                jmnuFile.add(jmnuItemExit);
            }

            menuBar1.add(jmnuFile);
        }

        setJMenuBar(menuBar1);
        contentPane.add(jtxtFieldProjectFolderName);
        jtxtFieldProjectFolderName.setBounds(25, 30, 205,
            jtxtFieldProjectFolderName.getPreferredSize().height);

        //---- jlblProjectFolderName ----
        jlblProjectFolderName.setText("Project Folder Name:");
        contentPane.add(jlblProjectFolderName);
        jlblProjectFolderName.setBounds(25, 10, 160,
            jlblProjectFolderName.getPreferredSize().height);

        //---- jcmboLocalNetwork ----
        jcmboLocalNetwork.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    jcmboLocalNetworkItemStateChanged(e);
                }
            });
        contentPane.add(jcmboLocalNetwork);
        jcmboLocalNetwork.setBounds(675, 30, 85, 20);

        //---- jlblLocalNetwork ----
        jlblLocalNetwork.setText("Local / Network:");
        contentPane.add(jlblLocalNetwork);
        jlblLocalNetwork.setBounds(675, 10, 85,
            jlblLocalNetwork.getPreferredSize().height);

        contentPane.add(jtxtFieldProjectSpecificTitle);
        jtxtFieldProjectSpecificTitle.setBounds(25, 75, 205, 20);

        //---- jlblProjectSpecificTitle ----
        jlblProjectSpecificTitle.setText("Project Specific Title:");
        contentPane.add(jlblProjectSpecificTitle);
        jlblProjectSpecificTitle.setBounds(25, 55, 135,
            jlblProjectSpecificTitle.getPreferredSize().height);

        //---- jcmboDateAutoManual ----
        jcmboDateAutoManual.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    jcmboDateAutoManualItemStateChanged(e);
                }
            });
        contentPane.add(jcmboDateAutoManual);
        jcmboDateAutoManual.setBounds(775, 30, 110,
            jcmboDateAutoManual.getPreferredSize().height);

        //---- jlblDateCodeAutoManual ----
        jlblDateCodeAutoManual.setText("Date Code Auto / Manual:");
        contentPane.add(jlblDateCodeAutoManual);
        jlblDateCodeAutoManual.setBounds(775, 10, 140,
            jlblDateCodeAutoManual.getPreferredSize().height);
        //======== scrlPaneStandardMenuItems ========
        {
            scrlPaneStandardMenuItems.setViewportView(jtxtAreaStandardMenuItems);
        }

        contentPane.add(scrlPaneStandardMenuItems);
        scrlPaneStandardMenuItems.setBounds(35, 130, 185, 65);

        //---- jlblStandardMenuItems ----
        jlblStandardMenuItems.setText("Standard Menu Items:");
        contentPane.add(jlblStandardMenuItems);
        jlblStandardMenuItems.setBounds(30, 110, 110, 15);
        //======== editTextAreaPopup ========
        {
            //---- insertRowPopup ----
            insertRowPopup.setText("Insert Row");
            insertRowPopup.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        insertRowPopup_MouseClicked(e);
                    }
                });

            editTextAreaPopupMenu.add(insertRowPopup);

            //---- deleteSelectionPopup ----
            deleteSelectionPopup.setText("Delete Selection");
            deleteSelectionPopup.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        deleteSelectionPopup_MouseClicked(e);
                    }
                });
            editTextAreaPopupMenu.add(deleteSelectionPopup);

            //---- copySelectionPopup ----
            copySelectionPopup.setText("Copy Selection");
            copySelectionPopup.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        copySelectionPopup_MouseClicked(e);
                    }
                });

            editTextAreaPopupMenu.add(deleteSelectionPopup);

            //---- pasteSelectionPopup ----
            pasteSelectionPopup.setText("Paste Selection");
            pasteSelectionPopup.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        pasteSelectionPopup_MouseClicked(e);
                    }
                });
            editTextAreaPopupMenu.add(deleteSelectionPopup);
        }

        // Add popup menu to contentpane
        //contentPane.add(editTextAreaPopupMenu);

        { // compute preferred size

            Dimension preferredSize = new Dimension();

            for (int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width,
                        preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height,
                        preferredSize.height);
            }

            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }

        pack();
        setLocationRelativeTo(getOwner());

        // End of component initialization 
    }

    public static void main(String[] args) {
        // The static utility method invokeLater(Runnable) is intended to execute a new runnable
        // thread from a Swing application without disturbing the normal sequence of event dispatching
        // from the GUI.
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        // Set the look and feel for the current OS (Windows) Scheme and
                        // all subsequent jFrames will also have the same look and feel
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                        Edit_XML_File2 edit_XML_File = new Edit_XML_File2();

                        edit_XML_File.setLocationRelativeTo(null);
                        edit_XML_File.setSize(930, 275);
                        edit_XML_File.setResizable(false);
                        edit_XML_File.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
    }
}

Open in new window


Change the network combo and see the popup menu
0
AttilaBAuthor Commented:
krakatoa, I cannot Extend JFrame and MouseAdapter at the same time, so I wrote it like the following based on your code example:

	    this.addMouseListener(new MouseAdapter() { 
	              public void mousePressed(MouseEvent me) {  
                        if (me.isPopupTrigger())
                                {
                                    editTextAreaPopupMenu.show(me.getComponent(),me.getX(),me.getY());
                                    editTextAreaPopupMenu.requestFocus();
                                    editTextAreaPopupMenu.setInvoker(edit_XML_File);
                                    editTextAreaPopupMenu.show(edit_XML_File, 0, 0);
                                    editTextAreaPopupMenu.setVisible(true);
                                    me.consume();   
                                   }
	              } 
            }); 
            
            

Open in new window


But then it cannot find the JFrame 'edit_XML_File' instance.  So, I don't know what to do. It cannot be this bad to make a pop-up menu to work...
0
AttilaBAuthor Commented:
Hi CEHJ, you wrote:

Change the network combo and see the popup menu

I tried the exact code that you posted with this message, and it doesn't seem to bring up the pop-up.
I can even remove network combo and still it doesn't seem to do the popup.
Did the code that you posted there work with the pop-up for you?
0
krakatoaCommented:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;



class Popup extends JFrame{

static JPopupMenu jpop;
static Popup pop;

public static void main(String[] args){

pop = new Popup();

pop.setSize(new Dimension(200,300));
pop.setVisible(true);
pop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	jpop = new JPopupMenu();

    pop.addMouseListener(new mouseadapter(jpop));
		
	JMenuItem jmi1 = new JMenuItem("Simon Says");
	JMenuItem jmi2 = new JMenuItem("Put your hands in the air");
	
	jpop.add(jmi1);
	jpop.add(jmi2);
        

}

	public static class mouseadapter extends MouseAdapter{

        private JPopupMenu jpop;


        public mouseadapter(JPopupMenu jpop){

          this.jpop = jpop;
          jpop.setInvoker(pop);

        }


	public boolean popup(MouseEvent ev)
	{
	   if (ev.isPopupTrigger())
	   {
      
      		jpop.show(ev.getComponent(),ev.getX(),ev.getY());
		jpop.requestFocus();
		ev.consume();	
	        return true;
	   }

           else return false;
	}


	public void mouseClicked(MouseEvent e)
	{
	   if (popup(e)) return;
	   // ...
	}


	public void mousePressed(MouseEvent e)
	{
	   if (popup(e)) return;
	   // ...
	}


	public void mouseReleased(MouseEvent e)
	{
	   if (popup(e)) return;
	   // ...
	}


    }//end class
	
	
}

Open in new window


... and do a right click.
0
AttilaBAuthor Commented:
Hi krakatoa,
Your code seems to work really nicely I just ran it. Let me update my code based on yours tomorrow morning and
I will let you know and accept solution.
0
CEHJCommented:
I tried the exact code that you posted with this message, and it doesn't seem to bring up the pop-up.
You were probably running your own ;)

Popped up
0
AttilaBAuthor Commented:
CEHJ, I tried it again, and it works for me just like your screen-shot above. If you change from FOO to BAR on the Local / Network it brings up the menus in 0:0 position of the JFrame. But it doesn't work as a pop-up menu with right-click:
CEHJ - Code running
On the other hand krakatoa's code works as a pop-up with right mouse-click, I just need to modify my original code according to his scheme:
krakatoa's code running on my machine
Will get there later on today and get back to you.
0
CEHJCommented:
But it doesn't work as a pop-up menu with right-click:

So - add the code i posted that shows it to the correct mouse listener method if that's what you want
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
OK with me. You ought, though, to defer to CEHJ for any true substance on these issues, rather than myself, as he is mammothly more knowledgeable in Java. My suggestions, whilst they hopefully might work, ;), might not be as cognisant of wider (OOP) principles or regular paradigms as they should be.
0
CEHJCommented:
There's not much mystery or complexity to this - you just call show on the menu wherever it's convenient (it was NOT being called anywhere in the code afaicr)
0
AttilaBAuthor Commented:
Now that I got sufficiently confused about this - as of how I should do this PROPERLY- I started looking at a book: Java - How to program, 9th edition - Deitel (2012) on Page 1015, and found an example I like, and expanded it into something runnable:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.ButtonGroup;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class PopupFrame extends JFrame
    {
        private JRadioButtonMenuItem[] items; // holds items for colors
        private final Color[] colorValues = { Color.BLUE, Color.YELLOW, Color.RED }; // colors to be used
        private JPopupMenu popupMenu; // allows user to select color
        // no-argument constructor sets up GUI
public PopupFrame()
    {
            ItemHandler handler = new ItemHandler(); // handler for menu items
            String[] colors = { "Blue", "Yellow", "Red" }; // array of colors
            ButtonGroup colorGroup = new ButtonGroup(); // manages color items
            popupMenu = new JPopupMenu(); // create pop-up menu
            items = new JRadioButtonMenuItem[ colors.length ]; // color items
            
            
        // construct menu item, add to pop-up menu, enable event handling
        for ( int count = 0; count < items.length; count++ )
            {
                items[ count ] = new JRadioButtonMenuItem( colors[ count ] );
                popupMenu.add( items[ count ] ); // add item to pop-up menu
                colorGroup.add( items[ count ] ); // add item to button group
                items[ count ].addActionListener( handler ); // add handler
            } // end for
        
        setBackground( Color.WHITE ); // set background to white
        
        // declare a MouseListener for the window to display pop-up menu
        addMouseListener(new MouseAdapter() // anonymous inner class
            {
        // handle mouse press event
        public void mousePressed( MouseEvent event )
        {
            checkForTriggerEvent(event ); // check for trigger
                } // end method mousePressed
// handle mouse release event
    public void mouseReleased( MouseEvent event )
    {
        checkForTriggerEvent( event ); // check for trigger
        } // end method mouseReleased
        // determine whether event should trigger pop-up menu
        private void checkForTriggerEvent( MouseEvent event )
    {
        if ( event.isPopupTrigger() )
            popupMenu.show( event.getComponent(), event.getX(), event.getY() );
} // end method checkForTriggerEvent
} // end anonymous inner class
); // end call to addMouseListener
} // end PopupFrame constructor
// private inner class to handle menu item events

public static void main(String[] args) {
                  
                          // The static utility method invokeLater(Runnable) is intended to execute a new runnable
                          // thread from a Swing application without disturbing the normal sequence of event dispatching
                          // from the GUI.
                  
                  
               SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                         try {
                                             // Set the look and feel for the current OS (Windows) Scheme and
                                             // all subsequent jFrames will also have the same look and feel
                                             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                                             
                                             PopupFrame popupFrame = new PopupFrame();
                                             
                                             popupFrame.setLocationRelativeTo(null);
                                             popupFrame.setSize(930,275);
                                             popupFrame.setResizable(false);
                                             popupFrame.setVisible(true);        
                                             
                                         } catch (Exception e) {
                                             e.printStackTrace();
                                         }
                                    }
                                });
                          }  

private class ItemHandler implements ActionListener
    {
// process menu item selections
    public void actionPerformed( ActionEvent event )
    {
// determine which menu item was selected
        for ( int i = 0; i < items.length; i++ )
        {
            if ( event.getSource() == items[ i ] )
            {
            getContentPane().setBackground( colorValues[ i ] );
            return;
            } // end if
        } // end for
    } // end method actionPerformed
    } // end private inner class ItemHandler
} // end class PopupFrame

Open in new window


Runs like this:
Running Program
So, I will try to use this for my pop-up example. I will let you know what I find when I implement it for the full program. You guys were trying to help me a lot, so I am going to share the credit between you.
0
CEHJCommented:
:)
0
krakatoaCommented:
Well a) it's good that you are on the home straight with this, and b) that you have been good enough to recognise the bit of help we could provide. But I'm not entirely sure about the "doing it properly" bit, as the Deitel code is hardly any different to the code I posted, and functionally works in the same way (except of course it changes colours rather than simply loads a menu popup).

If there is any mystery remaining, for me at least it's as to why the popup can only be acquired via a right-click, :)
0
krakatoaCommented:
You may - despite its age - like to be apprised of this piece of information :

http://bugs.java.com/view_bug.do?bug_id=6192557

. . . whether it still obtains or not, I am unsure, but it relates to my point about setInvoker(). :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.