I want to set the Maximum size of the JFrame while launching the application. Problem is, if the screen resolution is more my frame is getting bigger , but at that time it should not cross the max range defined, but same case works fine with low resolution.
Like I want my frame to be of Maximum of (500,500) , so I wrote this piece of code:
JFrame frame = new JFrame("FRAME TRANSPARENT");
frame.setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()-50), (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()-150));
frame.setMaximizedBounds(new Rectangle(0,0 , 500, 500));
frame.setVisible(true);
Even I set the Bound, the JFrame is considering setSize method and it seems to be that it is neglecting the setMaximizedBounds method. I already tried with setMaximumized method but got the same output.
I tried it and you're so right that setMaximumSize()
doesn't work.. all these years couldn't get to know that!!! Mostly my requirements of limiting size is fixed by setResizable(false)
, although I see you have specific query of minimum and maximum sizes to be different
Worked on a solution for you though:
public class MaxSizeUI
{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MaxSizeUI().makeUI();
}
});
}
public void makeUI() {
final JFrame frame = new JFrame("Sample Fram") {
@Override
public void paint(Graphics g) {
Dimension d = getSize();
Dimension m = getMaximumSize();
boolean resize = d.width > m.width || d.height > m.height;
d.width = Math.min(m.width, d.width);
d.height = Math.min(m.height, d.height);
if (resize) {
Point p = getLocation();
setVisible(false);
setSize(d);
setLocation(p);
setVisible(true);
}
super.paint(g);
}
};
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
frame.setMaximumSize(new Dimension(400, 200));
frame.setMinimumSize(new Dimension(200, 100));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
One possible solutions is:
Dimension DimMax = Toolkit.getDefaultToolkit().getScreenSize();
frame.setMaximumSize(DimMax);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With