public PopupMenuButton( Application app, Object[] data, Integer rows, Integer columns, Dimension iconSize, Integer mode, final boolean hasTable, boolean hasSlider) { super(); this.app = app; this.hasTable = hasTable; this.mode = mode; this.iconSize = iconSize; this.thisButton = this; this.setFocusable(false); // create the popup myPopup = new JPopupMenu(); myPopup.setFocusable(false); myPopup.setBackground(Color.WHITE); myPopup.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.GRAY), BorderFactory.createEmptyBorder(3, 3, 3, 3))); // add a mouse listener to our button that triggers the popup addMouseListener( new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { popupIsVisible = myPopup.isShowing(); } @Override public void mousePressed(MouseEvent e) { if (!thisButton.isEnabled()) return; if (popupIsVisible == true && !myPopup.isVisible()) { popupIsVisible = false; return; } if (prepareToShowPopup() == false) return; Point locButton = getLocation(); // trigger popup // default: trigger only when the mouse is over the right side of the button // if isStandardButton: pressing anywhere triggers the popup if (isStandardButton || e.getX() >= getWidth() - 16 && e.getX() <= getWidth()) { if (hasTable) myTable.updateFonts(); if (isDownwardPopup) // popup appears below the button myPopup.show(getParent(), locButton.x, locButton.y + getHeight()); else // popup appears above the button myPopup.show( getParent(), locButton.x - myPopup.getPreferredSize().width + thisButton.getWidth(), locButton.y - myPopup.getPreferredSize().height - 2); } popupIsVisible = myPopup.isShowing(); } }); // place text to the left of drop down icon this.setHorizontalTextPosition(SwingConstants.LEFT); this.setHorizontalAlignment(SwingConstants.LEFT); // create selection table if (hasTable) { this.data = data; myTable = new SelectionTable(app, data, rows, columns, iconSize, mode); setSelectedIndex(0); // add a mouse listener to handle table selection myTable.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { handlePopupActionEvent(); } }); /* // if displaying text only, then adjust the width if(mode == SelectionTable.MODE_TEXT){ Dimension d = this.getPreferredSize(); d.width = myTable.getColumnWidth(); setMinimumSize(d); setMaximumSize(d); } */ myTable.setBackground(myPopup.getBackground()); myPopup.add(myTable); } // create slider if (hasSlider) getMySlider(); isIniting = false; if (mode == SelectionTable.MODE_TEXT && iconSize.width == -1) { iconSize.width = myTable.getColumnWidth() - 4; iconSize.height = myTable.getRowHeight() - 4; } }
private void constructPopup() { popup = new JPopupMenu(); popup.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); popup.setBackground(new Color(100, 100, 100)); JMenuItem popupItemSaveImage = new JMenuItem("Save image"); popupItemSaveImage.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { display.saveButtonPressed(); } }); popup.add(popupItemSaveImage); JMenuItem popupItemSaveNewick = new JMenuItem("Save newick"); popupItemSaveNewick.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { display.saveNewickButtonPressed(); } }); popup.add(popupItemSaveNewick); JMenuItem popupItemRemove = new JMenuItem("Remove tree"); popupItemRemove.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { display.removeButtonPressed(); } }); popup.add(popupItemRemove); JMenu orientMenu = new JMenu("Orientation"); popup.add(orientMenu); ButtonGroup bg = new ButtonGroup(); OrientationListener oListen = new OrientationListener(); JRadioButtonMenuItem oRight = new JRadioButtonMenuItem("Right"); oRight.addActionListener(oListen); oRight.setSelected(true); orientMenu.add(oRight); oRight.setActionCommand("Right"); bg.add(oRight); JRadioButtonMenuItem oLeft = new JRadioButtonMenuItem("Left"); oLeft.addActionListener(oListen); orientMenu.add(oLeft); oLeft.setActionCommand("Left"); bg.add(oLeft); JRadioButtonMenuItem oUp = new JRadioButtonMenuItem("Up"); oUp.addActionListener(oListen); oUp.setActionCommand("Up"); orientMenu.add(oUp); bg.add(oUp); JRadioButtonMenuItem oDown = new JRadioButtonMenuItem("Down"); oDown.addActionListener(oListen); oDown.setActionCommand("Down"); orientMenu.add(oDown); bg.add(oDown); PopupListener popListener = new PopupListener(); this.addMouseListener(popListener); }
/** * Creates a ClickableTooltip with custom show/hide delays * * @param text The tooltip text * @param action The action which should be provided to the user * @param startupDelay Delay in ms before the tooltip should be shown * @param showTime Delay in ms after which the tooltip vanishes when the cursor moved away from * the component or the tooltip area */ public ClickableTooltip( final String text, final Action action, final int startupDelay, final int showTime) { Color c = UIUtilities.TOOLTIP_COLOR; Font f = (FontUIResource) UIManager.get("ToolTip.font"); popupMenu = new JPopupMenu(); popupMenu.setLayout(new BoxLayout(popupMenu, BoxLayout.PAGE_AXIS)); popupMenu.setBorder(BorderFactory.createEmptyBorder(INSETS, INSETS, INSETS, INSETS)); popupMenu.setBackground(c); popupMenu.setFont(f); if (text != null) { JLabel desc = new JLabel(text); desc.setBackground(c); desc.setFont(f); popupMenu.add(desc); } final JLabel label = new JLabel(); label.setForeground(LINK_COLOR); label.setBackground(c); label.setFont(f); label.setText((String) action.getValue(Action.NAME)); label.addMouseListener( new MouseAdapter() { Cursor defaultCursor = null; public void mouseReleased(MouseEvent e) { action.actionPerformed( new ActionEvent(this, -1, (String) action.getValue(Action.ACTION_COMMAND_KEY))); // if the user clicks on the 'link' remove tooltip popupMenu.setVisible(false); } @Override public void mouseEntered(MouseEvent e) { defaultCursor = label.getCursor(); label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); hideTimer.stop(); } @Override public void mouseExited(MouseEvent e) { if (defaultCursor != null) { label.setCursor(defaultCursor); } hideTimer.restart(); } }); popupMenu.add(label); showTimer = new Timer( startupDelay, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { popupMenu.show(component, location.x, location.y); showTimer.stop(); } }); hideTimer = new Timer( startupDelay + showTime, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { popupMenu.setVisible(false); hideTimer.stop(); } }); }