public class MenuScroller extends Object
Implementation note: The default number of items to display at a time is 15, and the default scrolling interval is 125 milliseconds.
This class is the work of Darryl Burke and the commenters at this link.
Modifier and Type | Class and Description |
---|---|
private static class |
MenuScroller.MenuIcon |
private class |
MenuScroller.MenuScrollItem |
private class |
MenuScroller.MenuScrollListener |
private class |
MenuScroller.MenuScrollTimer |
Modifier and Type | Field and Description |
---|---|
private int |
bottomFixedCount |
private MenuScroller.MenuScrollItem |
downItem |
private int |
firstIndex |
private int |
interval |
private int |
keepVisibleIndex |
private static org.slf4j.Logger |
logger |
private JPopupMenu |
menu |
private Component[] |
menuItems |
private MenuScroller.MenuScrollListener |
menuListener |
private int |
scrollCount |
private int |
topFixedCount |
private MenuScroller.MenuScrollItem |
upItem |
Constructor and Description |
---|
MenuScroller(JMenu menu)
Constructs a
MenuScroller that scrolls a menu with the
default number of items to display at a time, and default scrolling
interval. |
MenuScroller(JMenu menu,
int scrollCount)
Constructs a
MenuScroller that scrolls a menu with the
specified number of items to display at a time, and default scrolling
interval. |
MenuScroller(JMenu menu,
int scrollCount,
int interval)
Constructs a
MenuScroller that scrolls a menu with the
specified number of items to display at a time, and specified scrolling
interval. |
MenuScroller(JMenu menu,
int scrollCount,
int interval,
int topFixedCount,
int bottomFixedCount)
Constructs a
MenuScroller that scrolls a menu with the
specified number of items to display in the scrolling region, the
specified scrolling interval, and the specified numbers of items fixed at
the top and bottom of the menu. |
MenuScroller(JPopupMenu menu)
Constructs a
MenuScroller that scrolls a popup menu with the
default number of items to display at a time, and default scrolling
interval. |
MenuScroller(JPopupMenu menu,
int scrollCount)
Constructs a
MenuScroller that scrolls a popup menu with the
specified number of items to display at a time, and default scrolling
interval. |
MenuScroller(JPopupMenu menu,
int scrollCount,
int interval)
Constructs a
MenuScroller that scrolls a popup menu with the
specified number of items to display at a time, and specified scrolling
interval. |
MenuScroller(JPopupMenu menu,
int scrollCount,
int interval,
int topFixedCount,
int bottomFixedCount)
Constructs a
MenuScroller that scrolls a popup menu with the
specified number of items to display in the scrolling region, the
specified scrolling interval, and the specified numbers of items fixed at
the top and bottom of the popup menu. |
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Removes this MenuScroller from the associated menu and restores the
default behavior of the menu.
|
void |
finalize()
Ensures that the
dispose method of this MenuScroller is
called when there are no more refrences to it. |
int |
getBottomFixedCount()
Returns the number of items fixed at the bottom of the menu or popup menu.
|
int |
getInterval()
Returns the scroll interval in milliseconds
|
int |
getScrollCount()
Returns the number of items in the scrolling portion of the menu.
|
int |
getTopFixedCount()
Returns the number of items fixed at the top of the menu or popup menu.
|
void |
keepVisible(int index)
Scrolls the item at the specified index into view each time the menu is opened.
|
void |
keepVisible(JMenuItem item)
Scrolls the specified item into view each time the menu is opened.
|
private void |
refreshMenu() |
static int |
scrollCountForScreen(Component c,
Point pt,
JMenuItem item,
int bottomFixedCount)
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.
|
void |
setBottomFixedCount(int bottomFixedCount)
Sets the number of items to fix at the bottom of the menu or popup menu.
|
void |
setInterval(int interval)
Sets the scroll interval in milliseconds
|
void |
setScrollCount(int scrollCount)
Sets the number of items in the scrolling portion of the menu.
|
static MenuScroller |
setScrollerFor(JMenu menu)
Registers a menu to be scrolled with the default number of items to
display at a time and the default scrolling interval.
|
static MenuScroller |
setScrollerFor(JMenu menu,
int scrollCount)
Registers a menu to be scrolled with the default number of items to
display at a time and the specified scrolling interval.
|
static MenuScroller |
setScrollerFor(JMenu menu,
int scrollCount,
int interval)
Registers a menu to be scrolled, with the specified number of items to
display at a time and the specified scrolling interval.
|
static MenuScroller |
setScrollerFor(JMenu menu,
int scrollCount,
int interval,
int topFixedCount,
int bottomFixedCount)
Registers a menu to be scrolled, with the specified number of items
to display in the scrolling region, the specified scrolling interval,
and the specified numbers of items fixed at the top and bottom of the
menu.
|
static MenuScroller |
setScrollerFor(JPopupMenu menu)
Registers a popup menu to be scrolled with the default number of items to
display at a time and the default scrolling interval.
|
static MenuScroller |
setScrollerFor(JPopupMenu menu,
int scrollCount)
Registers a popup menu to be scrolled with the default number of items to
display at a time and the specified scrolling interval.
|
static MenuScroller |
setScrollerFor(JPopupMenu menu,
int scrollCount,
int interval)
Registers a popup menu to be scrolled, with the specified number of items to
display at a time and the specified scrolling interval.
|
static MenuScroller |
setScrollerFor(JPopupMenu menu,
int scrollCount,
int interval,
int topFixedCount,
int bottomFixedCount)
Registers a popup menu to be scrolled, with the specified number of items
to display in the scrolling region, the specified scrolling interval,
and the specified numbers of items fixed at the top and bottom of the
popup menu.
|
void |
setTopFixedCount(int topFixedCount)
Sets the number of items to fix at the top of the menu or popup menu.
|
private static final org.slf4j.Logger logger
private JPopupMenu menu
private MenuScroller.MenuScrollItem upItem
private MenuScroller.MenuScrollItem downItem
private final MenuScroller.MenuScrollListener menuListener
private int scrollCount
private int interval
private int topFixedCount
private int bottomFixedCount
private int firstIndex
private int keepVisibleIndex
public MenuScroller(JMenu menu)
MenuScroller
that scrolls a menu with the
default number of items to display at a time, and default scrolling
interval.menu
- the menupublic MenuScroller(JPopupMenu menu)
MenuScroller
that scrolls a popup menu with the
default number of items to display at a time, and default scrolling
interval.menu
- the popup menupublic MenuScroller(JMenu menu, int scrollCount)
MenuScroller
that scrolls a menu with the
specified number of items to display at a time, and default scrolling
interval.menu
- the menuscrollCount
- the number of items to display at a timeIllegalArgumentException
- if scrollCount is 0 or negativepublic MenuScroller(JPopupMenu menu, int scrollCount)
MenuScroller
that scrolls a popup menu with the
specified number of items to display at a time, and default scrolling
interval.menu
- the popup menuscrollCount
- the number of items to display at a timeIllegalArgumentException
- if scrollCount is 0 or negativepublic MenuScroller(JMenu menu, int scrollCount, int interval)
MenuScroller
that scrolls a menu with the
specified number of items to display at a time, and specified scrolling
interval.menu
- the menuscrollCount
- the number of items to display at a timeinterval
- the scroll interval, in millisecondsIllegalArgumentException
- if scrollCount or interval is 0 or negativepublic MenuScroller(JPopupMenu menu, int scrollCount, int interval)
MenuScroller
that scrolls a popup menu with the
specified number of items to display at a time, and specified scrolling
interval.menu
- the popup menuscrollCount
- the number of items to display at a timeinterval
- the scroll interval, in millisecondsIllegalArgumentException
- if scrollCount or interval is 0 or negativepublic MenuScroller(JMenu menu, int scrollCount, int interval, int topFixedCount, int bottomFixedCount)
MenuScroller
that scrolls a menu with the
specified number of items to display in the scrolling region, the
specified scrolling interval, and the specified numbers of items fixed at
the top and bottom of the menu.menu
- the menuscrollCount
- the number of items to display in the scrolling portioninterval
- the scroll interval, in millisecondstopFixedCount
- the number of items to fix at the top. May be 0bottomFixedCount
- the number of items to fix at the bottom. May be 0IllegalArgumentException
- if scrollCount or interval is 0 or
negative or if topFixedCount or bottomFixedCount is negativepublic MenuScroller(JPopupMenu menu, int scrollCount, int interval, int topFixedCount, int bottomFixedCount)
MenuScroller
that scrolls a popup menu with the
specified number of items to display in the scrolling region, the
specified scrolling interval, and the specified numbers of items fixed at
the top and bottom of the popup menu.menu
- the popup menuscrollCount
- the number of items to display in the scrolling portioninterval
- the scroll interval, in millisecondstopFixedCount
- the number of items to fix at the top. May be 0bottomFixedCount
- the number of items to fix at the bottom. May be 0IllegalArgumentException
- if scrollCount or interval is 0 or
negative or if topFixedCount or bottomFixedCount is negativepublic static int scrollCountForScreen(Component c, Point pt, JMenuItem item, int bottomFixedCount)
c
- Component
on which the point parameter is based.pt
- Point
at which the top of the menu will appear (in component coordinate space).item
- JMenuItem
of prototypical height off of which the average height is determined.bottomFixedCount
- Needed to offset the returned scrollCount.public static MenuScroller setScrollerFor(JMenu menu)
menu
- the menupublic static MenuScroller setScrollerFor(JPopupMenu menu)
menu
- the popup menupublic static MenuScroller setScrollerFor(JMenu menu, int scrollCount)
menu
- the menuscrollCount
- the number of items to display at a timeIllegalArgumentException
- if scrollCount is 0 or negativepublic static MenuScroller setScrollerFor(JPopupMenu menu, int scrollCount)
menu
- the popup menuscrollCount
- the number of items to display at a timeIllegalArgumentException
- if scrollCount is 0 or negativepublic static MenuScroller setScrollerFor(JMenu menu, int scrollCount, int interval)
menu
- the menuscrollCount
- the number of items to be displayed at a timeinterval
- the scroll interval, in millisecondsIllegalArgumentException
- if scrollCount or interval is 0 or negativepublic static MenuScroller setScrollerFor(JPopupMenu menu, int scrollCount, int interval)
menu
- the popup menuscrollCount
- the number of items to be displayed at a timeinterval
- the scroll interval, in millisecondsIllegalArgumentException
- if scrollCount or interval is 0 or negativepublic static MenuScroller setScrollerFor(JMenu menu, int scrollCount, int interval, int topFixedCount, int bottomFixedCount)
menu
- the menuscrollCount
- the number of items to display in the scrolling portioninterval
- the scroll interval, in millisecondstopFixedCount
- the number of items to fix at the top. May be 0.bottomFixedCount
- the number of items to fix at the bottom. May be 0IllegalArgumentException
- if scrollCount or interval is 0 or
negative or if topFixedCount or bottomFixedCount is negativepublic static MenuScroller setScrollerFor(JPopupMenu menu, int scrollCount, int interval, int topFixedCount, int bottomFixedCount)
menu
- the popup menuscrollCount
- the number of items to display in the scrolling portioninterval
- the scroll interval, in millisecondstopFixedCount
- the number of items to fix at the top. May be 0bottomFixedCount
- the number of items to fix at the bottom. May be 0IllegalArgumentException
- if scrollCount or interval is 0 or
negative or if topFixedCount or bottomFixedCount is negativepublic int getInterval()
public void setInterval(int interval)
interval
- the scroll interval in millisecondsIllegalArgumentException
- if interval is 0 or negativepublic int getScrollCount()
public void setScrollCount(int scrollCount)
scrollCount
- the number of items to display at a timeIllegalArgumentException
- if scrollCount is 0 or negativepublic int getTopFixedCount()
public void setTopFixedCount(int topFixedCount)
topFixedCount
- the number of itemspublic int getBottomFixedCount()
public void setBottomFixedCount(int bottomFixedCount)
bottomFixedCount
- the number of itemspublic void keepVisible(JMenuItem item)
null
to restore the default behavior, which is to show the menu as it last
appeared.item
- the item to keep visiblekeepVisible(int)
public void keepVisible(int index)
-1
to restore the default behavior, which is to show the menu as
it last appeared.index
- the index of the item to keep visiblekeepVisible(javax.swing.JMenuItem)
public void dispose()
public void finalize() throws Throwable
dispose
method of this MenuScroller is
called when there are no more refrences to it.private void refreshMenu()