How to center align the title in a JFrame?
Asked Answered
C

5

12

I have a JFrame with a Title. I want center align the title, so that it appears in the middle of the JFrame's title.

Thanks.

Cystocarp answered 12/3, 2012 at 6:2 Comment(0)
A
10

Consider leaving the title left-justified...but...this will get you near the center. For resizable frames, you need to rewrite the title on resize.

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");
Antistrophe answered 12/3, 2012 at 7:8 Comment(1)
+1, I guess that's the right answer, but for resizable from I guess he can write one addWindowStateListener(...) to make that work :-)Vernonvernor
D
8

Adapting the answer's source of @Java42, I did a "workaround" to adjust title when resizing JFrame:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;


public class JFrameTitleCenter {

    public void createAndShowGUI() {

        JFrame frame = new JFrame();
        frame.setPreferredSize(new Dimension(300, 200));
        frame.setTitle("Hello Center");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

        });

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    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 = "";
        pad = String.format("%" + (spaceCount - 14) + "s", pad);
        frame.setTitle(pad + currentTitle);

    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            new JFrameTitleCenter().createAndShowGUI();
        });
    }
}

Working:

enter image description here

Demonstrator answered 14/9, 2017 at 12:34 Comment(0)
D
2
I have a JFrame with a Title. I want center align the title, so that 
it appears in the middle of the JFrame's title.

simple not possible to centering narrarive in the JFrames TitleBar, this container came from Native OS

  • dirty hack are possible by implements getSystemLookAndFeel in Windows OS

  • create undecorated JFrame by adding JPanel (simulated Toolbar) with JButtons (JButtons with icons simulating standards buttons )

Dives answered 12/3, 2012 at 6:47 Comment(0)
S
0

I don't know that you can without using a complicated scheme. If it's to your liking, you could apply a titled border with centered text to the frame's content pane.

Salimeter answered 12/3, 2012 at 6:28 Comment(0)
H
0

center align the title in a jFrame is very easy in netbeans, what u need to do is below:

1.

enter image description here

2.

enter image description here

  1. run your app.

enter image description here

Horsy answered 30/10, 2019 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.