Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to Disable the JComboBox Popup Permanently ?

843807Jul 23 2010 — edited Jul 23 2010
Hi I want to disable the pop-up of the JComboBox Permanently when user try to select the item from it must be invisible.

I am trying to set the pop-up size to zero. But wont work.
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;

public class DisableComboPopup extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static JComboBox   comboBox;

    public DisableComboPopup() {

        final JFrame frame = new JFrame( "Modified JCombox GUI" );
        frame.getContentPane().setBackground( Color.PINK );

        final String[] description =
            {"AAAAA", "BBBBBB", "CCCCCCC", "DDDDDDD", "EEEEEEEE", "FFFFFFFF", "GGGGGGG", "HHHHHHHHH"};

        comboBox = new JComboBox( description );
        comboBox.setBounds( 350, 100, 250, 30 );

        comboBox.requestFocusInWindow();

        final JScrollPane scrollPane1 = new JScrollPane( comboBox );
        scrollPane1.setBounds( 350, 100, 250, 30 );
        scrollPane1.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER );
        scrollPane1.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
        frame.add( scrollPane1 );

        comboBox.setUI( new myComboUI() );

        frame.setLayout( null );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        frame.setSize( 1024, 768 );
        frame.setVisible( true );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    }

    public static void main( final String[] args ) {

        new DisableComboPopup();
    }

    public class myComboUI extends BasicComboBoxUI {

        @Override
        protected ComboPopup createPopup() {

            final BasicComboPopup popup = new BasicComboPopup( comboBox ) {

                private static final long serialVersionUID = 1L;

                @Override
                public void setPopupSize( final int arg0, final int arg1 ) {

                    // TODO Auto-generated method stub
                    super.setPopupSize( 0, 0 );
                }
            };
            return popup;
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 20 2010
Added on Jul 23 2010
19 comments
2,165 views