6

Possible Duplicate:
JDialog allow user to only change width of the dialog

I have a JDialog that I would like the width to be resizable, but the height not.

This is my current code:

addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        setLocation(getLocation().x, getLocation().y + (getHeight() - staticHeight));
        setSize(new Dimension(getWidth(), staticHeight));
        super.componentResized(e);
    }
});

Problem is, that code gets called after the window is resized. Making the window resize and then flicker back.

I would like to make it so when the user trys to drag the height of the window it does nothing.

Community
  • 1
  • 1
Josh
  • 6,046
  • 11
  • 52
  • 83

4 Answers4

4
  • you can't directly stop resizing,

  • you can't block basic property of containers,

  • container because from Native OS and are built on its properties,

  • and limitations for resizing is screen size

  • rest are (with code) Resizing Components by @camickr

  • there are dirty hack based on AbsoluteLayout, and I don't suggest using that

  • block resize if there is any flickering or freeze during resize

Denis Abakumov
  • 355
  • 3
  • 11
mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

looks like you cant. there's a setResizable() method but its all-or-nothing.
you could try mitigating this by using a layout inside your JDialog so that your content remains the same height regardless of the JDialog height
or perhaps (a bit more radical) set it as not resizable and implement your own mouse listener for resizing yourself ? that way you could have full control

radai
  • 23,949
  • 10
  • 71
  • 115
3

After looking for a while, I could not find any really satisfactory solutions. I think that the resize of the dialog is something that is handled directly at the OS level and therefore you can only say that you want it totally not resizable or totally resizable.

The code below will always prevent the dialog from being bigger, but as the user resize the dialog, the borders of the dialog still move.

The other option, also suggested by radai, would be to prevent resize and set a Custom content pane with a mouse listener that listens for the mouse and resize accordingly. However, I think that this will not feel very native to the user (I don't think that you will be able to catch the events on the border of the dialog).

import java.awt.Frame;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                init();
            }
        });
    }

    public static void init() {
        final int staticHeight = 150;
        final JDialog dialog = new JDialog((Frame) null) {

            @Override
            protected JRootPane createRootPane() {
                JRootPane rp = new JRootPane() {
                    @Override
                    public void reshape(int x, int y, int w, int h) {
                        super.reshape(x, y, w, staticHeight);
                    }
                };
                rp.setOpaque(true);
                return rp;
            }

            @Override
            public void reshape(int x, int y, int width, int height) {
                super.reshape(x, y, width, staticHeight);
            }

        };
        dialog.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                dialog.setSize(dialog.getWidth(), staticHeight);
            }
        });
        dialog.pack();
        dialog.setVisible(true);

    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • no there isn't any proper solution, ComponetListener isn't correct way, I saw dirty hack that un-armed mouse from container after, please see my answer here – mKorbel May 08 '12 at 16:08
1

Here try this code example, that's how you set the JDialog to not have vertical Resizing. Though I really doesn't want to take credit for this, the whole credit goes to the creator of this program which is "Darryl Burke" the link to the program is here.

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  

public class WidthResizeableDialog {  

  Robot robot;  
  static final int HEIGHT = 400;  
  Point lastLocation;  

  public static void main(String[] args) {  
    SwingUtilities.invokeLater(new Runnable() {  

      @Override  
      public void run() {  
        new WidthResizeableDialog().makeUI();  
      }  
    });  
  }  

  public void makeUI() {  
    try {  
      robot = new Robot();  
    } catch (AWTException ex) {  
      ex.printStackTrace();  
    }  
    final JDialog dialog = new JDialog();  
    dialog.setSize(400, HEIGHT);  
    dialog.setLocationRelativeTo(null);  
    dialog.addWindowListener(new WindowAdapter() {  

      @Override  
      public void windowClosing(WindowEvent e) {  
        System.exit(0);  
      }  
    });  
    dialog.addComponentListener(new ComponentAdapter() {  

      @Override  
      public void componentMoved(ComponentEvent e) {  
        SwingUtilities.invokeLater(new Runnable() {  

          @Override  
          public void run() {  
            lastLocation = dialog.getLocation();  
          }  
        });  
      }  
    });  
    dialog.getRootPane().addComponentListener(new ComponentAdapter() {  

      @Override  
      public void componentResized(ComponentEvent e) {  
        int height = dialog.getHeight();  
        if (robot != null && height != HEIGHT) {  
          robot.mouseRelease(InputEvent.BUTTON1_MASK);  
          dialog.setLocation(lastLocation);  
          dialog.setSize(dialog.getWidth(), HEIGHT);  
        }  
      }  
    });  
    dialog.setVisible(true);  
  }  
} 
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Darryl is great mind +1, again I disagreed with dirty hacks, nor with java.awt.Robot, because is API for testing purposes, not designated for productions code, sorry my view, maybe someone could be satifyied wiht that – mKorbel May 08 '12 at 16:52
  • Nah, rather your answer is as good as any other answer, too true, certain things are meant to be controlled by the OS, and they should be treated as such, without any interference :-) – nIcE cOw May 08 '12 at 16:54