Skip to main content

Magic with Merlin: Just another plain Frame

Maximize and undecorate your AWT Frames

John Zukowski (jaz@zukowski.net), President, JZ Ventures, Inc.
Author photo
John Zukowski conducts strategic Java consulting with JZ Ventures, Inc. His latest books are Java Collections and Definitive Guide to Swing for Java 2 (2nd ed) from Apress. Contact John at jaz@zukowski.net.

Summary:  Java 1.4 now allows you to remove a frame's title bar and programmatically maximize the frame. John Zukowski details and demonstrates these changes with this quick start and also shows how to support moving a frame without a draggable area.

View more content in this series

Date:  01 Jul 2001
Level:  Introductory
Activity:  1739 views

As you read through the enhancements to the latest release of the J2SE platform, you'll notice immediately that Sun has finally listened to our suggestions. I don't mean to imply that they didn't listen at all in the past, but they seemed to be more focused on adding large-scale APIs than on fixing the ones that have been around for ages. Take for example the AWT Frame class. While Sun added the ability to programmatically iconify the frame in version 1.2, you still couldn't hide the title bar or maximize a frame. While you could use a Window to avoid the title bar, some tasks require a top-level frame, not a window. You were basically out of luck.

Now, with the 1.4 release, you can programmatically hide the platform-specific window decorations, like the title bar, and maximize a frame. Both of these features were first requested back in 1997. The undecorated frame support is a result of an entry in Sun's bug database, bug ID 4038769 from March 1997, and frame zooming support was requested in August 1997 with bug ID 4071554. I'll show you how to use both of these features in this article.

Undecorated frames

The simplest of the two features to use is support for undecorated frames. To hide the title bar on the frame, you need to set the undecorated property of the specific frame to true. By default, the value is false, and you can't change the setting once the frame has been displayed (an IllegalComponentStateException will be thrown if you try).


Listing 1. Create an undecorated frame

  Frame frame = new Frame();
  frame.setUndecorated(true);

Because the title bar and other window decorations are now hidden, you can't rely on the underlying window management system to provide support for dragging the frame. You must add this in yourself with a pair of mouse listeners:


Listing 2. Add in drag support

  // Avoid creating a point with each mousePressed() call
  Point origin = new Point();
  frame.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
      origin.x = e.getX();
      origin.y = e.getY();
    }
  });
  frame.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
      Point p = frame.getLocation();
      frame.setLocation(
        p.x + e.getX() - origin.x, 
        p.y + e.getY() - origin.y);
    }
  });


Maximizing frames

Programmatic iconification was added to version 1.2 with the setState() method. Now, with the 1.4 version, there's a setExtendedState() method. This method takes a bitmask flag of the acceptable states, of which there are four. A fifth flag, MAXIMIZED_BOTH, is a combination of two others. The acceptable states are:

  • NORMAL -- The normal frame sizing state
  • ICONIFIED -- An iconified state
  • MAXIMIZED_BOTH -- The maximized frame state (maximized both horizontally and vertically)
  • MAXIMIZED_HORIZ -- A horizontal maximized state (may not be supported by windowing environment)
  • MAXIMIZED_VERT -- A vertical maximized state (may not be supported by windowing environment)

More often than not, you'll only use the first three states. If you find you need to combine states, like if you want to iconify a frame but preserve its current maximized state, you would use a vertical bar (|) to combine multiple states. Then, once you have the right state, you would use the new setExtendedState() method to change the frame's state.


Listing 3. Preserving maximized state

  // Preserve maximized
  frame.setExtendedState(Frame.ICONIFIED 
    | frame.getExtendedState());


A complete example

Here's a complete example that hides the frame's decorations and then adds the window buttons for maximizing, normalizing, iconifying (minimizing), and closing the frame as plain AWT button components. There's also an area available where you can drag the frame around.


Listing 4. Frame decorating complete example

import java.awt.*;
import java.awt.event.*;
public class FrameTest {
  static Point origin = new Point();
  public static void main (String args[]) {
    final Frame frame = new Frame();
    frame.setUndecorated(true);
    frame.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        origin.x = e.getX();
        origin.y = e.getY();
      }
    });
    frame.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        Point p = frame.getLocation();
        frame.setLocation(
          p.x + e.getX() - origin.x, 
          p.y + e.getY() - origin.y);
      }
    });
    frame.setSize(300, 300);
    Button b1 = new Button("Maximize");
    b1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
      }
    });
    Button b2 = new Button("Iconify");
    b2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // Preserve maximizing
        frame.setExtendedState(Frame.ICONIFIED 
		   | frame.getExtendedState());
      }
    });
    Button b3 = new Button("Normal");
    b3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        frame.setExtendedState(Frame.NORMAL);
      }
    });
    Button b4 = new Button("Close");
    b4.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    frame.setLayout(new GridLayout(5,1));
    frame.add(b1);
    frame.add(b2);
    frame.add(b3);
    frame.add(b4);
    frame.show();
  }
}



Download

DescriptionNameSizeDownload method
Sample codeFrameTest.java4KB HTTP

Information about download methods


Resources

About the author

Author photo

John Zukowski conducts strategic Java consulting with JZ Ventures, Inc. His latest books are Java Collections and Definitive Guide to Swing for Java 2 (2nd ed) from Apress. Contact John at jaz@zukowski.net.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=10563
ArticleTitle=Magic with Merlin: Just another plain Frame
publish-date=07012001
author1-email=jaz@zukowski.net
author1-email-cc=jaloi@us.ibm.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers