I am using Java 8 and I have a JFrame which I want to have minimum size required. It is okay if the frame is enlarged, but it should not go below minimum so that the GUI is usable.
For this I use the solution described in this answer. I first use pack
then set minimum size as frame size. But for some reason it only works if Windows scaling is set to 1/100%. For other scaling values I can actually make the frame smaller by dragging(this is proportional to the scaling value).
Here's an example code
import java.awt.BorderLayout;
import javax.swing.*;
public class ScalingProblem extends JFrame {
public ScalingProblem() {
setTitle("My Gui");
setSize(400, 400);
JButton button = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JPanel panel = new JPanel(new BorderLayout());
panel.add(button, BorderLayout.NORTH);
panel.add(button2, BorderLayout.EAST);
panel.add(button3, BorderLayout.WEST);
panel.add(button4, BorderLayout.SOUTH);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setMinimumSize(getSize());
}
public static void main(String[] args) {
ScalingProblem a = new ScalingProblem();
}
}
Even after setting minimum size of frame I can make it smaller by a factor of 1.25 which is also my scaling factor for display.
So basically when dragging the frame, size can be made smaller then the minimum size set using setMinimumSize
. I tried to set minimum size as 1.25 times of what getSize was returning, but that would make the frame larger and I can't make it smaller using methods like pack
or setSize
.
So my question is
- Can I make dragging respect the minimum size of the frame set using
setMinimumSize
? - Can I make the frame size smaller than the size set using
setMinimumSize
without manually dragging?