10 Replies Latest reply: Jul 27, 2008 2:15 PM by DarrylBurke RSS

    Keeping a JDialog Title Centered.

    843806
      Guys:

      I have a strange problem and am not sure how to get around it. I am ising setTitle() to place a title string on my JDialog. Now I toulf like to center the title. On my desk PC I do this by simply inserting enough spaces before the title to center it. When I run there the title is beautifully centered. However, when I take my code to another computer (especially one made by another manufacturer) it is totally not centered. Is there a way to insure my title stays centered no matter what machine my code is running on? SetTitle(String) takes only a String argument.

      Thank you in advance.

      Phil Rossomando
        • 1. Re: Keeping a JDialog Title Centered.
          DarrylBurke
          Consider using an undecorated JDialog [setUndecorated(true)] with a TitledBorder with the title ABOVE_TOP

          db
          • 2. Re: Keeping a JDialog Title Centered.
            843806
            Darryl:

            Actually considered this but the resulting Dialog was considered ugly by our Systems
            people.

            Phil
            • 3. Re: Keeping a JDialog Title Centered.
              DarrylBurke
              Would a Windows-only solution be of interest?

              db
              • 4. Re: Keeping a JDialog Title Centered.
                843806
                Thank you but I need a popup dialogue and the investment has been made. Now I need a centered title.
                Thank you for your suggestions however.

                Phil
                • 5. Re: Keeping a JDialog Title Centered.
                  DarrylBurke
                  lol, I'm talking about the WIndows OS, not java.awt.Window.

                  Give this a spin, but only on a Windows machine.
                  import java.awt.*;
                  import java.awt.event.*;
                  import javax.swing.*;
                  
                  public class DialogCenterTitle {
                  
                     JDialog dialog;
                     JTextField textField;
                     final Toolkit kit = Toolkit.getDefaultToolkit();
                     final Font font = (Font) kit.getDesktopProperty("win.frame.captionFont");
                     // titleHeight is a proxy for the width of the
                     // square space reserved for the titlebar icon.
                     final int titleHeight = (Integer) kit.getDesktopProperty(
                             "win.frame.captionHeight");
                  
                     void makeUI() {
                  
                        textField = new JTextField(50);
                        textField.addActionListener(new ActionListener() {
                  
                           @Override
                           public void actionPerformed(ActionEvent e) {
                              dialog.setTitle(getCenteredTitle(textField.getText()));
                           }
                        });
                  
                        JPanel panel = new JPanel();
                        panel.addComponentListener(new ComponentAdapter() {
                  
                           @Override
                           public void componentResized(ComponentEvent e) {
                              dialog.setTitle(getCenteredTitle(dialog.getTitle().trim()));
                           }
                        });
                        panel.add(textField);
                        panel.revalidate();
                  
                        dialog = new JDialog();
                        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        dialog.add(panel);
                        dialog.pack();
                  
                        String title = "Type in the text field and press <Enter> to change this title.";
                        title = getCenteredTitle(title);
                        dialog.setTitle(title);
                  
                        dialog.setLocationRelativeTo(null);
                        dialog.setVisible(true);
                     }
                  
                     private String getCenteredTitle(String title) {
                  
                        Graphics graphics = dialog.getGraphics();
                        FontMetrics metrics = graphics.getFontMetrics(font);
                  
                        int spaceWidth = (dialog.getWidth() - metrics.stringWidth(title)) / 2
                                - dialog.getInsets().left - titleHeight;
                        String spaces = "";
                        while (metrics.stringWidth(spaces) < spaceWidth) {
                           spaces += " ";
                        }
                        return spaces + title;
                     }
                     
                     public static void main(String[] args) {
                        SwingUtilities.invokeLater(new Runnable() {
                  
                           @Override
                           public void run() {
                              new DialogCenterTitle().makeUI();
                           }
                        });
                     }
                  }
                  db
                  • 6. Re: Keeping a JDialog Title Centered.
                    843806
                    Darryl:

                    No I can't use Windows OS windows. Our system runs under Linux.
                    I tried you solution on my home PC and it may be just what I need.
                    I will give it a shot. If it does the trick, a BIG thank you to you.

                    Phil
                    • 7. Re: Keeping a JDialog Title Centered.
                      843806
                      Darryl:

                      Is there a Linux equivalent solution?

                      Thank you so very much for your feedback.

                      Phil
                      • 8. Re: Keeping a JDialog Title Centered.
                        DarrylBurke
                        Is there a Linux equivalent solution?
                        Couldn't find any, but that doesn't mean there isn't one. Search around, and if you still can't find anything you could try bumping this topic after the weekend when more users will be online.

                        luck, db
                        • 9. Re: Keeping a JDialog Title Centered.
                          843806
                          Darryl:

                          How does one "bump" a topic?

                          Phil
                          • 10. Re: Keeping a JDialog Title Centered.
                            DarrylBurke
                            By posting in it :-) That brings it back to the top of the forum listings.

                            As you did, about 8 hours ago.

                            db