Conquer
Swing deficiencies in MDI development - JavaWorld May 2001
Tutorial Details:
Conquer Swing deficiencies in MDI development
Conquer Swing deficiencies in MDI development
By: By Gerald Nunn
Add more functionality to your Multiple Document Interface (MDI) applications
evelopers have used the Multiple Document Interface (MDI) for many years. It provides an understandable interface for building applications that require multiple documents or windows to be hosted simultaneously within a parent application. In an MDI application, a parent application contains a main application frame that hosts a variety of child frames within it. Users can then move, resize, minimize, and maximize these child windows on the desktop as desired.
Creating an MDI application in Swing is relatively trivial. Swing provides two out-of-box components for creating MDI applications: JDesktopPane and JInternalFrame . You use JDesktopPane as the hosting frame while JInternalFrame s act as the child frames that users move and resize.
On the surface, Swing seems to provide all the desired behavior for building an MDI application. However, creating an MDI application that behaves like users expect is quite challenging. For one reason or another, Swing's designers have omitted what would be considered standard MDI functionality on other platforms. The two most often requested features appear to be as follows:
Making the desktop pane scrollable : As a user positions some or all of an internal frame outside the desktop pane's viewable area, scrollbars will magically appear. This is required so that a user doesn't accidentally "lose" a child frame by positioning it outside the desktop pane's viewable area.
Having a windows menu available : A windows menu that lists all active child frames and lets users easily switch focus between the child frames is needed as well.
Scroll a desktop pane
The first issue we will tackle is allowing the desktop pane to automatically show scrollbars when a child frame is positioned outside its viewable area. We will create this desired behavior by extending the JDesktopPane class into our own MDIDesktopPane . The standard mechanism for adding scroll capability to a component is to first create the component and then add it to a scroll pane's viewport. One of our design goals should be to preserve this methodology of achieving scrolling in our MDIDesktopPane .
We are going to add scrolling to the desktop pane by simply increasing and decreasing the desktop pane's physical size as needed. Increasing the desktop pane's size beyond the size of the scroll pane's viewport will cause scrollbars to appear automatically. Of course, we have to make sure that the desktop pane isn't sized smaller than the scroll pane's viewport.
To accomplish this, we must be able to track the activity of child frames within the desktop pane. When the user finishes moving a child frame, we must determine if the child frame is now out of the desktop pane's bounds, and if so, increase the desktop pane's size so it encompasses the outer edges of the newly moved child frame. In a nutshell, every time the user adjusts the child frame's size or position, we must check the positioning of all child frames and adjust the desktop pane's size to ensure that it encompasses the outermost edges of the child frames.
The question then becomes how do we track the activities of the child frames. One approach might be to add listeners to every child frame that is added to the desktop pane; however, there is a better approach. The JDesktopPane manages user interaction with child frames through a DesktopManager . The DesktopManager is an interface defined by Swing whose methods are called when a frame is resized, closed, moved, maximized, or minimized.
While we can write a DesktopManager from scratch, a more convenient approach is to simply extend the existing DefaultDesktopManager class in Swing and add our code as needed. This results in the class MDIDesktopManager , which is automatically created by the MDIDesktopPane when constructed. The MDIDesktopManager then becomes the default desktop manager through the JDesktopPane 's setDesktopManager() method as shown below.
public MDIDesktopPane() {
manager=new MDIDesktopManager(this);
setDesktopManager(manager);
//...
}
Within MDIDesktopManager we override the methods endResizingFrame() and endDraggingFrame() . Overriding these two methods lets us track child frame behavior. The overridden methods appear below:
public void endResizingFrame(JComponent f) {
super.endResizingFrame(f);
resizeDesktop();
}
public void endDraggingFrame(JComponent f) {
super.endDraggingFrame(f);
resizeDesktop();
}
In each of these methods we first call the super methods and then call a protected method, resizeDesktop() , which is introduced in the MDIDesktopManager class. The resizeDesktop() method determines the outermost point of all child frames and then resizes the desktop accordingly. If the outermost point is actually within the boundaries of the desktop pane's viewable area, it simply resizes the desktop to the scroll pane's viewable area.
We also have to override a few methods within the JDesktopPane to achieve a consistent scrolling behavior. First we override the setBounds() method. This is needed because the scrollable area will change if the desktop pane itself is made smaller or bigger:
public void setBounds(int x, int y, int w, int h) {
super.setBounds(x,y,w,h);
checkDesktopSize();
}
The checkDesktopSize() simply ensures that the desktop pane is visible and has a parent before calling the resizeDesktop() method in MDIDesktopManager . For similar reasons, we override the remove() method. If a child frame is removed, this can again impact the size of the scrollable region, so we need to check it:
public void remove(Component c) {
super.remove(c);
checkDesktopSize();
}
Finally, we add a new overloaded add method that specifically takes a JInternalFrame as a parameter. This is not absolutely necessary, but is done as a convenience mechanism so that newly added frames can be cascaded instead of appearing directly on top of each other. Also, it automatically shows the frame within the desktop pane.
Creating a windows menu
When creating an MDI application, the development of a windows menu tends to be repeated over and over again. This is a special menu to MDI applications that shows a list of open windows and enables the user to focus on a specific one. It also enables the user to access a variety of window management functions, such as cascading or tiling the windows.
The WindowMenu class provides this functionality for a single MDIDesktopPane , which is passed to the WindowMenu in its constructor. The WindowMenu then adds an internal menu listener to itself that enables it to recreate its menu structure as needed for MDIDesktopPane 's current state.
When the user clicks the windows menu, the WindowMenu class automatically adds menu items for cascade and tile, and then adds a separator if any child frames are present on the desktop. Finally, a menu item for each child frame is added, with each menu item displaying the same icon and title as the child frame. Each frame is attached to each corresponding child menu so that when the user clicks a menu, he or she can easily focus on the correct frame.
You can obtain the source code for this article in Resources .
Conclusion
Creating full-featured MDI applications with Swing doesn't mean your users have to give up standard features they have come to expect from other platforms. You can use the classes created in this article in your own applications to provide the scrolling and window menu functionality that users depend on from a MDI application.
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Conquer
Swing deficiencies in MDI development - JavaWorld May 2001
View Tutorial: Conquer
Swing deficiencies in MDI development - JavaWorld May 2001
Related
Tutorials:
|
Displaying 1 - 50 of about 1375 Related Tutorials.
|
Java Swing
for the development of Desktop
application.
Graphics2D
Swing
After...
Java Swing,Java Swing Tutorials,Swing Example Java,Online Swing Tutorials,Swing Example Code Java
Java Swing Tutorials |
Java Swing Tutorials
for the development of Desktop
application.
Graphics2D
Swing
After...
Java Swing,Java Swing Tutorials,Swing Example Java,Online Swing Tutorials,Swing Example Code Java
Java Swing Tutorials |
Java Swing Tutorials
for the development of Desktop
application.
Graphics2D
Swing
After...
Java Swing,Java Swing Tutorials,Swing Example Java,Online Swing Tutorials,Swing Example Code Java
Java Swing Tutorials |
Java Swing Tutorials
for the development of Desktop
application.
Graphics2D
Swing
After...
Java Swing,Java Swing Tutorials,Swing Example Java,Online Swing Tutorials,Swing Example Code Java
Java Swing Tutorials |
Jigloo SWT/Swing GUI Builder
Jigloo SWT/Swing GUI Builder
Jigloo SWT/Swing GUI... and manage both
Swing and SWT GUI classes.
Jigloo creates and manages code for all the parts of
Swing or SWT GUIs as well as code to handle events, and shows |
Swing Jobs at Rose India
Swing Job,Swing Jobs at RoseIndia.net,Java Swing Job
Swing Jobs at Rose India
 ...;
