Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 16. (Advanced topics) Desktops and Internal Frames. Easy for reading, Click here!

Custom Search
Swing Chapter 16. (Advanced topics) Desktops and Internal Frames. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 1/5 



Next Page (2/5) Next Page

Chapter 16. Desktops and Internal Frames

Subpages: 1. JDesktopPane and JInternalFrame 
2.
Internalizable/Externalizable frames 
3.
Cascading and outline dragging mode 
4. An X windows style desktop environment 
5. A networked multi-user desktop using sockets 

In this Chapter:

  •    JDesktopPane and JInternalFrame
  •    Internalizable/Externalizable frames
  •    Cascading and outline dragging mode
  •    An X windows style desktop environment
  •    A networked multi-user desktop using sockets

16.1 JDesktopPane and JInternalFrame

16.1.1  JDesktopPane

class javax.swing.JDesktopPane

JDesktopPane is a powerful extension of JLayeredPane built specifically to manage JInternalFrame children. This is Swing's version of a multiple document interface, a feature common to most modern operating system desktops. In the last chapter we created our own MDI from scratch. Both our MDI and the JDesktopPane/JInternalFrame prebuilt MDI are quite powerful. This chapter focuses mostly on the latter, but we will relate the discussion of it to our own often.

16.1.2  JInternalFrame

class javax.swing.JInternalFrame

The purpose of JDesktopPane is to provide a specialized container for JInternalFrames. We can access its contents identically to JLayeredPane. There are several additional convenience methods defined in JDesktopPane for accessing JInternalFrame children (see API docs) and attaching a DesktopManager implementation (see below).

JInternalFrames are very similar to our custom InnerFrames of chapter 15. They can be dragged, resized, iconified, maximized, and closed. JInternalFrame contains a JRootPane as its main container and implements the RootPaneContainer interface. We can access a JInternalFrame's rootPane and its associated glassPane, contentPane, layeredPane, and menuBar the same way we access them in JFrame and in our InnerFrame.

16.1.3  JInternalFrame.JDesktopIcon

class javax.swing.JInternalFrame.JDesktopIcon

This represents a JInternalFrame in its iconified state. We are warned against using this class as it will disappear in future versions of Swing: "This API should NOT BE USED by Swing applications, as it will go away in future versions of Swing as its functionality is moved into JInternalFrame."API Currently when a JInternalFrame is iconified it is removed from its JDesktopPane and a JDesktopIcon instance is added to represent it. In future versions of Swing JInternalFrame will have JDesktopIcon functionality built into it.

16.1.4 DefaultDesktopManager

class javax.swing.DefaultDesktopManager

This is the concrete default implementation of the DesktopManager interface. An instance of this class is attached to each JDesktopPane if a custom DesktopManager implementation is not specified.

16.1.5  The DesktopManager interface

abstract interface javax.swing.DesktopManager

Each JDesktopPane has a DesktopManager object attached to it whose job it is to manage all operations performed on JInternalFrames within the desktop. DesktopManager methods are automatically called from the associated JDesktopPane when an action is invoked on a JInternalFrame within that desktop. These are usually invoked when the user performs some action on a JInternalFrame with the mouse:

activateFrame(JInternalFrame f)

beginDraggingFrame(JComponent f)

beginResizingFrame(JComponent f, int direction)

closeFrame(JInternalFrame f)

deactivateFrame(JInternalFrame f)

deiconifyFrame(JInternalFrame f)

dragFrame(JComponent f, int newX, int newY)

endDraggingFrame(JComponent f)

endResizingFrame(JComponent f)

iconifyFrame(JInternalFrame f)

maximizeFrame(JInternalFrame f)

minimizeFrame(JInternalFrame f)

openFrame(JIntenerlFrame f)

resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight)

setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight)

Note that if we want to manually invoke say, iconification, on a JInternalFrame we should do the following:

    myJInternalFrame.getDesktopPane().getDesktopManager().

      iconifyFrame(myJInternalFrame);

We could also directly call setIcon(true) on a JInternalFrame, but we are discouraged from doing so because it is not good practice to bypass the DesktopManager. The reason this is not good practice is that there may be necessary actions defined within the DesktopManager's iconifyFrame() method that would not be invoked. So, in general, all calls to methods of JInternalFrame that have DesktopManager counterparts should be delegated to the DesktopManager.

We have written an animated demo that shows when and how often each DesktopManager method is called. See \Chapter16\4, and execute the DesktopManagerDemo class. Figure 16.1 illustrates.

Figure 16.1   DesktopManager animated demo.

<<file figure16-1.gif>>

16.1.6  The WindowConstnats interface

abstract interface javax.swing.WindowConstants

Refer to chapter 3 for a description of this interface.

Bug Alert: using DO_NOTHING_ON_CLOSE with setDefaultCloseOperation() on a JInternalFrame does not work as expected. See bug #4176136 at the Java Developer Connection Bug Parade: http://developer.javasoft.com/developer/bugParade/bugs/4176136.html. This will most likely be fixed in the next release of Java 2.

