Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Swing

Sliding Panel in Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
21 Mar 2013CPOL3 min read 46.5K   2K   8   5
Animated sliding panel in Swing.

Introduction  

The purpose of this article is to create an animated sliding panel to give a cool look in a Swing application. If you are a Windows user, then you must be familiar with this kind of a panel. You will be able to group / ungroup the sliding pane. Fig 1 shows a group sliding pane, at a time one panel can be in expanded mode. And in Fig 2, there are multiple panels in expanded mode. Also you will be able to set which panel can be in expand mode by default. Here we go. 

Image 1    Image 2

                               Fig 1                                                                       Fig 2


Understanding the classes

After extracting the zip file you will get the following classes:

  1. SlideAnimator.java: This class is responsible for doing the animation.
  2. SlideContainer.java: This class hold the panel which need to slide.
  3. TitlePanel.java: Title panel carries the title name along with other info such as icon, toggle button icon etc.
  4. SlidingPanel.java: Encapsulate both the title panel and the sliding panel.
  5. SlidePaneFactory.java: Basically its a box , to add the sliding panel.
  6. StateListener.java: An interface to toggle the state (expand / collapse) of the sliding panel.
  7. BookForm.java: Custom panel which will appear in slide panel.
  8. TestSlidingPanel.java: This is the testing class.  

Background 

Image 3

                                                        Fig 3 

Form Fig 3 you can see how  classes are related to each other. Yellow box represent the panel which need to slide (in our case BookForm.java). The grey box represent the  SlideConatiner panel on which  the SlideAnimator class (responsible for animating) do the  slide transition . The red box is the title panel. TitlePanel will appear on top of each SlideComponent. Blue box wrap up the TitlePanel and SlideContainer. And the last one, the black box,  represent the SlidePaneFactory class which is basically a container to hold the SlidePanel's.

Step by Step  

It's very easy to create Sliding Pane. At first you have to create a SlidePaneFactory instance. There are two ways to create a factory instance.

Java
 SlidePaneFactory.getInstance();
//                or
SlidePaneFactory.getInstance(boolean isGroup);

The default getInstance() is used to create a ungrouped Slide Pane. To create grouped Slide Pane pass the boolean value true. Now SlidingPaneFactory is ready. Each factory represent each Sliding Pane and again each Sliding Pane can contains multiple Sliding panel. To add sliding panel into SlidePane you can use one of the following statement :

C++
SlidePaneFactory.add((JComponent slideComponent);
//                    or
SlidePaneFactory.add((JComponent slideComponent, String title);
//                    or
SlidePaneFactory.add((JComponent slideComponent, String title, Image imageIcon);
//                    or
SlidePaneFactory.add((JComponent slideComponent, String title, Image imageIcon, boolean isExpand);

"slideComponent" is represent the Sliding Panel. The Second parameter is used to set the title of the Sliding Panel. imageIcon is used set the image of the Sliding Panel and the last one is used to set whether the panel is in expand/collapse mode.

Now the final step, add the factory instance in to your app.

Points of Interest

Very easy to create. You can easily convert the grouped slide panel to ungrouped and vice-versa and also can easily set the default state  (expand / collapse)  of each panel.

Thanks To

  • Special thanks to filthy-rich-clients . For animating the panel , I have used the SlideInNotification.java code with little bit customized as per my requirement.
  • Special thanks to Dhilshuk Reddy for gradient panel .

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Make the world open source

Comments and Discussions

 
Questionusing JRadioButton Pin
Member 1260073223-Jun-16 9:39
Member 1260073223-Jun-16 9:39 
AnswerRe: using JRadioButton Pin
Shubhashish_Mandal27-Jun-16 23:55
professionalShubhashish_Mandal27-Jun-16 23:55 
Answerthanks! Pin
Member 1171163122-May-15 5:00
Member 1171163122-May-15 5:00 
SuggestionStatic preferred size issue Pin
Member 115763203-Apr-15 0:05
Member 115763203-Apr-15 0:05 
Hi,

i just tried your sliding panel, and it works quite well.
What i don't like is the fact that the whole component cannot react to
a parent resize action, because it stays on a fixed size from the beginning
by setting the preferred size.

Couldn't this be changed in order that the component can adapt and redraw
with parent resize actions ?

Thank you!

GeneralRe: Static preferred size issue Pin
Shubhashish_Mandal5-May-15 1:20
professionalShubhashish_Mandal5-May-15 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.