Skip to main content

Adding a JPanel to a JMenu - Focus issues

Please note these forums are being decommissioned and use the new and improved forums at
2 replies [Last post]
Joined: 2010-01-13

Hi all,

I am working on a requirement and have come across something unexpected.

The expected behaviour when working with menu items is when adding a JMenuItems to a JMenu and making the Menu visible, moving the mouse over each of the items highlights that items and allows each to be selected.

However when I add a JPanel to a JMenu, and the JPanel contains the JMenuItems, these items are no longer highlighted or selectable. Further, when mousing over the contained JMenuItems, focus seems to be lost from the JMenu entirely and the popup disappears altogether.
I've constructed the following code to demonstrate this issue:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class TestMenuItems extends JFrame {

private JPopupMenu _thePopup = new JPopupMenu();

public TestMenuItems() {

this.setLayout( new BorderLayout() );
JPanel thePanel = new JPanel();
this.add( thePanel, BorderLayout.CENTER );

this.addMouseListener( new MouseAdapter(){
public void mousePressed(MouseEvent e) {, e.getX(), e.getY());

JMenu theMenu = new JMenu("Menu");
_thePopup.add( theMenu );

thePanel.setBackground( Color.BLUE );
thePanel.setLayout( new BorderLayout() );

JPanel theInnerPanel = new JPanel();
theInnerPanel.setLayout( new BoxLayout( theInnerPanel, BoxLayout.X_AXIS ) );
theInnerPanel.add( new JMenuItem("Press 1") );
theInnerPanel.add( new JMenuItem("Press 2") );

theMenu.add(new JMenuItem("Press 3"));
theMenu.add(new JMenuItem("Press 4"));

public static void main( String[] args ){

TestMenuItems theApp = new TestMenuItems();
theApp.setSize( new Dimension(200, 200));
theApp.setLocation(200, 200);
theApp.setVisible( true );
Does anyone know the reason why this happens and how to make the JMenuItems that are wrapped by the JPanel receive the focus of the mouse. Any idea's?

Just as a reminder, this is a small demonstration of a larger issue I have. I am not interested in how I can perform the same GUI representation using different techniques. Please only respond if you understand what is happening with my example and how to pass the events to the correct components.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2011-01-04

I have solved that issue, I don't know why this is happening but doing below i have resolved.

Changed the JMenuItem to JLabel, then add these to JPanel.

Need to handle mousePressed and mouseMoved method for these JLabel.

Joined: 2010-01-13

Hi Verma018,
Thanks for your reply both here and the other java forum that I posted the question.
Your solution works fine, so thanks very much but I have actually identified the underline problem and now solved it using JMenuItems. I've posted the full solution here:
Thanks again for your help.