To capture the closing of a JInternalFrame and display a confirmation dialog we can construct the following JInternalFrame subclass:

 class ConfirmJInternalFrame extends JInternalFrame

   implements VetoableChangeListener {

    public ConfirmJInternalFrame(String title, boolean resizable,

     boolean closable, boolean maximizable, boolean iconifiable) {

      super(title, resizable, closable, maximizable, iconifiable);

      addVetoableChangeListener(this);

    }

    public void vetoableChange(PropertyChangeEvent pce)

     throws PropertyVetoException {     

      if (pce.getPropertyName().equals(IS_CLOSED_PROPERTY)) {

        boolean changed = ((Boolean) pce.getNewValue()).booleanValue();

        if (changed) {

          int confirm = JOptionPane.showOptionDialog(this,

            "Close " + getTitle() + "?",

            "Close Confirmation",

            JOptionPane.YES_NO_OPTION,

            JOptionPane.QUESTION_MESSAGE,

            null, null, null);

          if (confirm == 0) {

            m_desktop.remove(this);

            m_desktop.repaint();

          }

          else throw new PropertyVetoException("Cancelled",null);

        }

      }

    }

  }

Using this class in place of JInternalFrame will always display a confirmation dialog when the close button is pressed. This code checks to see if the closed property has changed from its previous state. This is a constrained property which we can veto if desired (see chapter 2). Luckily this comes in real handy for working around the DO_NOTHING_ON_CLOSE bug.

If the confirmation dialog is displayed and then cancelled (i.e.either the "NO"button or the close dialog button is pressed) a PropertyVetoException is thrown which vetos the property change and the internal frame will not be closed. Figure 16.2 illustrates.

Figure 16.2   Handling internal frame closing with a close confirmation dialog.

<<file figure16-2.gif>>

16.1.7  The InternalFrameListener interface

abstract interface  javax.swing.event.InternalFrameListener

Each JInternalFrame can  have one or more InternalFrameListeners attached. An InternalFrameListener will receive InternalFrameEvents allowing us to capture and handle them however we like with the following methods:

internalFrameActivated(InternalFrameEvent e)

internalFrameClosed(InternalFrameEvent e)

internalFrameClosing(InternalFrameEvent e)

internalFrameDeactivated(InternalFrameEvent e)

internalFrameDeiconified(InternalFrameEvent e)

internalFrameIconified(InternalFrameEvent e)

internalFrameOpened(InternalFrameEvent e)

InternalFrameListener and DesktopManager both exist to process changes in a JInternalFrame's state. However, they can both be used to achieve different ends. DesktopManager allows us to define internal frame handling methods for all JInternalFrames within a given JDesktopPane, whereas InternalFrameListener allows us to define InternalFrameEvent handling unique to each individual JInternalFrame. We can attach a different InternalFrameListener implementation to each instance of JInternalFrame, whereas only one DesktopManager implementation can be attached to any  instance of JDesktopPane (and thus, each of its children).

We have written an animated demo that shows when and how often each InternalFrameListener method is called. See \Chapter16\5, and execute the InternalFrameListenerDemo class. Figure 16.3 illustrates.

Figure 16.3   InternalFrameListener animated demo.

<<file figure16-3.gif>>

16.1.8  InternalFrameEvent

 class javax.swing.event.InternalFrameEvent

InternalFrameEvents are sent to InternalFrameListeners whenever a JInternalFrame is activated, closed, about to close, deactivated, deiconified, iconified, and opened. The following static int IDs designate which type of action an InternalFrameEvent corresponds to:

INTERNAL_FRAME_ACTIVATED

INTERNAL_FRAME_CLOSED

INTERNAL_FRAME_CLOSING

INTERNAL_FRAME_DEACTIVATED

INTERNAL_FRAME_DEICONIFIED

INTERNAL_FRAME_ICONIFIED

INTERNAL_FRAME_OPENED

InternalFrameEvent extends AWTEvent, and thus encapsultes its source and the associated event ID  (retrievable with getSource() and getID() respectively).

16.1.9  InternalFrameAdapter

class javax.swing.event.InternalFrameAdapter

This is a concrete implementation of the InternalFrameListener interface. It is intended to be extended for use by InternalFrameListener implementations that need to define only a subset of the InternalFrameListener methods. All methods defined within this adapter class have empty bodies.

16.1.10            Outline dragging mode

JDesktopPane supports an outline dragging mode to help with JInternalFrame dragging performance bottlenecks. To enable this mode on any JDesktopPane we must set the JDesktopPane.dragMode client property:

    myDesktopPane.putClientProperty(

      "JDesktopPane.dragMode","outline");

Instead of actually moving and painting the frame whenever it is dragged, an XOR'd rectangle is drawn in its place until the drag ends. The example in the next section shows outline dragging mode in action.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy