3 Replies Latest reply on Oct 6, 2008 7:52 AM by 843806

    Hide JComboBox Arrow?

    843806
      Greetings All
      any ideas how to hide or remove the JComboBox Arrow not to disable it?
      thanks in advance
        • 1. Re: Hide JComboBox Arrow?
          843806
          I don't know of any "approved" way, but one "cheatin'" way:
          import java.awt.*;
          import javax.swing.*;
          
          public class ComboBoxArrowGone2
          {
            private static final String[] STRINGS = 
            {
              "Fe", "Fi", "Fo", "Fum", "Fubar"
            };
            private JPanel mainPanel = new JPanel();
            private JComboBox comboBox = new JComboBox(STRINGS);
          
            public ComboBoxArrowGone2()
            {
              mainPanel.add(comboBox);
              for (Component component : comboBox.getComponents())
              {
                if (component instanceof AbstractButton)
                {
                  if (component.isVisible())
                  {
                    component.setVisible(false);
                    mainPanel.revalidate();
                    //mainPanel.repaint();
                  }
                }
              }
            }
            
            public JComponent getComponent()
            {
              return mainPanel;
            }
          
            private static void createAndShowUI()
            {
              JFrame frame = new JFrame("Test");
              frame.getContentPane().add(new ComboBoxArrowGone2().getComponent());
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
            }
          
            public static void main(String[] args)
            {
              java.awt.EventQueue.invokeLater(new Runnable()
              {
                public void run()
                {
                  createAndShowUI();
                }
              });
            }
          }
          Edited by: Encephalopathic on Oct 5, 2008 7:28 PM
          • 2. Re: Hide JComboBox Arrow?
            794342
            this is from a recent-ish thread, which makes the combo the same width as the popup
            import javax.swing.*;
            import java.awt.*;
            class Testing
            {
              public void buildGUI()
              {
                JComboBox cbo = new JComboBox(new String[]{"London","Madrid","New York","Rome","Sydney"});
                cbo.setUI(new javax.swing.plaf.metal.MetalComboBoxUI(){
                  public void layoutComboBox(Container parent, MetalComboBoxLayoutManager manager) {
                    super.layoutComboBox(parent, manager);
                    arrowButton.setBounds(0,0,0,0);
                  }
                });
                JFrame f = new JFrame();
                f.getContentPane().add(cbo);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
              }
              public static void main(String[] args)
              {
                SwingUtilities.invokeLater(new Runnable(){
                  public void run(){
                    new Testing().buildGUI();
                  }
                });
              }
            }
            • 3. Re: Hide JComboBox Arrow?
              843806
              Thank you both,that's really what i want