Search code examples
javaswingawtjcomboboxalways-on-top

JComboBox Dropdown Menu Not Obeying AlwaysOnTop


I have an always-on-top window that I want to remain above all other windows. Using setAlwaysOnTop(true) seems to work for most purposes, but fails when it comes to JComboBox dropdown menus. Is there any way to prevent this from happening? Attached below is a SSCCE and picture of the undesired functionality.

EDIT: Not sure if the behavior is OS-dependent, but I'm noticing the issue on Windows 7 using Java 7. On top is supported on this OS.

EDIT 2: Seems that JPopupMenu has an override on alwaysOnTop() to return true. This is the source of the problem, since on-top components do not have a defined order in how they appear on top of each other (OS-dependent). Worse still, the method is package private. Quite problematic...

Undesired Behavior:

Undesired Behavior

SSCCE:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JTextField;

public class OnTopTest
{
    public static void main( String[] args )
    {
        new OnTopTest();
    }

    public OnTopTest()
    {
        JDialog onTop = new OnTopWindow();
        JDialog other = new OtherWindow();

        System.out.println("IS ON TOP SUPPORTED? " + onTop.isAlwaysOnTopSupported());

        other.setVisible( true );
        onTop.setVisible( true );
    }


    private class OnTopWindow extends JDialog
    {
        public OnTopWindow()
        {
            setLayout( new BorderLayout() );

            JButton button = new JButton("Button");
            add( button, BorderLayout.CENTER );

            setSize( 100, 100 );

            setAlwaysOnTop( true );
        }
    }

    private class OtherWindow extends JDialog
    {
        public OtherWindow()
        {
            setLayout( new BorderLayout() );

            JTextField textField = new JTextField("Text");
            add( textField, BorderLayout.NORTH);

            JButton button = new JButton("Button");
            add( button, BorderLayout.CENTER );

            JComboBox comboBox = new JComboBox( new Object[] {"Item1", "Item2", "Item3"} );
            add( comboBox, BorderLayout.SOUTH );

            setSize( 200, 200 );
        }
    }
}

Solution

  • I'm pretty sure this is handled by the Operating System and that Java cannot force the dropdown to not overlap the other window, as searching for this without specifying Java reported the same behaviour in many different languages.

    You can test with a JMenu to confirm, but I'm sure it will also happen as menus and some other controls (like dropdowns) will (by necessity) show above any other window.