This Swing Job |
What is Java Swing?
What is Java Swing,Definition of Java Swings,Java Swing API
What is Java Swing?
 ... the Java swing. The Java
Swing provides the multiple platform independent APIs |
Development Process
Java: Development Process
Java: Development Process
Software development can be looked at from two... have to master the process of software development,
which is largely self-taught |
Changing Look and Feel of Swing Application
Changing Look and Feel,Change Swing Look and Feel,Java Swing Look and Feel Example
Changing Look and Feel of Swing Application... Swing Application. The look and
feel feature of Java Swing provides more |
Definition of Bioinformatics
;
About Bioinformatics
In February 2001, the human genome was finally... for Biotechnology Information (NCBI
2001) defines bioinformatics as:
"... sub-disciplines within bioinformatics: the
development of new algorithms |
Rich Internet Application Development
Rich Internet Application Development,Rich Internet Applications Development...
Rich Internet Application Development
 ... in downloading the page because the client engine may be prefetching
some |
EasyEclipse Desktop Java
;
EasyEclipse Desktop Java
For development of Desktop GUI applications with Swing
or SWT.
Composition:
This distribution includes... development:
Eclipse
Java Development Tools - Edit, compile, run, debug, test |
Iterative/Incremental Development
Java: Iterative/Incremental Development
Java NotesIterative/Incremental Development
The generally... lump.
It may sound that making these baby steps is only for beginners |
Iterative/Incremental Development
Java: Iterative/Incremental Development
Java NotesIterative/Incremental Development
There are many ways to divide the Software Development Life Cycle.
I've choosen some common terms |
The Development Release of PC-BSD1.0RC2
The Development Release of PC-BSD1.0RC2
The Development Release of PC-BSD1.0RC2
PC-BSD 1.0RC2 Released!
The latest... support, as well as some additional features
/ bugfixes. ISO's may now be downloaded |
J2EE Web Service Development with Attachments Using Axis
Service Development with Attachments Using Axis
Summary
This article discusses the development of a java web... service development.
Introduction
Attachments are becoming increasingly |
J2EE Web Service Development with Attachments Using Axis
Service Development with Attachments Using Axis
Summary
This article discusses the development of a java web... service development.
Introduction
Attachments are becoming increasingly |
Swing Applet Example in java
Java Swing Applet,Java Swing Applets,Swing Applet Tutorials,Swing Applet Example Java
Java - Swing Applet Example in java...;
Introduction
In this section we will show you about using swing |
JSlider Component of Java Swing
Java Slider Example,Java JSlider,Slider Component in Java Swing,JSlider Source Code Java
JSlider Component of Java Swing... of Java Swing. A Slider is a Swing tool which you can use for selecting a range |
Progress Bar in Java Swing
Swing Progress Bar,Java Progress Bar Example,How to Create Progressbar in Java
Progress Bar in Java Swing
 ...;
