Results 1 to 1 of 1
  1. #1
    ShinobiAC is offline Member
    Join Date
    Aug 2009
    Rep Power

    Default JPopupMenu, JTextField, and Canvas not working together

    Hello all,

    I'm writing a graphical program and am trying to use a Canvas object for the majority of my main window. I have a BorderLayout set with the Canvas placed into the CENTER and a JTextField to the SOUTH. The problem I am getting is that once I click into the JTextArea and type letters, I am unable to right click into my Canvas to retrieve my JPopupMenu. Printing out the location and visibility of the JPopupMenu states that the menu is visible and is at the appropriate location, yet nothing shows. I've included the simplest program I could write that duplicates the bug below. The problem does not occur when a JPanel is used in the CENTER instead.

    Also, the JPopupMenu will show when a right click is used that would place the popup outside of the window.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JFrame;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JPopupMenu;
    import javax.swing.JTextField;
    public class Problem extends Canvas implements MouseListener{
    	private JFrame window = new JFrame();
    	private JTextField message = new JTextField();
    	public static void main(String args[])
    		Problem problem = new Problem();
    	public Problem()
    	private void initMainWindow()
    		JPanel panel = (JPanel)window.getContentPane();
    		panel.setLayout(new BorderLayout());
    		setPreferredSize(new Dimension(200,200));
    		panel.add(this, BorderLayout.CENTER);
    		panel.add(message, BorderLayout.SOUTH);
    	public void mousePressed(MouseEvent e) {
    		if(e.getButton() == MouseEvent.BUTTON3)
    			JPopupMenu menu = new JPopupMenu();
    			JMenuItem testItem = new JMenuItem("Test");
    			menu.add(testItem);, e.getXOnScreen() - window.getX(), e.getYOnScreen() - window.getY());
    	public void mouseClicked(MouseEvent e) {}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    Any help or workarounds would be greatly appreciated. Thanks!

    EDIT: I found a workaround. After accidentally minimizing, then restoring the window, I found the JPopupMenu began to function again. So, I tried a few JFrame methods until I found that calling window.validate() before displaying the JPopupMenu worked. Hopefully this quick fix helps anybody else running into this problem, though I'm still unsure as to why it functions like this in the first place. I suppose it may just be a bug.
    Last edited by ShinobiAC; 08-04-2009 at 04:31 PM. Reason: workaround found

Similar Threads

  1. Replies: 0
    Last Post: 07-05-2009, 01:31 PM
  2. JTextField in a JPopupMenu
    By baran_khan in forum AWT / Swing
    Replies: 6
    Last Post: 05-02-2009, 10:43 PM
  3. JMenu to JPopupMenu
    By carderne in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 07:07 PM
  4. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  5. JPopupMenu
    By hungleon88 in forum NetBeans
    Replies: 8
    Last Post: 01-15-2009, 06:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts