This commit is contained in:
tool4EvEr
2021-04-21 20:50:13 +02:00
parent c00a384591
commit 982400b74d

View File

@@ -8,6 +8,8 @@ import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelEvent;
@@ -19,6 +21,7 @@ import javax.swing.JMenu;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.MenuSelectionManager; import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
@@ -46,6 +49,7 @@ public class MenuScroller {
private MenuScrollItem upItem; private MenuScrollItem upItem;
private MenuScrollItem downItem; private MenuScrollItem downItem;
private final MenuScrollListener menuListener = new MenuScrollListener(); private final MenuScrollListener menuListener = new MenuScrollListener();
private final MouseWheelListener mouseWheelListener = new MouseScrollListener();
private int scrollCount; private int scrollCount;
private int interval; private int interval;
private int topFixedCount; private int topFixedCount;
@@ -298,7 +302,7 @@ public class MenuScroller {
this.menu = menu; this.menu = menu;
menu.addPopupMenuListener(menuListener); menu.addPopupMenuListener(menuListener);
menu.addMouseWheelListener(new MouseScrollListener()); menu.addMouseWheelListener(mouseWheelListener);
} }
/** /**
@@ -425,13 +429,14 @@ public class MenuScroller {
public void dispose() { public void dispose() {
if (menu != null) { if (menu != null) {
menu.removePopupMenuListener(menuListener); menu.removePopupMenuListener(menuListener);
menu.removeMouseWheelListener(mouseWheelListener);
menu = null; menu = null;
} }
} }
/** /**
* Ensures that the <code>dispose</code> method of this MenuScroller is * Ensures that the <code>dispose</code> method of this MenuScroller is
* called when there are no more refrences to it. * called when there are no more references to it.
* *
* @exception Throwable if an error occurs. * @exception Throwable if an error occurs.
* @see MenuScroller#dispose() * @see MenuScroller#dispose()
@@ -470,6 +475,12 @@ public class MenuScroller {
menu.add(menuItems[i]); menu.add(menuItems[i]);
} }
int preferredWidth = 0;
for (Component item : menuItems) {
preferredWidth = Math.max(preferredWidth, item.getPreferredSize().width);
}
menu.setPreferredSize(new Dimension(preferredWidth, menu.getPreferredSize().height));
JComponent parent = (JComponent) upItem.getParent(); JComponent parent = (JComponent) upItem.getParent();
parent.revalidate(); parent.revalidate();
parent.repaint(); parent.repaint();
@@ -598,4 +609,26 @@ public class MenuScroller {
mwe.consume(); mwe.consume();
} }
} }
/**
* Calculates the number for scrollCount such that the menu fills the available
* vertical space from the point (mouse press) to the bottom of the screen.
*
* @param c The component on which the point parameter is based
* @param pt The point at which the top of the menu will appear (in component coordinate space)
* @param item A menuitem of prototypical height off of which the average height is determined
* @param bottomFixedCount Needed to offset the returned scrollCount
* @return the scrollCount
*/
public static int scrollCountForScreen(Component c, Point pt, JMenuItem item, int bottomFixedCount) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point ptScreen = new Point(pt);
SwingUtilities.convertPointToScreen(ptScreen, c);
int height = screenSize.height - ptScreen.y;
int miHeight = item.getPreferredSize().height;
int scrollCount = (height / miHeight) - bottomFixedCount - 2; // 2 just takes the menu up a bit from the bottom which looks nicer
return scrollCount;
}
} }