r22793 - FScrollPanel and FAbsolutePositioner now available
Post MTG Forge Related Programming Questions Here
Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins
2 posts
• Page 1 of 1
r22793 - FScrollPanel and FAbsolutePositioner now available
by drdev » 05 Aug 2013, 08:48
As part of my recent work to clean up the home screen, I was asked to add scrolling support to the menu. This turned out to be a much bigger project than I anticipated (mostly because showing a vertical scrollbar looked terrible), however it proved interesting and helped me really delve into the trenches of Java GUI development.
What I came up with in the end was a solution that involves using arrow buttons that appear over the top or bottom of the menu as long as there's something to scroll to in that direction, with the ability to continuously click or click and hold that button to scroll (the equivalent of clicking the arrow button at the ends of a normal scrollbar, with the main difference being this new arrow button is hidden when you can't scroll anymore). Mouse wheel scrolling works the same as with scrollbars.
Rather than hack something together to just work for the home screen menu, I created a new FScrollPanel class that works as a JPanel with built in scrolling support. It can be configured to use regular scrollbars or the new arrow buttons for horizontal and/or vertical scrolling. It extends JScrollPane, so you can access all the methods in that class, however you can call scrollPanel.add to add components to an inner JPanel managed within it as if you were working with any other JPanel.
The trickiest part of all this was figuring out a way to render the arrow buttons on top of the scroll panel without interfering with the flow of components in or around the panel. As I do mostly web UI development for a living, I am very familiar with the concept of utilizing elements with position: absolute inside elements with position: relative to accomplish a similar task. After some digging, I realized we can support "absolute" positioning of this sort by adding components to a transparent panel that lives in the frame's layered pane (directly above the main content panel), then utilizing screen location comparisons to position that component such that it appears anywhere we want without messing with the flow of anything else. To encapsulate all this, I created a utility class called FAbsolutePositoner which exposes a few simple "show" and "hide" methods for absolutely positioning components.
With all that said, here's a peek at what an FScrollPanel looks like on the Home Screen at 800x600 window size. Previously, most of the menu would be inaccessible without collapsing every group.
As an example for FScrollPanel, the code that creates the scroll panel for Home screen menu looks like this:
As an example for FAbsolutePositioner, FScrollPanel uses it to absolutely position the arrow buttons on top of it like this:
Hopefully these two new tools will prove useful for future development. Let me know if you have any questions or input. I'm particularly curious what people think of the scroll speed when holding down the arrow buttons, as I'm debating about increasing it.
Thanks.
-Dan
What I came up with in the end was a solution that involves using arrow buttons that appear over the top or bottom of the menu as long as there's something to scroll to in that direction, with the ability to continuously click or click and hold that button to scroll (the equivalent of clicking the arrow button at the ends of a normal scrollbar, with the main difference being this new arrow button is hidden when you can't scroll anymore). Mouse wheel scrolling works the same as with scrollbars.
Rather than hack something together to just work for the home screen menu, I created a new FScrollPanel class that works as a JPanel with built in scrolling support. It can be configured to use regular scrollbars or the new arrow buttons for horizontal and/or vertical scrolling. It extends JScrollPane, so you can access all the methods in that class, however you can call scrollPanel.add to add components to an inner JPanel managed within it as if you were working with any other JPanel.
The trickiest part of all this was figuring out a way to render the arrow buttons on top of the scroll panel without interfering with the flow of components in or around the panel. As I do mostly web UI development for a living, I am very familiar with the concept of utilizing elements with position: absolute inside elements with position: relative to accomplish a similar task. After some digging, I realized we can support "absolute" positioning of this sort by adding components to a transparent panel that lives in the frame's layered pane (directly above the main content panel), then utilizing screen location comparisons to position that component such that it appears anywhere we want without messing with the flow of anything else. To encapsulate all this, I created a utility class called FAbsolutePositoner which exposes a few simple "show" and "hide" methods for absolutely positioning components.
With all that said, here's a peek at what an FScrollPanel looks like on the Home Screen at 800x600 window size. Previously, most of the menu would be inaccessible without collapsing every group.
As an example for FScrollPanel, the code that creates the scroll panel for Home screen menu looks like this:
- Code: Select all
pnlSubmenus = new FScrollPanel(new MigLayout("insets 0, gap 0, wrap, hidemode 3"), true,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
...
pnlSubmenus.add(allGroupLabels.get(e), "w 100%!, h 30px!, gap 0 0 3px 3px");
pnlSubmenus.add(allGroupPanels.get(e), "w 100%!, gap 0 0 0 0");
...
pnlMenu.add(pnlSubmenus, "w 100%!, h 100% - " + pnlMainMenuHeight + "px!");
As an example for FAbsolutePositioner, FScrollPanel uses it to absolutely position the arrow buttons on top of it like this:
- Code: Select all
FAbsolutePositioner.SINGLETON_INSTANCE.show(arrowButton, this, x, y);
Hopefully these two new tools will prove useful for future development. Let me know if you have any questions or input. I'm particularly curious what people think of the scroll speed when holding down the arrow buttons, as I'm debating about increasing it.
Thanks.
-Dan
Last edited by drdev on 05 Aug 2013, 22:45, edited 6 times in total.
- drdev
- Programmer
- Posts: 1958
- Joined: 27 Jul 2013, 02:07
- Has thanked: 189 times
- Been thanked: 565 times
Re: r22793 - FScrollPanel and FAbsolutePositioner now availa
by Sloth » 05 Aug 2013, 09:08
It's a good solution. Thanks a lot Dan.
-
Sloth - Programmer
- Posts: 3498
- Joined: 23 Jun 2009, 19:40
- Has thanked: 125 times
- Been thanked: 507 times
2 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 60 guests