 | Level: Introductory John Zukowski (jaz@zukowski.net), President, JZ Ventures, Inc.
01 Jul 2001 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.
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();
}
}
|
Resources
About the author
Rate this page
|  |