Creating Scrollable JDesktopPane on Netbeans
Hi there..
Long time ago, I got some challenge to create some MDI (Multiple Document Interface) application. Why I called it challenge? Because I must create it with Java and I didn’t have experience on using Swing. But thanks to Netbeans, it makes Swing gui development far easier.
Netbeans provided some tools for gui building. You just need to drag-and-drop, edit some properties, and adding some event-handling. Voila, your desktop application will be up and running. But there is a but. In MDI application, we’ll need a scrollable desktop pane. For your information, desktop pane is a home for all of child windows. If I dragged the child window outside the desktop pane, the desktop pane should automatically give scroll to facilitate it. Java have library to implement dekstop pane. It’s called JDesktopPane. But the usual JDesktopPane didn’t have support for auto-scrolling.
Gerald Nunn in Java World site write a way to conquer this deficiencies. He create some libraries that we can use to enhance the usual JDesktopPane. He called the object MDIDesktopPane. All we have to do are just download it, link it to our codes, and use it.
In Netbeans, these were I did:
- I extracted the sample code (library) into the same package with my gui package.
- Using gui builder tools provided by Netbeans, I created
JFrameand inside it, I addedJMenuBar,JItem, andJMenuItemas necessary. - Using gui builder, Netbeans will generate function called
initComponents. This function lists all of the variable declarations and gui setups for components added before. - In the
JFrameconstructor, theinitComponentsfunction will be called. In this area, we will added theMDIDesktopPaneinit. - Before that, I created
MDIDesktopPanedeclaration. It will not far from this:private MDIDesktopPane desktop = new MDIDesktopPane(); private JScrollPane scrollPane = new JScrollPane();
We need
JScrollPanebecause it will support the scrolling functions. - Then, after the
initComponentsfunction, I added theMDIDesktopPaneand theJScrollPaneto the frame.jMenuBar1.add(new WindowMenu(desktop)); scrollPane.getViewport().add(desktop); getContentPane().setLayout(new BorderLayout()); getContentPane().add(scrollPane,BorderLayout.CENTER);
FYI,
jMenuBar1was the variable name of myJMenuBar. It means I addedWindowMenuof theMDIDesktopto the menu bar.WindowMenuwas a class to control the display of all of the child menu like cascaded or tiled windows. - It’s done. It’s simple, right.
With this MDIDesktopPane and WindowMenu class, I finally conquer the scrollable desktop pane. Thanks to Gerald Nunn for his great class.
