Create a scrollable virtual desktop
in Swing
Tutorial Details:
Create a scrollable virtual desktop in Swing
Create a scrollable virtual desktop in Swing
By: By Tom Tessier
Enhance your Java GUIs with the JScrollableDesktopPane class
he JDesktopPane class, first introduced in JDK 1.2 as a subsidiary to Swing's GUI component series, lets you create a virtual desktop or Multiple Document Interface (MDI) in Java applications. JInternalFrame 's various child windows or internal frames populate this desktop, and because those frames are internal, they are clipped at the boundary of the JDesktopPane container class (as opposed to JFrame 's external frames, which are painted without regard to container boundaries).
This clipping, demonstrated in Figure 1, exemplifies one of JDesktopPane 's inherent limitations: a user cannot view an internal frame's hidden portion without dragging the frame back within the virtual desktop boundary, or resizing the JDesktopPane container itself. Needless to say, such actions are not conducive to navigability and usability.
Figure 1. Internal frame clipped by the virtual desktop. Click on thumbnail to view full-size image.
JDesktopPane 's second limitation is that it doesn't provide a simple method to switch between internal frames; instead, you must click upon the frame title bar. Should internal frames obscure one another, the user must drag each frame aside before the next one becomes accessible. This work becomes tedious if several internal frames overlap, as is possible in any MDI environment.
Introducing JScrollableDesktopPane
The JScrollableDesktopPane class presented in this article offers a solution to the aforementioned clipping and overlap problems, and mimics the interface of the original JDesktopPane class in order to easily upgrade your application. Figure 2 depicts the scrollable desktop pane in action. As Figure 2 shows, JScrollableDesktopPane involves three main subcomponents: a virtual desktop, a toolbar, and a menu.
Figure 2. The JScrollableDesktopPane class in action. Click on thumbnail to view full-size image.
The virtual desktop comprises the main display area. When internal frames are positioned outside the virtual desktop's boundary, scroll bars update to provide access to the cropped internal frames, solving the clipping problem.
A toolbar provides a lengthwise set of toggle buttons above the virtual desktop, with each button mapped to a corresponding internal frame. The toolbar contents automatically size to fit as you add or remove buttons. When you click a button, the associated frame centers upon the virtual desktop and positions atop all other internal frames, solving the accessibility problem.
You may register a menu bar with JScrollableDesktopPane so that your application can offer an alternative solution to the accessibility problem. Upon registration, a Window menu is added to the main application's menu bar. This menu contains Tile , Cascade , Auto , and Close options along with a set of radio buttons that serve as dynamic shortcuts to the internal frames. Tile saturates the desktop with a tiled version of all internal frames, as shown in Figure 3.
Figure 3. Internal frames displayed in Tile mode. Click on thumbnail to view full-size image.
Cascade positions each internal frame in diagonal succession, as shown in Figure 4.
Figure 4. Internal frames displayed in Cascade mode. Click on thumbnail to view full-size image.
Auto allows the user to automatically tile or cascade new internal frames (default is auto-cascade), and Close disposes of the active internal frame.
Implement JScrollableDesktopPane
As you read the implementation details that follow, keep the UML class diagram in Figure 5 handy. You can click on the image for the full implementation details.
Figure 5. UML class diagram of JScrollableDesktopPane at a conceptual level. Click on thumbnail to view the implementation-level UML class diagram.
The JScrollableDesktopPane class is a JPanel subclass built upon five major GUI components: the BaseDesktopPane , DesktopScrollPane , BaseInternalFrame , DesktopResizableToolbar , and DesktopMenu component classes. These GUI components are labeled in Figure 6.
Figure 6. JScrollableDesktopPane with major GUI components labeled. Click on thumbnail to view full-size image.
The BaseDesktopPane and DesktopScrollPane classes comprise the virtual desktop. BaseDesktopPane , a JDesktopPane subclass, is located within a container of DesktopScrollPane , a JScrollPane subclass. The JScrollPane Swing component provides a scrollable view replete with horizontal and vertical scroll bars. When you move or resize an internal frame within the BaseDesktopPane class, a ComponentListener event fires. This event updates the DesktopScrollPane class's scroll bars via manipulation of the BaseDesktopPane preferred size (set via the setPreferredSize() method). The dimensions of this preferred size are determined from the minimum and maximum extents of all internal frames upon the desktop:
Rectangle viewP = getViewport().getViewRect();
int maxX=viewP.width+viewP.x, maxY=viewP.height+viewP.y;
int minX=viewP.x, minY=viewP.y;
JInternalFrame f = null;
JInternalFrame[] frames = getAllFrames();
for (int i=0; i < frames.length; i++) {
f = frames[i];
if (f.getX() < minX) { // get min X
minX = f.getX();
}
if ((f.getX() + f.getWidth()) > maxX) { // get max X
maxX = f.getX() + f.getWidth();
}
if (f.getY() < minY) { // get min Y
minY = f.getY();
}
if ((f.getY() + f.getHeight()) > maxY) { // get max Y
maxY = f.getY() + f.getHeight();
}
}
This technique is similar to that employed by the ScrollDemo2 example found in The Java Tutorial .
Internal frames added to the virtual desktop are of class BaseInternalFrame , a JInternalFrame subclass. The BaseInternalFrame class provides the getter and setter methods necessary to fetch any associated toggle and menu buttons (i.e., get/setAssociatedMenuButton() and get/setAssociatedButton() methods). When you minimize an internal frame, a blank image replaces the icon, as you access minimized frames via the toolbar's toggle buttons and any icon images only clutter the desktop.
The DesktopResizableToolbar class comprises the toolbar in Figure 6. A ResizableToolBar (not depicted in Figures 5 and 6) subclass, DesktopResizableToolbar renders a generic toolbar whose contents automatically size to fit as you add or remove buttons. ResizeableToolBar itself subclasses JToolBar , a Swing component class that provides a container for toolbar buttons. Upon a button's addition or removal (or a toolbar resizing), the ResizeableToolBar class dynamically determines the width of the remaining buttons by dividing the total container width by the button number:
int containerWidth =
getWidth() - getInsets().left - getInsets().right;
int numButtons = getButtonCount();
float buttonWidth = containerWidth / numButtons;
Each toggle button is of class BaseToggleButton , a subclass of Swing's JToggleButton component class. The BaseToggleButton class provides the necessary getter and setter methods to fetch the associated BaseInternalFrame class (i.e., get/setAssociatedFrame() methods).
DesktopMenu , a JMenu subclass, comprises the Window menu in Figure 6. Each dynamic menu shortcut (those numbered sequentially in Figure 6) is of class BaseRadioButtonMenuItem , a subclass of Swing's JRadioButtonMenuItem . Like BaseToggleButton , the BaseRadioButtonMenuItem class provides the necessary getter and setter methods to fetch the associated BaseInternalFrame class (i.e., get/setAssociatedFrame() methods). When an internal frame is destroyed, the DesktopMenu class renumbers the dynamic menu shortcuts to prevent any gaps in their sequence:
Enumeration e = frameRadioButtonMenuItemGroup.getElements();
int displayedCount = 1;
int currentMenuCount = 0;
BaseRadioButtonMenuItem b = null;
while (e.hasMoreElements()) {
b = (BaseRadioButtonMenuItem)e.nextElement();
// compute the key mnemonic based upon the currentMenuCount
currentMenuCount = displayedCount;
// derive the mnemonic from the first digit only..
if (currentMenuCount > 9) {
currentMenuCount/=10;
}
b.setMnemonic(KeyEvent.VK_0 + currentMenuCount);
b.setText(displayedCount +
" " + b.getAssociatedFrame().getTitle());
displayedCount++;
}
Note that if an application does not register a menu bar with the scrollable desktop, then DesktopMenu is not created. As such, DesktopMenu is listed with a 0..1 cardinality in Figure 5's UML diagram.
I should mention some of the package's non-GUI classes. Figure 5's DesktopListener provides a shared event class for various system objects. It implements a ComponentListener interface for the virtual desktop and internal frames, as well as an ActionListener interface for the toggle and menu buttons. Action commands differentiate between the varying source buttons:
public void actionPerformed(ActionEvent e) {
String actionCmd = e.getActionCommand();
if (actionCmd.equals("Tile")) {
desktopMediator.tileInternalFrames();
}
else if (actionCmd.equals("Cascade")) {
desktopMediator.cascadeInternalFrames();
}
...
Figure 5's DesktopMediator class coordinates state changes between other package objects per the Mediator design pattern. It reduces coupling between the coordinated classes (coupling being the number of objects a given class references and depends upon). For more information on these and other classes comprising JScrollableDesktopPane , refer to the source code and javadoc in Resources .
Note that JScrollableDesktopPane requires JDK 1.3 or greater to operate. If you use JDK 1.2, you must supplant the setDragMode() method in the BaseDesktopPane class with a call to the putClientProperty() method (see the comments in the BaseDesktopPane.java source code). Also, you must replace the getButtonCount() method in the ResizableToolBar class with a custom routine.
I tested JScrollableDesktopPane under Java 2 JDK 1.3.1-beta24 on Linux and JDK 1.3_02 on Windows and Intel Solaris.
Using JScrollableDesktop
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Create a scrollable virtual desktop
in Swing
View Tutorial: Create a scrollable virtual desktop
in Swing
Related
Tutorials:
|
Displaying 1 - 50 of about 3307 Related Tutorials.
|
Creating a Scrollable JTable
, you will learn how to
create a scrollable JTable component. When any table has... to create a scrollable
JTable. First of all, this program creates a table having...
Scrollable JTable,Scroll Table Example Java,Scrolling JTable,Creating |
Create a Desktop Pane Container in Java
Desktop Container Java,Create Desktop Pane Container,Example Code for Desktop Pane Container
Create a Desktop Pane Container...;
In this section, you will learn how to create a desktop |
Java Swing
for the development of Desktop
application.
Graphics2D
Swing
After... a Frame
This program shows you how to create a frame in Java Swing... you how to create a Check Box component in Java
Swing.
  |
Java Swing Create LinkButton
Java Swing Create LinkButton
You all are aware of JButtons, JRadioButtons... are going to create a Link
Button that will allow you to move to another page...(ActionEvent e) {
if (Desktop.isDesktopSupported()) {
Desktop desktop |
Java Swing Tutorials
for the development of Desktop
application.
Graphics2D
Swing
After... a Frame
This program shows you how to create a frame in Java Swing... you how to create a Check Box component in Java
Swing.
  |
What is Java Swing?
It displays the desktop icon and create the instance of
JInternalFrame...
What is Java Swing,Definition of Java Swings,Java Swing API
What is Java Swing?
  |
Java Swing Tutorials
for the development of Desktop
application.
Graphics2D
Swing
After... a Frame
This program shows you how to create a frame in Java Swing... you how to create a Check Box component in Java
Swing.
  |
Java Swing Tutorials
for the development of Desktop
application.
Graphics2D
Swing
After... a Frame
This program shows you how to create a frame in Java Swing... you how to create a Check Box component in Java
Swing.
  |
EasyEclipse Desktop Java
EasyEclipse Desktop Java
EasyEclipse Desktop Java...;
EasyEclipse Desktop Java
For development of Desktop GUI applications with Swing
or SWT.
Composition:
This distribution includes |
Virtual Private Servers
Virtual Private Servers,Virtual Private Server Hosting,Virtual Dedicated Server,Hosting,Dedicated Server
Virtual Private
Servers...;
A Virtual Private |
Java Swing
this
you can also create your own look and feel using Swing other than the
ones...
Java Swing Components,Swing GUI Components,Java Components Example - Java Swing Tutorials
Java Swing
  |
User-friendly Desktop Internet GIS (UDIG)
User-friendly Desktop Internet GIS (UDIG)
User-friendly Desktop Internet GIS (UDIG)
 ... Desktop Internet GIS (uDig)
is both a GeoSpatial application and a platform |
JSlider Component of Java Swing
Java Slider Example,Java JSlider,Slider Component in Java Swing,JSlider Source Code Java
JSlider Component of Java Swing...;
In this section, you will learn how to create a JSlider
component |
SplitPane in Java Swing
SplitPane in Java Swing
In this section, you will learn how to create split pane using java swing.
For this, we have used JSplitPane class. Basically this class is used to divide
two components. Here splitpane separates |
Chess Application In Java Swing
Chess Application In Java Swing
Chess Application In Java Swing
 ...;
In this section, you will learn how to create chess |
Creating a JTabbedPane Container in Java Swing
Swing
 ...;
In this section, you will learn how to create the
JTabbedPane container in Java Swing. The example for illustration is given in which |
Java Virtual Machine(JVM)
Java Virtual Machine,JVM Free Tutorials,Sun Java Virtual Machine Definition
Java Virtual Machine(JVM)
 ... is Java Virtual Machine?
Ans:- Java Virtual Machine
JVM is the main component |
Creating Check Box in Java Swing
Java Check Boxes,Checkbox Example,Create Check Box in Java,Checkbox Program Code Java
Creating Check Box in Java Swing...;
This section illustrates you how to create a Check Box
component in Java |
Java Swing Open Browser
Java Swing Open Browser
Java provides different methods to develop useful applications. In this section, you will learn how to create an example that will open the specified url on the browser using java swing. This example will really |
Writing Calculator Program in Swing
an example which illustrates you how to
a create calculator in Swing with the source...
Writing Calculator program in Swing
Writing Calculator Program in Swing
  |
SQL Create View
SQL Create View
SQL Create View
 ...;
A View is a imaginary and virtual table in SQL. In SQL, a view is
defined as a virtual table outcome as a result of an SQL statement. A view
include rows |
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 |
AutoSuggestion Box in Java Swing
will learn how to create autocomplete box in java swing.
For this, we have created...
AutoSuggestion Box in Java Swing
AutoSuggestion Box in Java Swing
  |
Create a JList Component in Java
, you will learn how to create
a JList component of swing. JList...
Jlist Java,List Box Example,Creating List Box Using Java Swing
Create a JList Component in Java
  |
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 |
Create a JSpinner Component in Java
;
In this section, you will learn how to create a
JSpinner component of swing...
Java Spinner,Create JSpinner Component in Java,Jspinner Example in Java
Create a JSpinner Component in Java
  |
Create a JRadioButton Component in Java
Swing
Create a JRadioButton Component in Java...;
In this section, you will learn how to create a radio
button in java swing. Radio Button is like check box. Differences between check
box and radio |
Create a Scroll Pane Container in Java
;
In this section, you will learn how to create a scroll
pane container in Java Swing. When you simply create a Text Area and putting
text... Example Java
Create a Scroll Pane Container in Java |
Virtual Hosting
The virtual web hosting usually abbreviated as vhost, simply refers... I.P. (Internet Protocol) address.
Virtual hosting is provided by almost all web hosting companies that involve dividing the server into a number of virtual |
Create a ToolBar in Java
;
In this section, you will learn how to create toolbar in java. Swing...
JToolbar Example,Java Toolbar,Create Toolbar Icons Java,How to Create Tool Bar in Java
Create a ToolBar in Java |
Why Virtual Dedicated Hosting?
Virtual Dedicated Hosting,Virtual Dedicated Server,Virtual Dedicated,Servers,Host
Why Virtual Dedicated Hosting...;
These days Virtual Dedicated |
Java Virtual Machine
Java: Java Virtual Machine
Java: Java Virtual Machine
After you read this section, you should be able... different machines.
The key to Java's portability and security is the
Java Virtual |
Create a Popup Menus with Nested Menus in Java
will learn how to create a nested popup
menu in Java Swing. When you click...
Create a Popup Menus with Nested Menus in Java
Create a Popup Menus with Nested Menus in Java
  |
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 |
Java Swing Scientific Calculator
Java Swing Scientific Calculator
A Scientific Calculator is a very powerful... mathematical functions is needed. Here we are going to create a Scientific calculator using java swing.
Here is the code:
import java.awt.*;
import |
Create a JComboBox Component in Java
Combobox Example,Create Combo Box,Java Combo Box Example,Create a JComboBox Component in Java
Create a JComboBox Component... Component
of swing in java. The
JComboBox is used to display drop-down list |
Create menus and submenus in Java
Swing Menu,Create Menus and Submenus,Java Menu Example Using Swing
Create menus and submenus in Java
 ... in Java Swing. Menu bar contains a
collection of menus. Each menu can have multiple |
Java Virtual Machine
Java Virtual Machine
Java Virtual Machine...;
In this section, you will learn about the java virtual
machine (JVM) and how to work?
Java Virtual Machine (JVM)
Java Virtual Machine is a set |
Create Layout Components in a Grid in Java
Create Layout Components in a Grid,How to Create Layout in Grid Using Java Swing
Create Layout Components in a Grid in Java...;
In this section, you will learn how to create layout components |
Scrollpane in Java Swing
Scrollpane in Java Swing
Scrollpane in Java Swing
 ...;
Use Canvas with ScrollPane in swing.
In this section |
Login Form in Swing
Login Form in Swing
Login Form in Swing...;
This section illustrates you how to create a Login form.
To create a Login...
In the LoginDemo.java, we have create two text fields text1 and text2 to set
the text |
Java Virtual Machine Free Download
Java Virtual Machine Free Download
Java Virtual...;
The Java Virtual Machine is required to run your java program in your
computer. The Java Virtual Machine software is freely |
Java program to get the desktop Path
Java program to get the desktop path
Java program to get the desktop
Path
 ... have to get the desktop path
of the system. In the java environment we can get |
Splash Screen in Java Swing
to create a swing splash screen. The splash screen will get disappeared on its own after |
TextField
Text Field in Java,Create Text Field in Java,Textfield Program in Java
TextField
 ...;
A scrollable text display object |
Google Desktop Search
Eclipse Plugin-Language
Google Desktop Search... search
capabilities, this plug-in uses the Google Desktop Search Engine instead.
Usage
The search can be started from either the Search menu or the Google Desktop |
"Hello World" program in Swing and JRuby
to use Swing in JRuby
program.
In this example we will create our...
"Hello World" program in Swing and JRuby
"Hello World" program in Swing and JRuby
  |
create Multiple Frames
Multiple Frames
In this section, you will learn how to create multiple frames.Java Swing provides the utility to show frame within another frame by providing the class JInternalFrame. With the use of this class, you can display a JFrame |
Create Sine Wave
;
In this section, you will learn how to create a Sine Wave using Java Swing.
We have...
Create Sine Wave
Create Sine Wave
  |
Create JTree using an Object
Create Jtree in Java,Creating Tree Using an Object,How to Create Tree Using Object in Java Swing
Create JTree using an Object...;
In this section you
will learn to create a JTree using |
|
|
|