I have a JPopupMenu that has a JTextField inside. If part of the menu is painted outside the main window, the window frame flashes. If all of the menu is painted within the window, there is no flashing. After some sleuthing, I found that if the JTextField is set to not be focusable, then the flashing stops. This isn't a viable solution as you can't click and type in the text field if it is not focusable.
In the example below, if you left click in the window, the popup will have a non focusable textfield and displays as expected. If you right click, the popup will have a focusable text field and the application frame will flash before the popup is displayed.
If you increase the size of the window to be larger than the popup, there will be no flashing in either case.
How can I get the menu to display without the application frame flashing when part of it displays outside the window?
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PopupTesting
{
public static void main( String[] args )
{
SwingUtilities.invokeLater( () ->
{
JPopupMenu popupMenu = new JPopupMenu();
for( int i = 0; i < 10; i++ )
{
popupMenu.add( new JCheckBoxMenuItem( "A Menu " + i ) );
}
JTextField textField = new JTextField( "" );
popupMenu.add( textField );
JFrame frame = new JFrame( "Popup Testing" );
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
JLabel header = new JLabel( "Header" );
header.setPreferredSize( new Dimension( 200, 100 ) );
header.addMouseListener( new MouseAdapter()
{
@Override
public void mouseClicked( MouseEvent e )
{
if( SwingUtilities.isLeftMouseButton( e ) )
{
textField.setFocusable( false );
}
else
{
textField.setFocusable( true );
}
popupMenu.show( header, e.getX(), e.getY() );
}
} );
frame.add( header );
frame.pack();
frame.setVisible( true );
} );
}
}