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);
}
}