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.

JFrame with MDIDesktopPane preview
JFrame with MDIDesktopPane preview

In Netbeans, these were I did:

  1. I extracted the sample code (library) into the same package with my gui package.
  2. Using gui builder tools provided by Netbeans, I created JFrame and inside it, I added JMenuBar, JItem, and JMenuItem as necessary.
  3. Using gui builder, Netbeans will generate function called initComponents. This function lists all of the variable declarations and gui setups for components added before.
  4. In the JFrame constructor, the initComponents function will be called. In this area, we will added the MDIDesktopPane init.
  5. Before that, I created MDIDesktopPane declaration. It will not far from this:
    private MDIDesktopPane desktop = new MDIDesktopPane();
    private JScrollPane scrollPane = new JScrollPane();

    We need JScrollPane because it will support the scrolling functions.

  6. Then, after the initComponents function, I added the MDIDesktopPane and the JScrollPane to the frame.
    jMenuBar1.add(new WindowMenu(desktop));
    getContentPane().setLayout(new BorderLayout());

    FYI, jMenuBar1 was the variable name of my JMenuBar. It means I added WindowMenu of the MDIDesktop to the menu bar. WindowMenu was a class to control the display of all of the child menu like cascaded or tiled windows.

  7. 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.

10 thoughts on “Creating Scrollable JDesktopPane on Netbeans

  1. dude, I respect your knowledge, and its great that you went deep into internet and did some work..

    But there is an "Easier" way of doing this in netbeans..

    If all you want is a scrollable JDesktopPane in netbeans, here goes how to :

    1. create a jscrollpane using gui builder.

    2. insert your desktop pane into that scrollpane..

    3. clikc on the desktop pane and set the properties – "maximum size", "minimum size", "preferred size" as same.. like if max size you want to set is 1920×1080, then set min size and preferred size also as 1920,1080.

    There you are.. immediatly the scrollbar appears and your desktop pane is scrollable..

    you can then insert any component to desktop pane.. it holds good..

    if you have any internal frames going out , then just increase the preferred size and max size, you'll be good..

    you can play with those properties and find out more things..

    if anything is wrong in my post, please correct me..

  2. dear Shrinath..

    thanks for your comment..

    I'm aware of this "Easier" way few moments after I wrote this post. As a matter of fact, I used it too in my <code>JInternalFrame</code>. One of the reasons I still use the way above is that the library has <code>WindowMenu</code> class that can control the display of all child windows (cascaded or tiled).

    I tried your way after I read your comment. I tried to add <code>JInternalFrame</code> into the <code>JDesktopPane</code>. But the scrolling bar didn't auto expand when I drag the <code>JInternalFrame</code> outside the <code>JDesktopPane</code>. Did it work on you? Would you mind share it with me?

    Thank you before..

    1. The trick to making the "easier" way work is to override reshape() in JInternalFrame. In your subclass, get the parent (be sure to do a null check!), then you can resize the parent if needed by changing both its preferredSize and its size. I did this and it works. However, resizing the parent from within the child document breaks the natural component hierarchy in a way I don't like. I'd therefore already planned to subclass JDesktopPane in my next version.

      So I really appreciate your post, and I think I will try using MDIDesktopPane instead because I'm also not happy with the Default iconization in Swing and was considering writing my own code for that.

  3. Thanks for the info! It's what I was looking for a time…And the "easier" way to do it, for me it didn't work like a wanted, sometimes it's better if we do our own classes. :ngayal:

  4. hay bro nice for see your blog :wow: .actually wanna ask bagaman jika jinternlframe kta geser ke kiri apakah scrollnya ikut ever get a class that give automatic scrollable but if jintenalframe touch the right or down but no scrolol if we toucing the jinternal frame in left or left down thanks for answer :) sukses my bro :hi:

  5. Pingback: billigt bredbaand

What's in your mind?