How to center the title of a Jframe?

Asked

Viewed 1,122 times

3

I have a jFrame component and I want to center his title, would it be possible?

Titulo do jFrame

1 answer

7


Natively it is not possible, but there is a solution that can solve alternatively, found in this reply from Soen, which would be aligning the text in a justified way to the left:

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import javax.swing.JFrame;

/**
 *
 * @author diego
 */
public class JFrameTitleCenter {

    public void createAndShowGUI() {
        JFrame t = new JFrame();
        t.setSize(600, 300);
        t.setFont(new Font("System", Font.PLAIN, 14));
        Font f = t.getFont();
        FontMetrics fm = t.getFontMetrics(f);
        int x = fm.stringWidth("Hello Center");
        int y = fm.stringWidth(" ");
        int z = t.getWidth() / 2 - (x / 2);
        int w = z / y;
        String pad = "";
        //for (int i=0; i!=w; i++) pad +=" "; 
        pad = String.format("%" + w + "s", pad);
        t.setTitle(pad + "Hello Center");

        t.setVisible(true);
        t.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JFrameTitleCenter().createAndShowGUI();
            }
        });
    }
}

The result:

inserir a descrição da imagem aqui

The above solution has a small limitation, which is that it does not work properly in resizable windows, for this, you need to detect resizing and rewrite the title after this event, as explained below.


Update

I developed a way to make the centering according to the screen size, that is, using the method below, it is possible to release resizing, so that the title will be adjusted to the new window size:

private void titleAlign(JFrame frame) {

    Font font = frame.getFont();
    
    String currentTitle = frame.getTitle().trim();
    FontMetrics fm = frame.getFontMetrics(font);
    int frameWidth = frame.getWidth();
    int titleWidth = fm.stringWidth(currentTitle);
    int spaceWidth = fm.stringWidth(" ");
    int centerPos = (frameWidth / 2) - (titleWidth / 2);
    int spaceCount = centerPos / spaceWidth;
    String pad = "";
    // for (int i=0; i!=w; i++) pad +=" ";
    pad = String.format("%" + (spaceCount - 14) + "s", pad);
    frame.setTitle(pad + currentTitle);

}

What I did was delegate to a method the centralization part of the title, passing only the screen reference.

However, it is necessary to have this method called every time the screen is resized, and for that just add a ComponentListener, overwriting the method componentSized() of the screen in which the title is intended to be centered:

seuJFrame.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        titleAlign(seuJFrame);
    }
});

After this, the screen will automatically adjust the title after each resizing.

inserir a descrição da imagem aqui

  • The window is not resizable so I believe the solution should work, I will test later. Thanks @diegofm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.