6

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
  • 1
    Why setMaximizedBounds instead of setMaximumSize?
    – wchargin
    May 7, 2013 at 4:46

3 Answers 3

7

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
  • This makes your frame flicker on the screen as it reaches the max width or height. In some cases, it gets into an infinite loop hiding itself and reshowing.
    – Rangi Keen
    Jul 16, 2015 at 15:15
3

One possible solutions is:

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

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
-2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.