Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Set a window's minimum size? - JavaWorld October 1999

Set a window's minimum size? - JavaWorld October 1999

Tutorial Details:

Set a window's minimum size?
Set a window's minimum size?
By: By Java Q&A Experts
Use event listeners to enforce the minimum size of a window
Is there a clean way to set a minimum size for a window? I want users to be able to enlarge my application window, but not make it smaller than a certain minimum size. I looked at APIs on the JFrame hierarchy and couldn't find anything I could use. Any help would be greatly appreciated.
There is no single method to create a minimum size for a window. However, you can enforce a minimum size by using event listeners. Whenever a resize event is received, check the size of the window and, if it is below the specified minimum, resize it back.
Here is the code:
import javax.swing.*;
import java.awt.event.*;
public class MinSizeFrame extends JFrame implements ComponentListener {
static final int WIDTH = 400;
static final int HEIGHT = 400;
static final int MIN_WIDTH = 300;
static final int MIN_HEIGHT = 300;
public MinSizeFrame() {
setSize(WIDTH, HEIGHT);
addComponentListener(this);
}
public void componentResized(ComponentEvent e) {
int width = getWidth();
int height = getHeight();
//we check if either the width
//or the height are below minimum
boolean resize = false;
if (width < MIN_WIDTH) {
resize = true;
width = MIN_WIDTH;
}
if (height < MIN_HEIGHT) {
resize = true;
height = MIN_HEIGHT;
}
if (resize) {
setSize(width, height);
}
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
public static void main(String args[]) {
MinSizeFrame f = new MinSizeFrame();
f.setVisible(true);
}
}
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:
Set a window's minimum size? - JavaWorld October 1999

View Tutorial:
Set a window's minimum size? - JavaWorld October 1999

Related Tutorials:

Reloading Applets
Reloading Applets
 
Java Tip 72: Press Escape to close your Swing dialog windows
Java Tip 72: Press Escape to close your Swing dialog windows
 
Accelerate your RMI programming
Accelerate your RMI programming
 
Create a scrollable virtual desktop in Swing
Create a scrollable virtual desktop in Swing
 
Flex your grid layout
Flex your grid layout
 
Pick up performance with generational garbage collection
Pick up performance with generational garbage collection
 
Create your own type 3 JDBC driver, Part 2
Create your own type 3 JDBC driver, Part 2
 
Bridge the gap between Java and Twain
Bridge the gap between Java and Twain
 
Jini's relevance emerges, Part 2
Jini's relevance emerges, Part 2
 
Add concurrent processing with message-driven beans
Add concurrent processing with message-driven beans
 
Introducing the Portlet Specification, Part 1
Introducing the Get your feet wet with the specification's underlying terms and concepts
 
Very interesting
Very interesting
 
Juggling JOGL
This article introduces the concepts in JOGL, the Java bindings to OpenGL, that are applicable to 2D gaming. We start with the handling of coordinate spaces and how they\'re scaled from the OpenGL world to the screen. Then we integrated JOGL\'s built-in A
 
Use SWT for data entry
Use SWT for data entry Like many Java programmers, you may have given up on writing client-side window applications. There's a lot of debate about why client-side Java is out of fashion. But most of it boils down to the fact that Java—until now—cou
 
Daffodil DB - Your complete Java RDBMS
Daffodil DB - Cross Platform Java Database Products... Daffodil DB is a J2EE-certified, SQL-99 and JDBC standards compliant Java RDBMS, made available in both Embedded and Network Editions .
 
The JModalWindow Project
This article explains the workings of the JModalWindow project, which provides two top-level components, called ModalWindows, that introduce such modality.
 
Develop aspect-oriented Java applications with Eclipse and AJDT
AspectJ is an aspect-oriented extension of the Javaâ„¢ language that enables a modular implementation of crosscutting concerns. This crosscutting behavior, which can be static or dynamic, presents an extra challenge to tools that support AspectJ. The AJDT
 
JSP templates. Use JSP templates to encapsulate Webpage layout and encourage modular design
Window toolkits typically provide a layout mechanism that positions widgets in a container. For example, AWT and Swing have layout managers, and VisualWorks Smalltalk has wrappers. This article presents a template mechanism for JSP that allows layout to b
 
J2ME Technology Turns 5!
In 2004 the Java 2 Platform, Micro Edition (J2ME) celebrated its fifth anniversary. This article presents where J2ME is today.
 
UML 2.0 Sequence Diagramming
Modeling Complex Code in SunONE Studio with Embarcadero's Integrated Modeling Developement Environment, Describe.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.