JDK-4188386 : JMenu doesn't display correctly after selction with JDialog panel on it.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1998-11-09
  • Updated: 1998-11-18
  • Resolved: 1998-11-18
Related Reports
Duplicate :  
Description

Name: krT82822			Date: 11/09/98


JMenu does not display correctly after clicking one
of the menu Item, which bring up a Dialog panel.
The Dialog panel is displayed over the Menu items.
When Dialog panel closes, the Menu items still show
, but you can not click on it, and paint ugly if
you click on the menu item.

The provided source code was modified from Java
Tutorial for JMenu from Sun. Only JDialog Panel
was added to show this problem. 

1) run the MenuDemo
2) Click on the  'A Menu' menu.
3) Click on the 'A text-only menu item' menu item.
4) 3) brings up Dialog Panel.
5) click on the Cancel Button to close the Dialog panel.
6) The 'A Menu' popup menu still display, click on
any one of the menu item, not response, and paint
incorrectly.

7) This may not happen all the time. By repeating
above 2) to 6) 10 times or moere, It will happen.
If the Dialog panel is AWT Dialog, it seems to happen
less frequently.

(By the way, there is another bug related with 
popup Menu. The JCheckBoxMenuItem doesn't dispaly
check or uncheck in Jpopup Menu.)

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.ImageIcon;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.*;

/*
 * This class adds event handling to MenuLookDemo.
 */
public class MenuDemo extends JFrame 
                      implements ActionListener, ItemListener {
    JTextArea output;
    JScrollPane scrollPane;
    String newline = "\n";

    public MenuDemo() {
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;
        JRadioButtonMenuItem rbMenuItem;
        JCheckBoxMenuItem cbMenuItem;

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //Add regular components to the window, using the default BorderLayout.
        Container contentPane = getContentPane();
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);
        contentPane.add(scrollPane, BorderLayout.CENTER);

        //Create the menu bar.
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        //Build the first menu.
        menu = new JMenu("A Menu");
        menuBar.add(menu);

        //a group of JMenuItems
        menuItem = new JMenuItem("A text-only menu item");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        menuItem = new JMenuItem("Both text and icon", 
                                 new ImageIcon("images/middle.gif"));
        menuItem.addActionListener(this);
        menu.add(menuItem);
        menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
        menuItem.addActionListener(this);
        menu.add(menuItem);

        //a group of radio button menu items
        menu.addSeparator();
        ButtonGroup group = new ButtonGroup();
        rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
        rbMenuItem.setSelected(true);
        group.add(rbMenuItem);
        rbMenuItem.addActionListener(this);
        menu.add(rbMenuItem);
        rbMenuItem = new JRadioButtonMenuItem("Another one");
        group.add(rbMenuItem);
        rbMenuItem.addActionListener(this);
        menu.add(rbMenuItem);

        //a group of check box menu items
        menu.addSeparator();
        cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
        cbMenuItem.addItemListener(this);
        menu.add(cbMenuItem);
        cbMenuItem = new JCheckBoxMenuItem("Another one");
        cbMenuItem.addItemListener(this);
        menu.add(cbMenuItem);

        //a submenu
        menu.addSeparator();
        submenu = new JMenu("A submenu");
        menuItem = new JMenuItem("An item in the submenu");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menuItem = new JMenuItem("Another item");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);

        //Build second menu in the menu bar.
        menu = new JMenu("Another Menu");
        menuBar.add(menu);

    }

    public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource());
        String s = "Action event detected."
                   + newline
                   + "    Event source: " + source.getText()
                   + " (an instance of " + getClassName(source) + ")";
        output.append(s + newline);
		JeditBookmark();
    }

    public void itemStateChanged(ItemEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource());
        String s = "Item event detected."
                   + newline
                   + "    Event source: " + source.getText()
                   + " (an instance of " + getClassName(source) + ")"
                   + newline
                   + "    New state: " 
                   + ((e.getStateChange() == ItemEvent.SELECTED) ?
                     "selected":"unselected");
        output.append(s + newline);
		
    }

    // Returns just the class name -- no package info.
    protected String getClassName(Object o) {
        String classString = o.getClass().getName();
        int dotIndex = classString.lastIndexOf(".");
        return classString.substring(dotIndex+1);
    }

    public static void main(String[] args) {
        MenuDemo window = new MenuDemo();

        window.setTitle("MenuDemo");
        window.setSize(450, 260);
		Location.centerFrame(window);

		window.setVisible(true);
    }


	private JButton editSel;
	JDialog editD;
	public void JeditBookmark()
	{
		JList urlList = new JList();
		urlList.setFont(new Font("SansSerif",Font.PLAIN,14));
		JScrollPane scrollPanel = new JScrollPane(urlList);
		editD = new JDialog(this, "Edit Bookmarks", true);

		editD.getContentPane().setLayout(new BorderLayout());
	    editD.setSize(500, 460);


	    JPanel editp = new JPanel(new BorderLayout());
	    editp.setBackground(Color.lightGray);

		JPanel editbp = new JPanel(new GridLayout(7, 1,10,15));
		JPanel editbbp = new JPanel();
		
	    editbp.setBackground(Color.lightGray);
		
    	   
		editp.add("Center", scrollPanel);


		editSel = new JButton("  Select  ");
		editbp.add(editSel);

		JButton editCopy = new JButton("   Copy   ");
		editbp.add(editCopy);


		JButton editDel = new JButton("  Delete  ");
		editbp.add(editDel);

		JButton editUp = new JButton("   Up   ");
		editbp.add(editUp);

		JButton editDown = new JButton("   Down   ");
		editbp.add(editDown);


		JButton editOK = new JButton("    OK    ");
		editbp.add(editOK);


		JButton editCancel = new JButton("  Cancel  ");
		editbp.add(editCancel);
		
		editCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				editD.dispose();
			}
		});

		editD.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				editD.dispose();
			}

		} );
		editD.getContentPane().add(editp);
		editbbp.add(editbp);
		editD.getContentPane().add(editbbp,"East");
		Location.centerLocation(this,editD);

		editD.setResizable(false);
		editD.show();
		
	}	

}
class Location 
{
  public static void centerLocation(JFrame fr, JDialog d)
	{
		Point po = fr.getLocation();
		int x=po.x+(fr.getSize().width-d.getSize().width)/2;
		int y=po.y+(fr.getSize().height-d.getSize().height)/2;
		d.setLocation(x,y);
	}
  public static void centerFrame(JFrame f)
  {
  		Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle frameDim = f.getBounds();
        f.setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
  } 
}
(Review ID: 42182)
======================================================================

Comments
WORK AROUND Name: krT82822 Date: 11/09/98 * If parent JFrame is bigger than JDialog in at least one dimension, the problem goes away (proper repaint of JFrame occurs) * Can also work around it by doing explicit repaint() on JFrame either before or after JDialog is shown ======================================================================
11-06-2004