Results 1 to 9 of 9
  1. #1
    TimmerCA is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default pack() makes JDialog too small

    I'm building a JDialog from scratch, like this:

    Java Code:
    package MyClient;
    
    import javax.swing.*;
    
    public class MyDialogForgotPassword extends JDialog {
      public JLabel InstructionsLabel1 = new JLabel();
      public JLabel EMailAddressLabel = new JLabel();
      public JTextField EMailAddress = new JTextField();
      public JButton SendRecoveryCode = new JButton();
      public JLabel InstructionsLabel2 = new JLabel();
      public JLabel RecoveryCodeLabel = new JLabel();
      public JTextField RecoveryCode = new JTextField();
      public JLabel NewPasswordLabel = new JLabel();
      public JPasswordField NewPassword = new JPasswordField();
      public JLabel ConfirmPasswordLabel = new JLabel();
      public JPasswordField ConfirmPassword = new JPasswordField();
      public JButton ChangePasswordButton = new JButton();
      public JButton CancelButton = new JButton();
    
      public MyDialogForgotPassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
        setTitle("Forgot Password");
    
        setResizable(false);
    
        InstructionsLabel1.setText("<html>If you have forgotten your password, enter your e-mail address in the space provided and click <strong>Send Recovery Code</strong>.  An account recovery code will be sent to your e-mail address.</html>");
    
        EMailAddressLabel.setLabelFor(EMailAddress);
        EMailAddressLabel.setText("E-Mail Address:");
    
        SendRecoveryCode.setText("Send Recovery Code");
    
        InstructionsLabel2.setText("<html>When you have received your recovery code, enter it into the space provided, enter your new password twice and then click <strong>Change Password</strong>.</html>");
    
        RecoveryCodeLabel.setLabelFor(RecoveryCode);
        RecoveryCodeLabel.setText("Recovery Code:");
    
        NewPasswordLabel.setLabelFor(NewPassword);
        NewPasswordLabel.setText("New Password:");
    
        ConfirmPasswordLabel.setLabelFor(ConfirmPassword);
        ConfirmPasswordLabel.setText("Confirm Password:");
    
        ChangePasswordButton.setText("Change Password");
    
        CancelButton.setText("Cancel");
    
        GroupLayout layout = new GroupLayout(getContentPane());
    
        getContentPane().setLayout(layout);
    
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
    
        layout.setHorizontalGroup(
          layout.createParallelGroup()
          .addComponent(InstructionsLabel1, 450, 450, 450)
          .addComponent(InstructionsLabel2, 450, 450, 450)
          .addGroup(
            layout.createSequentialGroup()
            .addGroup(
              layout.createParallelGroup()
              .addComponent(EMailAddressLabel)
              .addComponent(RecoveryCodeLabel)
              .addComponent(NewPasswordLabel)
              .addComponent(ConfirmPasswordLabel)
            )
            .addGroup(
              layout.createParallelGroup()
              .addComponent(EMailAddress)
              .addComponent(RecoveryCode)
              .addComponent(NewPassword)
              .addComponent(ConfirmPassword)
              .addComponent(SendRecoveryCode)
              .addGroup(
                layout.createSequentialGroup()
                .addComponent(ChangePasswordButton)
                .addComponent(CancelButton)
              )
            )
          )
        );
    
        layout.setVerticalGroup(
          layout.createSequentialGroup()
          .addComponent(InstructionsLabel1)
          .addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(EMailAddressLabel)
            .addComponent(EMailAddress)
          )
          .addComponent(SendRecoveryCode)
          .addComponent(InstructionsLabel2)
          .addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(RecoveryCodeLabel)
            .addComponent(RecoveryCode)
          )
          .addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(NewPasswordLabel)
            .addComponent(NewPassword)
          )
          .addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(ConfirmPasswordLabel)
            .addComponent(ConfirmPassword)
          )
          .addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(ChangePasswordButton)
            .addComponent(CancelButton)
          )
        );
    
        pack();
    
        setLocationRelativeTo(null);
      }
    }
    The problem is that pack() is apparently not computing the JDialog size correctly. The JDialog is shown with the correct width, but is about 100 pixels too short - the last JTextField and the last row of JButtons is clipped by the bottom of the JDialog. I'm sure I'm missing something really basic, but all my hair is currently in a pile on the floor...so, what am I doing wrong?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: pack() makes JDialog too small

    I think you should define the size of JDialog before calling the pack() method.

  3. #3
    TimmerCA is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default Re: pack() makes JDialog too small

    Quote Originally Posted by wsaryada View Post
    I think you should define the size of JDialog before calling the pack() method.
    Forgive me, but I'm pretty sure that the whole point of the pack() method is to calculate the size of the dialog for you. From the documentation:

    public void pack()

    Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: pack() makes JDialog too small

    Then you have to set the preferred size for your UI component. For example if you have a text field call recoveryCode, then set the preferred size by calling recoveryCode.setPreferredSize(dimension).

  5. #5
    TimmerCA is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default Re: pack() makes JDialog too small

    Quote Originally Posted by wsaryada View Post
    Then you have to set the preferred size for your UI component. For example if you have a text field call recoveryCode, then set the preferred size by calling recoveryCode.setPreferredSize(dimension).
    Then why does it show everything *except* the very last row (give or take a few pixels)?

    And why does it work correctly on JFrame forms, but not JDialog forms?

  6. #6
    TimmerCA is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default Re: pack() makes JDialog too small

    To give you a better idea of what I mean, here are two attachments: the first is a JFrame set up this way, and the second is a JDialog. They are both running in the same application - in fact, the JFrame creates and shows the JDialog when you click the "Forgot Password" button.

    The pack() function is setting the size of the JFrame perfectly, but the JDialog is missing the bottom 80 pixels or so.

    pack() makes JDialog too small-jframe.png

    pack() makes JDialog too small-jdialog.png

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: pack() makes JDialog too small

    Quote Originally Posted by TimmerCA View Post
    Then why does it show everything *except* the very last row (give or take a few pixels)?

    And why does it work correctly on JFrame forms, but not JDialog forms?
    Nothing to do with JFrame or JDialog. A little investigation revealed the cause: the layout isn't taking the increased height of the multi-line labels into account.

    Reduce the text on the labels to fit in one line and see for yourself.

    This looks like providing a viable solution: java - Get height of multi line text with fixed width to make dialog resize properly - Stack Overflow

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    TimmerCA is offline Member
    Join Date
    Jun 2012
    Posts
    10
    Rep Power
    0

    Default Re: pack() makes JDialog too small

    Quote Originally Posted by DarrylBurke View Post
    Nothing to do with JFrame or JDialog. A little investigation revealed the cause: the layout isn't taking the increased height of the multi-line labels into account.

    Reduce the text on the labels to fit in one line and see for yourself.

    This looks like providing a viable solution: java - Get height of multi line text with fixed width to make dialog resize properly - Stack Overflow

    db
    AWESOME! Thanks!

    The fact that pack() doesn't take multi-line labels into account seems like a bug to me...I wonder if I should open a bug report with Soracle about that...?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: pack() makes JDialog too small

    Swing development is at a near-standstill (the official line is that it is in 'maintenance mode') so it probably wouldn't ever be attended, seeing as there must be a few hundred bugs in the Swing pipeline.

    The problem is not with the layout. It's with the fundamentals of how JLabel and several other components handle line wrap. Or at an even more fundamental level, the design decision that decreed that only a preferredSize exists -- not separate preferredWidth and preferredHeight. It looks like JavaFX has addressed this design flaw -- there are separate setPrefWidth(...) and setPrefHeight(...) methods.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. What exactly does the pack() method do?
    By Nosrettap in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2011, 08:32 PM
  2. pack() method for JButton?
    By gib65 in forum AWT / Swing
    Replies: 3
    Last Post: 10-06-2010, 07:18 PM
  3. pack libraries into jar file
    By BigBear in forum New To Java
    Replies: 1
    Last Post: 05-24-2010, 11:19 AM
  4. Never makes it to the method!
    By minihazard10 in forum New To Java
    Replies: 9
    Last Post: 11-10-2008, 07:30 AM
  5. visual web pack
    By Jack in forum NetBeans
    Replies: 2
    Last Post: 07-02-2007, 05:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •