-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.3.0
-
x86
-
windows_nt
Name: dc32491 Date: 01/25/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
Display a modal dialog after selecting an item in a popup or pulldown menu.
When the dialog is dismissed, the area under the menu in the originating frame
is not repainted, i.e. it looks as though the menu is still displayed.
Since I was originally seeing the problem with a tree-based app, I took the
SampleTree demo that's shipped with JDK 1.3 and subclassed it to add a popup
menu and pulldown menu item that launches a modal dialog to demonstrate the
problem. Unlike some demos in earlier, closed problems that I believe may have
the same root cause, this demonstrates the behavior 100% of the time.
Compile the demo program SampleTreeWithPopup.java in the same directory as the
sample code from jdk1.3/demo/jfc/SampleTree. Run the program; right mouse
click to pop up the menu; select any item to display the modal dialog, then
dismiss the dialog. Trying this a few times really garbles what's shown in the
frame. Also select the item I added to the 'Tree' pulldown menu.
Run again with any command-line argument to demonstrate a brute-force
workaround: after the dialog is dismissed, a repaint of the frame contents is
requested, and the problem is no longer visible.
The code for this follows:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class SampleTreeWithPopup extends SampleTree
{
static private boolean doRevalidateAfterDialogDismissed_;
static public void main( String args[] )
{
doRevalidateAfterDialogDismissed_ = ( args.length > 0 );
new SampleTreeWithPopup();
}
public SampleTreeWithPopup()
{
super();
initTreePopup();
initMenubarItem();
centerOnScreen( frame );
}
private void initMenubarItem()
{
JMenuBar mb = frame.getJMenuBar();
JMenu treeMenu = ( JMenu )mb.getComponent( 1 );
JMenuItem launchItem = new JMenuItem( "Launch test dialog..." );
launchItem.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ev )
{
launchDialog();
}
} );
treeMenu.add( launchItem );
}
private void initTreePopup()
{
tree.addMouseListener( new MouseAdapter()
{
public void mousePressed( MouseEvent ev )
{
handleMouseEvent( ev );
}
public void mouseReleased( MouseEvent ev )
{
handleMouseEvent( ev );
}
private void handleMouseEvent( MouseEvent ev )
{
if ( ev.isPopupTrigger() )
{
final TreePath tp = tree.getPathForLocation( ev.getX(),
ev.getY() );
if ( tp != null )
{
frame.setCursor( Cursor.getPredefinedCursor(
Cursor.WAIT_CURSOR ) );
tree.setSelectionPath( tp );
final DefaultMutableTreeNode tn =
( DefaultMutableTreeNode )tp.getLastPathComponent();
JPopupMenu menu = new JPopupMenu();
final JMenuItem openItem = new JMenuItem( "Open..." );
openItem.setMnemonic( 'O' );
openItem.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ev )
{
launchDialog();
}
} );
menu.add( openItem );
final JMenuItem delItem = new JMenuItem( "Delete..." );
delItem.setMnemonic( 'D' );
delItem.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ev )
{
launchDialog();
}
} );
menu.add( delItem );
final JMenuItem newItem = new JMenuItem( "New..." );
newItem.setMnemonic( 'N' );
newItem.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ev )
{
launchDialog();
}
} );
menu.add( newItem );
frame.setCursor( Cursor.getDefaultCursor() );
menu.show( tree, ev.getX(), ev.getY() );
}
}
}
} );
}
private void launchDialog()
{
final JDialog d = new JDialog( frame, "Some Dialog", true );
JButton b = new JButton( "Launch and dismiss a few times to see the
problem." );
b.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent ev ) { d.hide(); }
} );
JPanel panel = new JPanel( new BorderLayout() );
panel.add( b );
d.getContentPane().add( panel );
d.setSize( new Dimension( 500, 500 ) );
centerOnScreen( d );
d.show();
/*
** After the dialog is hidden, the popup or pulldown menu that
** showed the dialog is still shown because the area beneath
** the menu wasn't repainted.
*/
/*
** The following is a workaround :-(.
*/
if ( doRevalidateAfterDialogDismissed_ )
{
JComponent c = ( JComponent )frame.getContentPane();
c.revalidate();
c.repaint();
frame.getJMenuBar().revalidate();
frame.getJMenuBar().repaint();
}
}
static public void centerOnScreen( Window w )
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = w.getSize();
Point location = new Point();
location.x = ( screenSize.width / 2 ) - ( windowSize.width / 2 );
location.y = ( screenSize.height / 2 ) - ( windowSize.height / 2 );
w.setLocation( location );
}
}
(Review ID: 115817)
======================================================================
- duplicates
-
JDK-4189244 Swing Popup menu is not being refreshed (cleared) under a Dialog
- Resolved