How to set a Java JFrame title

JFrame title FAQ: How do I set the title of a JFrame?

Solution

There are two ways to set the JFrame title.

First, you can set the JFrame title when you construct your JFrame, like this:

JFrame jframe = new JFrame("My JFrame Title");

Second, once you have a valid JFrame object, you can call the setTitle method of the JFrame class, like this:

jframe.setTitle("My JFrame Title");

You can also use this second technique to change the title of an existing JFrame.

A complete JFrame title example

To help with your own practice and understanding, here's the source code for a complete "JFrame title" example:

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JFrameTitle
{

  public static void main(String[] args)
  {
    // schedule this for the event dispatch thread (edt)
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        displayJFrame();
      }
    });
  }

  static void displayJFrame()
  {
    // set the jframe title in the constructor
    JFrame jframe = new JFrame("JFrame Title Example");

    // all the other jframe setup stuff
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setPreferredSize(new Dimension(400, 300));
    jframe.pack();
    jframe.setLocationRelativeTo(null);
    jframe.setVisible(true);
  }

}

I hope these JFrame title examples help in your own practice.