2 Replies Latest reply: Aug 30, 2010 12:59 PM by 843807 RSS

    Resize JDialog to fit dialog title

    843807
      Hello members

      I've been trying to resize a JDialog to have the same width as the dialog title (which is longer than the information in the dialog panels).
      The code below does not work, how can I change it to get the dialog resized?
      import javax.swing.*;*
      *import java.awt.*;
      import java.awt.event.*;
      
      public class DialogTitleTest {
      
           public DialogTitleTest() {
                JDialog dialog = new JDialog();
                dialog.setLayout(new BorderLayout());
                dialog.setSize(new Dimension(300, 300));
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.add(new JLabel("Hello"), BorderLayout.CENTER);
                String title = "This is a very very very very long title";
                dialog.setTitle(title);
      
                dialog.pack();
                dialog.setVisible(true);
      
                JLabel titleLabel = new JLabel(title);
                Dimension dialogPrefSize = dialog.getPreferredSize();
                System.out.println("Dialog pref width "  +dialogPrefSize.getWidth()+  " title pref width " + titleLabel.getPreferredSize().getWidth());
          dialog.setPreferredSize(new Dimension(
              (int) titleLabel.getPreferredSize().getWidth(),
              (int) dialogPrefSize.getHeight()));
          dialog.validate();
          dialog.repaint();
           }
      
      
        public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                 DialogTitleTest p = new DialogTitleTest();
               }
          });
        }
      }
        • 1. Re: Resize JDialog to fit dialog title
          DarrylBurke
          Since JDialog doesn't expose any public API for this, any solution has to be implementation dependent. Moreover, unless you set LaF decorations, there is no solution as the titlebar font is then under the control of the OS.

          This works for the default Metal LaF.
          import java.awt.*;
          import javax.swing.*;
          
          public class DialogTitleTest {
          
            public static void main(String[] args) {
              JDialog.setDefaultLookAndFeelDecorated(true);
              SwingUtilities.invokeLater(new Runnable() {
          
                public void run() {
                  new DialogTitleTest().makeUI();
                }
              });
            }
            public void makeUI() {
              JDialog dialog = new JDialog();
              dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
              dialog.add(new JLabel("Hello"));
              String title = "This is a very very very very long title";
              dialog.setTitle(title);
              dialog.pack();
          
              int spacing = 5; // from MetalTitlePane.java
              int width = 0;
              Container titleBar = (Container) dialog.getLayeredPane().getComponents()[1];
              FontMetrics fm = titleBar.getFontMetrics(titleBar.getFont());
              width += spacing + fm.stringWidth(title); // title text and space before
              for (Component component : titleBar.getComponents()) {
                width += (spacing + component.getWidth()); // buttons and space before
              }
              width += spacing; // space after last button
              width += dialog.getWidth() - dialog.getContentPane().getWidth(); // dialog borders
              dialog.setSize(new Dimension(width, dialog.getPreferredSize().height));
              
              dialog.setVisible(true);
            }
          }
          db
          • 2. Re: Resize JDialog to fit dialog title
            843807
            Thank you, it works like a charm.