In this section, you can learn how to handle progress
bar in java swing |
Creating a JTabbedPane Container in Java Swing
Swing
 ... the
JTabbedPane container in Java Swing. The example for illustration is given in which... illustrated in efficient manner.
This program has used various tools of swing |
Creating Check Box in Java Swing
Code Java
Creating Check Box in Java Swing... Swing.
In this section, you can learn simply creating the
Check Box in Java Swing. Check Boxes are created in swing by creating the
instance of the JCheckBox |
Sum of a Number using Swing
Sum of a Number using Swing
Sum of a Number using Swing
 ...
using swing. JTextField, JButton is a component of GUI.
Brief description |
Writing Calculator Program in Swing
Writing Calculator program in Swing
Writing Calculator Program in Swing
 ... an example which illustrates you how to
a create calculator in Swing with the source |
Acceleo
;
Development
Acceleo is an Open Source software based on the Eclipse platform. The generators also are open-source, you may modify them according... done using the forum or the bug reports . If you want to take part in development |
GUI - Swing vs. AWT
Java: GUI - Swing vs. AWT
Java: GUI - Swing vs. AWT
The original graphical user interface (GUI... as Swing. Swing provides replacements for
most of the AWT components, altho many |
Print Screen Using Java Swing
print Screen Using Java Swing
Print Screen Using Java Swing
 ...
swing. The printable that is passed to setPrintable must have a print method |
Programming - Replace method
with another.
The predefined Java function replace may not be used for this --
you... memory overflow.
Iterative development hints
Write the method header. Should |
Java Development Kit (JDK)
Java: Java Development Kit (JDK)t
Java NotesJava Development Kit (JDK)
The most popular Java compiler
is Sun's Software Development Kit (JDK) (aka
the Software Development Kit (SDK |
Haskell development support
Eclipse Plugin-Language
Haskell development support
 ...
We extend the
Eclipse IDE to support development in functional programming |
J2EE Development-zen Platform
J2EE Development-zen Platform
J2EE Development-zen... for the
development of internet, intranet, web service or B2B applications with Java...
development and runtime environment for J2SE and J2EE (Java 2 Platform,
Standard |
Eclipse Plunging/Systems Development
Eclipse Plunging/Systems Development
Eclipse Plunging/Systems Development
 ...;
Wind River Workbench Development Suite |
PDT - PHP Development Tools
PDT - PHP Development Tools
PDT - PHP Development...;
PDT Project
PHP Development Tools
About PDT
The PDT project is working towards providing a PHP
Development Tools framework |
JUnit and Test-Driven Development
Java: JUnit and Test-Driven Development
Java NotesJUnit and Test-Driven Development
Test-Driven Development
One of the big changes in software engineering, and part of the agile |
Lemo Cobol Development Tool
Lemo Cobol Development Tool
Lemo Cobol Development... to assist
Cobol developers in the creation, development of Cobol applications... combination of
enhanced development tools, including: COBOL editor, COBOL compilers |
iPhone Application Development
iPhone Development companies in India, Rose India - iPhone Development Company in India, iPhone development Company India
|
|