Results 1 to 6 of 6
  1. #1
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default JDialog.setResizable() odd behaviour

    This is a pretty basic question, but I ran into this today when I got a bugreport of a dialog that was too big to close (Damned users! ) Apparently it's not so basic as I thought...

    When I make a JDialog resizable (which is the default), the resulting dialog fits on my screen perfectly and scrollbars are added on the child panel. However, when I set resizable to false, the dialog will be huge and runs off the screen. As the statement JDialog.setResizable(false) implies, the JDialog is not resizable. More specifically, the JavaDoc says: Sets whether this dialog is resizable by the user. However, the LayoutManagers also seem to be limited at the same time as well.

    I have tried several different orders, like pack() before and after setResizable(), doesn't change a thing. You can see in the demo below that I have also tried several LayoutManagers (Swing, JGoodies and MigLayout), to rule out those, but the result is always the same. If you change the number of buttons to a smaller number, say 20, you can see it does work as expected.

    In the end I could make the JDialog resizable and get this over with, but this problem will nevertheless haunt me in my dreams :)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class DialogTest extends JDialog {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new DialogTest().setVisible(true);
                }
            });
        }
    
        public DialogTest() {
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
            getContentPane().setLayout(new BorderLayout());
            int buttonCount= 100;
    
            //Test using Swings own layoutmanager
            JPanel buttons = new JPanel(new GridLayout(buttonCount,1));
            for (int i = 0; i < buttonCount; i++) {
                buttons.add(new JButton("" + i));
            }
            JScrollPane pane = new JScrollPane(buttons);
    
    /*
            //Test using JGoodies FormLayout
            DefaultFormBuilder builder = new DefaultFormBuilder( new FormLayout("pref"));
            for (int i = 0; i < buttonCount; i++) {
                builder.append(new JButton("" + i));
                builder.nextLine();
            }
            JScrollPane pane = new JScrollPane(builder.getPanel());
    */
    
    /*
            //Test using MigLayout
            JPanel buttons = new JPanel( new MigLayout("wrap"));
            for (int i = 0; i < buttonCount; i++) {
                buttons.add(new JButton("" + i));
            }
            JScrollPane pane = new JScrollPane(buttons);
    */
    
            getContentPane().add(pane, BorderLayout.CENTER);
            pack();
            //change this to true and false and back
            setResizable(false);
            setLocationRelativeTo(null);
    
        }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: JDialog.setResizable() odd behaviour

    Well if I set a preferred size on the content pane, it behaves more "normally".

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: JDialog.setResizable() odd behaviour

    Another option is to limit the JScrollpane's preferredSize before adding it to the contentPane.

    Java Code:
     pane.setPreferredSize(new Dimension(10, 200));
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: JDialog.setResizable() odd behaviour

    Both your suggestions work. Thanks for testing it.

    In our application, this dialog shows a summary of some calculation with can have anywhere from one to n lines. But the JDialog should grow if it wants to, but not bigger than the screen. Setting a preferred size will make the dialog too big in 99% of the cases, and setMaximumSize() does nothing. It's a mystery why that method is in there anyway.

    I am thinking of setting the preferredsize to n * labelheight, but that sounds really hackish to me... Isn't this just a JDialog bug?

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: JDialog.setResizable() odd behaviour

    Isn't this just a JDialog bug?
    Not really, the LayoutManagers overrule some of the dialog's settings. With a NullLayout (blech) it should work.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: JDialog.setResizable() odd behaviour

    If it is you're stuck anyway, any bug report about it will get such a low priority it will never be picked up.

    > and setMaximumSize() does nothing. It's a mystery why that method is in there anyway
    Well Swing is at its core an API, it COULD be supported by a component implementation. But only when it really makes sense and can work reliably.

Similar Threads

  1. Replies: 7
    Last Post: 05-30-2013, 06:17 PM
  2. very Strange behaviour !!
    By selimzz in forum Advanced Java
    Replies: 12
    Last Post: 01-06-2013, 11:35 AM
  3. Replies: 1
    Last Post: 12-12-2012, 08:43 PM
  4. StringWorker different behaviour
    By _SAS in forum Threads and Synchronization
    Replies: 5
    Last Post: 08-06-2011, 09:18 PM
  5. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 03:31 PM

Posting Permissions

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