6 Replies Latest reply on Oct 11, 2007 3:33 AM by 843806

    Scrollable PopupMenu

    843804
      FWIW, here is a little demonstration on how to create a JPopupMenu with a vertical scrollbar:

      ComboBoxPopupMenuDemo.java
      ===========================
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.event.*;
      
      public class ComboBoxPopupMenuDemo extends JPanel {
         public ComboBoxPopupMenuDemo() {
           super(new BorderLayout());
           String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig", "Fish", "Horse", "Cow", "Bee", "Skunk" };
           final ScrollablePopupMenu petList=new ScrollablePopupMenu(petStrings);
           petList.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                System.out.println(petList.getSelectedItem());
              }
           });
           final JLabel lab=new JLabel(" ");
           lab.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
                petList.showPopup(e.getX(),e.getY());
              }
           });
           add("Center",lab);
           add("North",petList);
           setPreferredSize(new Dimension(200,100));
         }
         public static void main(String s[]) {
           JFrame frame = new JFrame("ComboBoxDemo");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setContentPane(new ComboBoxPopupMenuDemo());
           frame.pack();
           frame.setVisible(true);
         }
      }
      ScrollablePopupMenu.java
      =====================
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;
      import javax.swing.event.*;
      import javax.swing.plaf.basic.*;
      
      public class ScrollablePopupMenu extends JComboBox {
         public ScrollablePopupMenu(ComboBoxModel aModel) {
           super(aModel);
           setup();
         }
         public ScrollablePopupMenu(final Object items[]) {
           super(items);
           setup();
         }
         public ScrollablePopupMenu(Vector items) {
           super(items);
           setup();
         }
         boolean keySelection=false;
         myBasicComboBoxUI myCBUI;
         private void setup() {
           myCBUI=new myBasicComboBoxUI();
           setUI(myCBUI);
           addKeyListener(new KeyAdapter() {
              public void keyPressed(KeyEvent ke) {
                if (ke.getKeyCode()==10) {
                   keySelection=false;
                   fireActionEvent();
                   return;
                } else if (!myCBUI.getPopup().isVisible()) {
                   ke.consume();
                }
                keySelection=true;
              }
           });
           setPreferredSize(new Dimension(0,0));
           setVisible(true);     
         }
         public void showPopup(int x, int y) {
           setLocation(x,y);
           myCBUI.getPopup().show();
         }
         public ComboPopup getPopup() {
           return myCBUI.getPopup();
         }
         public void setSelectedItem(Object anObject) {
           Object oldSelection = selectedItemReminder;
           if (oldSelection == null || !oldSelection.equals(anObject)) {
              if (anObject != null && !isEditable()) {
                boolean found = false;
                for (int i = 0; i < dataModel.getSize(); i++) {
                   if (anObject.equals(dataModel.getElementAt(i))) {
                     found = true;
                     break;
                   }
                }
                if (!found) return;
              }
              dataModel.setSelectedItem(anObject);
              if (selectedItemReminder != dataModel.getSelectedItem()) selectedItemChanged();
           }
         }
         public void contentsChanged(ListDataEvent e) {
           Object oldSelection = selectedItemReminder;
           Object newSelection = dataModel.getSelectedItem();
           if (oldSelection == null || !oldSelection.equals(newSelection)) selectedItemChanged();
           if (!keySelection) fireActionEvent();
         }
         class myBasicComboBoxUI extends BasicComboBoxUI {               // just to be able to get to the popup
           public ComboPopup getPopup() {
              return popup;
           }
         }
      }
      This program has been compiled and tested on 1.4.2 and is provided on an as-is basis courtesy of yours truly.

      ;o)

      V.V.