Setting the Maximum size of a JFrame while launching the application

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.

1

3 Answers

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

One possible solutions is:

Dimension DimMax = Toolkit.getDefaultToolkit().getScreenSize();
frame.setMaximumSize(DimMax);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

use dimension
Dimension d=getMaximumSize(); Frame.setSize(d.width, d.height);

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like