Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: JFrame - Window


The javax.swing.JFrame class is used to create a "window". This window has a few characteristics of its own (title bar, etc), but most of the controls are placed in one of two subareas: content pane or menu bar.


You can create a frame like this:

   JFrame w = new JFrame("your title");  // OK, but it's better to use subclassing as below.

Subclass JFrame, build in constructor. Another better way is to define a class, eg MyWindow that extends JFrame, put the code which builds the GUI in the class's constructor, and create an instance of the window from the main program. See example below.

Content pane - Two styles - Get it or set it

There are two common ways to use a JFrame's content pane. Both are commonly used. In both cases, the easiest style is to assign the working version of the content pane to

  • Get the predefined content pane and change it. Every JFrame comes with a default content pane. It doesn't have anything on it, altho it does have a default layout, probably not the one you want to use tho! The getContentPane() method returns a Container object, which interestingly is actually a Jpanel. No problem with this approach.
  • Create a JPanel and make it the content pane. Most of my example code is written this way. This requires a call to setContentPane(...).

Common methods

JFrame    w;   // This is the natural Java "window".
Container c;   // This will typically be a JPanel (subclass of Container).
WindowListener listen; //
String    title;
boolean   b;
JMenuBar  mBar;
Using the content pane. See Content Panes
c = w.getContentPane(); Returns window's content pane. Use either get or set, but not both.
w.setContentPane(c); Sets window's content pane to c (or subclass JPanel).
Handling the window's close box. See JFrame Close Box
This adds a window listener that simply executes System.exit(). It's the short, appropriate, solution for most small programs.
w.addWindowListener(listen); Use this to call your own window closing listener.
Executing the layout.
w.pack(); Finalize layout after everything is added to window and content pane. Don't use validate() with layout managers.
Displaying the window.
w.setVisible(true); Makes the window visible, and starts the Event Dispatch Thread (EDT) which manages the GUI. This is usually called by the main program after calling the constructor. show() should no longer be used.
w.setTitle(title); xxx
w.setResizable(false); Set to false if you don't want the user to resize your window.
w.setJMenuBar(mbar); This is how to add a menubar to the window.
w.setLocationRelativeTo(null); Centers the window. If you even bother with position, this is the most common choice.
Less common positioning and sizing.
w.setSize(w, h); Sets window to pixel width (w) and height (h). The only reasons to use this are to fill the screen or restore a window size from a previous run.
w.setLocation(x, y); Sets the upper left corner of the window to this screen pixel coordinate.


There is typically be a very short main method something like this.

  public static void main(String[] args {
      JFrame windo = new MyExample();

And a class which defines the window.

public class MyExample extends JFrame {
    //... Declare components, something to hold the model.
    . . .
    public MyExample() { // constructor builds GUI
        //... Build the content pane.
        Container content = this.getContentPane();
        content.add(...) // Add components to the content
        . . .
        this.setTitle("My new window");
        this.pack();   // does layout of components.
    }//end constructor
Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

hi !
I used to love roseindia.net for its amaaazing examples. A fully working class for every example was simply awesome, but your new design where you just give snippets is not as good... could you please insert full codes instead ? thanks

Posted by kpa on Friday, 01.9.09 @ 09:24am | #83671

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

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

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]