Java Code Examples for javax.swing.event.PopupMenuListener

The following examples show how to use javax.swing.event.PopupMenuListener. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: darklaf   Source File: PopupMenuContainer.java    License: MIT License 6 votes vote down vote up
protected PopupMenuListener getMenuListener() {
    if (menuListener == null) {
        menuListener = new PopupMenuAdapter() {
            @Override
            public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
                onHide();
            }

            @Override
            public void popupMenuCanceled(final PopupMenuEvent e) {
                onHide();
            }

            private void onHide() {
                if (popupMenu == null) return;
                popupMenu.removePopupMenuListener(this);
                popupMenu.removeMenuKeyListener(menuKeyListener);
            }
        };
    }
    return menuListener;
}
 
Example 2
private void setupAction() {
	addActionListener(e -> {
		JPopupMenu popupMenu = menuProvider.getPopupMenu();

		if (!popupMenu.isVisible()) {
			// hack to prevent filter popup from opening itself again
			// when you click the button to actually close it while it
			// is open
			if (System.currentTimeMillis() - lastPopupCloseTime < POPUP_CLOSE_DELTA) {
				return;
			}
			popupMenu.addPopupMenuListener(popupListener);
			for (PopupMenuListener listener : otherListeners) {
				popupMenu.addPopupMenuListener(listener);
			}

			popupMenu.show(DropDownPopupButton.this, 0, DropDownPopupButton.this.getHeight() - 1);
			popupMenu.requestFocusInWindow();
		}
	});
}
 
Example 3
Source Project: consulo   Source File: PopupHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static MouseListener installPopupHandler(@Nonnull JComponent component,
                                                @Nonnull ActionGroup group,
                                                @NonNls String place,
                                                @Nonnull ActionManager actionManager,
                                                @Nullable PopupMenuListener menuListener) {
  if (ApplicationManager.getApplication() == null) return new MouseAdapter() {
  };
  PopupHandler popupHandler = new PopupHandler() {
    @Override
    public void invokePopup(Component comp, int x, int y) {
      ActionPopupMenu popupMenu = actionManager.createActionPopupMenu(place, group);
      popupMenu.setTargetComponent(component);
      JPopupMenu menu = popupMenu.getComponent();
      if (menuListener != null) menu.addPopupMenuListener(menuListener);
      menu.show(comp, x, y);
    }
  };
  component.addMouseListener(popupHandler);
  return popupHandler;
}
 
Example 4
Source Project: darklaf   Source File: DarkScrollableTabSupport.java    License: MIT License 5 votes vote down vote up
public DarkScrollableTabSupport(final DarkTabbedPaneUI ui, final int tabPlacement) {
    super(ui);
    this.ui = ui;
    viewport = new DarkScrollableTabViewport(ui);
    tabPanel = new DarkScrollableTabPanel(ui);

    viewport.setView(tabPanel);
    viewport.addMouseWheelListener(this);

    moreTabsButton = ui.createMoreTabsButton();
    moreTabsButton.setVisible(false);
    moreTabsButton.addActionListener(this);

    newTabButton = ui.createNewTabButton();
    newTabButton.setVisible(PropertyUtil.getBooleanProperty(ui.tabPane, DarkTabbedPaneUI.KEY_SHOW_NEW_TAB_BUTTON));

    scrollPopupMenu = new ScrollPopupMenu(UIManager.getInt(DarkTabbedPaneUI.KEY_MAX_POPUP_HEIGHT));
    PopupMenuListener popupMenuListener = new PopupMenuAdapter() {
        @Override
        public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
            lastClickEvent = System.currentTimeMillis();
        }
    };
    scrollPopupMenu.addPopupMenuListener(popupMenuListener);

    ui.tabPane.add(moreTabsButton);
    timer = new Timer(SCROLL_REWIND_DELAY, e -> endScroll());
    timer.setRepeats(false);
}
 
Example 5
Source Project: darklaf   Source File: PopupMenuContainer.java    License: MIT License 5 votes vote down vote up
public void setPopupMenu(final JPopupMenu popupMenu) {
    MenuKeyListener keyListener = getMenuKeyListener();
    PopupMenuListener popupMenuListener = getMenuListener();
    this.popupMenu = popupMenu;
    if (popupMenu != null) {
        popupMenu.removeMenuKeyListener(keyListener);
        popupMenu.removePopupMenuListener(popupMenuListener);
        popupMenu.addMenuKeyListener(keyListener);
        popupMenu.addPopupMenuListener(popupMenuListener);
    }
}
 
Example 6
Source Project: xdm   Source File: MainWindow.java    License: GNU General Public License v2.0 5 votes vote down vote up
private JMenu addSubMenu(String id, JMenu parentMenu, PopupMenuListener popupListener) {
	JMenu menu = new JMenu(StringResource.get(id));
	menu.setName(id);
	menu.setFont(FontResource.getNormalFont());
	// menu.setForeground(ColorResource.getLightFontColor());
	menu.addActionListener(this);
	// menu.setBackground(ColorResource.getDarkerBgColor());
	menu.setBorderPainted(false);
	menu.getPopupMenu().addPopupMenuListener(popupListener);
	parentMenu.add(menu);
	return menu;
}
 
