Saturday, March 12, 2005

How to find the width and height of an application

Introduction

Why find the width and height of an application?
A good answer might be: You want the application to run as an applet, using the same width and height as the application.

Program

void createFrame() {
    setTitle(getClass().getName());
    createContentPane();

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    pack(); // show JFrame
    setVisible(true);

    System.out.println("width: " + getContentPane().getSize().width);
    System.out.println("height: " + getContentPane().getSize().height);
}