Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDialog allow user to only change width of the dialog

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

I know i can call the method setResizible(boolean) and that disables or enables the user from resizing the JDialog, but is there a way to restrict the user from changing the height of the window but allowing him to change the width?

The dialog I'm creating looks funny if it grows vertically but horizontally growing the component could be a benefit to the user.

Thanks in advance.

like image 737
user597608 Avatar asked Nov 27 '25 13:11

user597608


1 Answers

You could add a ComponentListener to the JDialog, and check in componentResized if height changed. This could be implemented by extending the JDialog class this way:

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);
            }
        });
    }
}
like image 180
airborn Avatar answered Nov 30 '25 01:11

airborn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!