Example 7
/**
 * Maps {@code JPopupMenu.addPopupMenuListener(PopupMenuListener)}
 * through queue
 */
public void addPopupMenuListener(final PopupMenuListener popupMenuListener) {
    runMapping(new MapVoidAction("addPopupMenuListener") {
        @Override
        public void map() {
            ((JPopupMenu) getSource()).addPopupMenuListener(popupMenuListener);
        }
    });
}
 
Example 8
/**
 * Maps {@code JPopupMenu.removePopupMenuListener(PopupMenuListener)}
 * through queue
 */
public void removePopupMenuListener(final PopupMenuListener popupMenuListener) {
    runMapping(new MapVoidAction("removePopupMenuListener") {
        @Override
        public void map() {
            ((JPopupMenu) getSource()).removePopupMenuListener(popupMenuListener);
        }
    });
}
 
Example 9
@Override
public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
	lastPopupCloseTime = System.currentTimeMillis();
	JPopupMenu jPopupMenu = (JPopupMenu) e.getSource();
	jPopupMenu.removePopupMenuListener(this);

	for (PopupMenuListener otherListener : otherListeners) {
		jPopupMenu.removePopupMenuListener(otherListener);
	}

}
 
Example 10
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

  JComboBox<String> combo = new JComboBox<String>(makeModel()) {
    private transient PopupMenuListener listener;
    @Override public void updateUI() {
      removePopupMenuListener(listener);
      super.updateUI();
      if (getUI() instanceof WindowsComboBoxUI) {
        setUI(new RightPopupWindowsComboBoxUI());
      } else {
        setUI(new RightPopupBasicComboBoxUI());
      }
      listener = new RightPopupMenuListener();
      addPopupMenuListener(listener);
    }
  };

  JPanel p = new JPanel(new GridLayout(2, 2, 5, 5));
  p.add(new JComboBox<>(makeModel()));
  p.add(new JLabel("<- default"));
  p.add(combo);
  p.add(new JLabel("<- RightPopupMenuListener"));

  add(p, BorderLayout.NORTH);
  setPreferredSize(new Dimension(320, 240));
}
 
Example 11
Source Project: ghidra   Source File: MenuManager.java    License: Apache License 2.0 4 votes vote down vote up
public PopupMenuListener getMenuHandler() {
	return menuHandler;
}
 
Example 12
Source Project: netbeans   Source File: RuntimeComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public void removePopupMenuListener(PopupMenuListener l) {
    combo.removePopupMenuListener(l);
}
 
Example 13
Source Project: netbeans   Source File: RuntimeComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public PopupMenuListener[] getPopupMenuListeners() {
    return combo.getPopupMenuListeners();
}
 
Example 14
Source Project: netbeans   Source File: RuntimeComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public void addPopupMenuListener(PopupMenuListener l) {
    combo.addPopupMenuListener(l);
}
 
Example 15
Source Project: netbeans   Source File: GrowingComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public void removePopupMenuListener(PopupMenuListener l) {
    combo.removePopupMenuListener(l);
}
 
Example 16
Source Project: netbeans   Source File: GrowingComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public PopupMenuListener[] getPopupMenuListeners() {
    return combo.getPopupMenuListeners();
}
 
Example 17
Source Project: netbeans   Source File: GrowingComboBox.java    License: Apache License 2.0 4 votes vote down vote up
public void addPopupMenuListener(PopupMenuListener l) {
    combo.addPopupMenuListener(l);
}
 
