How to set a Java JFrame title

JFrame title FAQ: How do I set the title of a Java 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");

A second approach is that 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 demonstrate this, here's the source code for a complete “JFrame title” example that shows how to set the title in the constructor:

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()
  {
    // [1] 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.