Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPopupMenu with JInternalFrame, popup menu doesn't work

843804Jun 3 2005 — edited Jun 3 2005
hi, i have this problem, as shown in the sample code at the end of this post.. basically, i have a table, and i added a JPopupMenu onto the table.. the popup menu works well when running the table class, though, when i call the table class in a JInternalFrame environment, the popup menu won't work.. anyone know why?
///Basic sample table code, when run this alone, the popup menu will work
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

public class TableBasic extends JPanel
{
	private JTable table;
	public TableBasic()
	{
		String[] columnNames = { "Date", "String", "Integer", "Boolean" };
		Object[][] data =
			{  { new Date(), "A", new Integer(1), Boolean.TRUE },
				{ new Date(), "B", new Integer(2), Boolean.FALSE },
				{ new Date(), "C", new Integer(9), Boolean.TRUE },
				{ new Date(), "D", new Integer(4), Boolean.FALSE}
		};

		table = new JTable(data, columnNames)
		{
			//Returning the Class of each column will allow different
			//renderers to be used based on Class

			public Class getColumnClass(int column)
			{
				return getValueAt(0, column).getClass();
			}
		};
		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane);
	}
	
	public void createPopupMenu()
	{
		JMenuItem menuItem;
		//Create the popup menu.
		JPopupMenu popup = new JPopupMenu();
		menuItem = new JMenuItem("A popup menu item");
		//menuItem.addActionListener(this);
		popup.add(menuItem);
		menuItem = new JMenuItem("Another popup menu item");
		//menuItem.addActionListener(this);
		popup.add(menuItem);
		//Add listener to the text area so the popup menu can come up.
		MouseListener popupListener = new PopupListener(popup);
		table.addMouseListener(popupListener);
	}

	public static void main(String[] args)
	{
		JFrame frame = new JFrame();
		TableBasic table = new TableBasic();
		table.createPopupMenu();
		frame.setContentPane(table);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		//frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	class PopupListener extends MouseAdapter
	{
		JPopupMenu popup;
		PopupListener(JPopupMenu popupMenu)
		{
			popup = popupMenu;
		}
		public void mousePressed(MouseEvent e)
		{
			maybeShowPopup(e);
		}
		public void mouseReleased(MouseEvent e)
		{
			maybeShowPopup(e);
		}
		private void maybeShowPopup(MouseEvent e)
		{
			if (e.isPopupTrigger())
			{
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
}
///when integrate the previous table into here, popup menu won't work
import java.awt.*;
import javax.swing.*;

public class InternalFrameBasic
	extends JFrame
	//implements ActionListener
{
	private JDesktopPane desktop;
	private JInternalFrame menuWindow;
	public static final int desktopWidth = 800;
	public static final int desktopHeight = 700;
	
	public InternalFrameBasic(String title)
	{
		super(title);

		//Set up the GUI.
		desktop = new JDesktopPane();
		desktop.putClientProperty("JDesktopPane.dragMode", "outline");
		//Because we use pack, it's not enough to call setSize.
		//We must set the desktop's preferred size.
		desktop.setPreferredSize(new Dimension(desktopWidth, desktopHeight));
		setContentPane(desktop);

		createMenuWindow();
		desktop.add(menuWindow); //DON'T FORGET THIS!!!
		Dimension displaySize = menuWindow.getSize();
		menuWindow.setSize(desktopWidth, displaySize.height);
	}
	
	private void createMenuWindow()
	{
		menuWindow =
					new JInternalFrame("Event Watcher", true, //resizable
																			true, //closable
																			false, //not maximizable
																			true); //iconifiable

		menuWindow.setContentPane(new TableBasic());
		menuWindow.pack();
		menuWindow.setVisible(true);
	}
	
	/**
	 * Create the GUI and show it.  For thread safety,
	 * this method should be invoked from the
	 * event-dispatching thread.
	 */
	private static void createAndShowGUI()
	{
		//Make sure we have nice window decorations.
		//JFrame.setDefaultLookAndFeelDecorated(true);

		//Create and set up the window.
		JFrame frame = new InternalFrameBasic("Example Internal Frame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args)
	{
		//Schedule a job for the event-dispatching thread:
		//creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				createAndShowGUI();
			}
		});
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 1 2005
Added on Jun 3 2005
1 comment
403 views