JDialog allows the user to change the width of the dialog

Does anyone know if it is possible to limit how a user resizes a JDialog?

I know that I can call the setResizible (boolean) method and disable or allow the user to resize the JDialog, but is there a way to limit the user to resize the window, but let him resize it?

The dialog that I create looks funny if it grows vertically, but a horizontally growing component can be useful to the user.

Thanks in advance.

+3
source share
1 answer

You can add ComponentListenerto JDialog and check componentResizedif the height has changed. This can be implemented by extending the JDialog class as follows:

public class Dialog extends javax.swing.JDialog {

    public Dialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        final int h = getHeight();
        addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent e) {
                Rectangle b = getBounds();
                if (b.height != h) {
                    b.height = h;
                    setBounds(b);
                }
                super.componentResized(e);
            }
        });
    }
}
+3

All Articles