Example 18
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 4 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());

  String[] model = {"123456", "7890", "a"};

  JComboBox<String> comboBox0 = new JComboBox<>(model);
  comboBox0.setEditable(true);

  JComboBox<String> comboBox1 = new JComboBox<String>(model) {
    private transient PopupMenuListener handler;
    @Override public void updateUI() {
      removePopupMenuListener(handler);
      super.updateUI();
      setEditable(true);
      handler = new SelectItemMenuListener();
      addPopupMenuListener(handler);
    }
  };

  JComboBox<String> comboBox2 = new JComboBox<String>(model) {
    private static final int MAX_HISTORY = 10;
    private static final String ENTER_PRESSED = "enterPressed";
    private transient PopupMenuListener handler;
    @Override public void updateUI() {
      removePopupMenuListener(handler);
      getActionMap().put(ENTER_PRESSED, null);
      super.updateUI();
      Action defaultAction = getActionMap().get(ENTER_PRESSED);
      Action a = new AbstractAction() {
        @Override public void actionPerformed(ActionEvent e) {
          boolean isPopupVisible = isPopupVisible();
          setPopupVisible(false);
          DefaultComboBoxModel<String> m = (DefaultComboBoxModel<String>) getModel();
          String str = Objects.toString(getEditor().getItem(), "");
          if (m.getIndexOf(str) < 0) {
            m.removeElement(str);
            m.insertElementAt(str, 0);
            if (m.getSize() > MAX_HISTORY) {
              m.removeElementAt(MAX_HISTORY);
            }
            setSelectedIndex(0);
            setPopupVisible(isPopupVisible);
          } else {
            defaultAction.actionPerformed(e);
          }
        }
      };
      getActionMap().put(ENTER_PRESSED, a);
      setEditable(true);
      handler = new SelectItemMenuListener();
      addPopupMenuListener(handler);
    }
  };

  JPanel p = new JPanel(new GridLayout(0, 1));
  p.add(new JLabel("Default:", SwingConstants.LEFT));
  p.add(comboBox0);
  p.add(Box.createVerticalStrut(15));
  p.add(new JLabel("popupMenuWillBecomeVisible:", SwingConstants.LEFT));
  p.add(comboBox1);
  p.add(Box.createVerticalStrut(15));
  p.add(new JLabel("+enterPressed Action:", SwingConstants.LEFT));
  p.add(comboBox2);
  add(p, BorderLayout.NORTH);
  setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 19
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 4 votes vote down vote up
private MainPanel() {
  super(new BorderLayout(15, 15));

  UIManager.put("ScrollBar.width", 10);
  UIManager.put("ScrollBar.thumbHeight", 20); // GTKLookAndFeel, SynthLookAndFeel, NimbusLookAndFeel
  UIManager.put("ScrollBar.minimumThumbSize", new Dimension(30, 30));
  UIManager.put("ScrollBar.incrementButtonGap", 0);
  UIManager.put("ScrollBar.decrementButtonGap", 0);
  UIManager.put("ScrollBar.thumb", THUMB);
  UIManager.put("ScrollBar.track", BACKGROUND);

  UIManager.put("ComboBox.foreground", FOREGROUND);
  UIManager.put("ComboBox.background", BACKGROUND);
  UIManager.put("ComboBox.selectionForeground", SELECTION_FOREGROUND);
  UIManager.put("ComboBox.selectionBackground", BACKGROUND);
  UIManager.put("ComboBox.buttonDarkShadow", BACKGROUND);
  UIManager.put("ComboBox.buttonBackground", FOREGROUND);
  UIManager.put("ComboBox.buttonHighlight", FOREGROUND);
  UIManager.put("ComboBox.buttonShadow", FOREGROUND);

  JComboBox<String> combo = new JComboBox<String>(makeModel()) {
    private transient MouseListener handler;
    private transient PopupMenuListener listener;
    @Override public void updateUI() {
      removeMouseListener(handler);
      removePopupMenuListener(listener);
      UIManager.put(KEY, new TopRoundedCornerBorder());
      super.updateUI();
      setUI(new BasicComboBoxUI() {
        @Override protected JButton createArrowButton() {
          JButton b = new JButton(new ArrowIcon(BACKGROUND, FOREGROUND));
          b.setContentAreaFilled(false);
          b.setFocusPainted(false);
          b.setBorder(BorderFactory.createEmptyBorder());
          return b;
        }

        @Override protected ComboPopup createPopup() {
          return new BasicComboPopup(comboBox) {
            @Override protected JScrollPane createScroller() {
              JScrollPane sp = new JScrollPane(list) {
                @Override public void updateUI() {
                  super.updateUI();
                  getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
                  getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
                }
              };
              sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
              sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
              sp.setHorizontalScrollBar(null);
              return sp;
            }
          };
        }
      });
      handler = new ComboRolloverHandler();
      addMouseListener(handler);
      listener = new HeavyWeightContainerListener();
      addPopupMenuListener(listener);
      Object o = getAccessibleContext().getAccessibleChild(0);
      if (o instanceof JComponent) {
        JComponent c = (JComponent) o;
        c.setBorder(new BottomRoundedCornerBorder());
        c.setForeground(FOREGROUND);
        c.setBackground(BACKGROUND);
      }
    }
  };

  JPanel p = new JPanel(new GridLayout(0, 1, 15, 15));
  p.setOpaque(true);
  p.add(combo);

  JTree tree = new JTree();
  int row = 0;
  while (row < tree.getRowCount()) {
    tree.expandRow(row);
    row++;
  }

  JScrollPane scroll = new JScrollPane(tree) {
    @Override public void updateUI() {
      super.updateUI();
      getVerticalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
      getHorizontalScrollBar().setUI(new WithoutArrowButtonScrollBarUI());
    }
  };
  scroll.setBackground(tree.getBackground());
  scroll.setBorder(new RoundedCornerBorder());

  setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
  add(scroll);
  add(p, BorderLayout.NORTH);
  setOpaque(true);
  setPreferredSize(new Dimension(320, 240));
}
 
Example 20
/**
 * Adds a {@link PopupMenuListener} that will be registered to the buttons popup menu each time
 * it is shown.
 *
 * @param listener
 *            the listener to add
 */
public void addPopupMenuListener(PopupMenuListener listener) {
	otherListeners.add(listener);
}