JFrame: How to disable window resizing?
Asked Answered
A

8

50

I am creating a JFrame and I call the method setSize(500, 500). Now the desired behaviour is that JFrame should not be resized by user in any condition. Either by maximizing or by dragging the borders. It should be 500x500. How can I do it? I have also attached the code in case you can guide me better.

    package com.techpapa;    
    import javax.swing.*;  
    import java.awt.*;  
    import java.awt.event.*;  

    public class MainWindow extends JFrame{


private JTextField
            write;
private JRadioButton
            rb1,
            rb2,
            rb3;
private ButtonGroup
            bg;

private ActionListener al = new ActionListener(){
    public void actionPerformed(ActionEvent e){
        write.setText("JRadioButton : " + ((JRadioButton)e.getSource()).getText());
    }

};

public MainWindow(){
    //Frame Initialization
    setSize(500, 500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(null);
    setTitle(".:JRadioButton:.");
    setVisible(true);

    //Components Initialization
    write = new JTextField(20);
    write.setEditable(false);
    rb1 = new JRadioButton("Male", false);
    rb1.addActionListener(al);
    rb2 = new JRadioButton("Female", false);
    rb2.addActionListener(al);
    rb3 = new JRadioButton("I don't want to specify", true);
    rb3.addActionListener(al);
    bg = new ButtonGroup();

    //Add radio buttons to buttongroup
    bg.add(rb1); bg.add(rb2); bg.add(rb3);

    //Add to window
    add(write);
    write.setBounds(140, 100, 150, 20);
    write.setDragEnabled(true);
    add(rb1);
    rb1.setBounds(180, 200, 100, 30);
    add(rb2);
    rb2.setBounds(180, 225, 100, 30);
    add(rb3);
    rb3.setBounds(180, 250, 130, 30);

}

public static void main(String[] args) {
    new MainWindow();

}

}
Accompany answered 3/8, 2013 at 10:12 Comment(2)
please why 1. setBound(), 2. non_resizeable 3. setVisible(true); must be last code line in contructorZachar
Have thought about using LayoutManagers? Because it's highly required with Swing, also it's not that hard to learn, maybe takes that time when you manually set the location of each component.Stannum
M
128

You can use a simple call in the constructor under "frame initialization":

setResizable(false);

After this call, the window will not be resizable.

Marelda answered 3/8, 2013 at 10:14 Comment(2)
It works, but on windows 10, if the frame is maximized it still can be dragged off by the top of the screen and changes size. Then it can't be maximized again. Maybe you know a good solution for this?Rotow
It is only not resizable by user (code). It still can be resized by some internal event, unfortunately.Legate
S
16

Use setResizable on your JFrame

yourFrame.setResizable(false);

But extending JFrame is generally a bad idea.

Suspensory answered 3/8, 2013 at 10:13 Comment(10)
-1 extending JFrame is not a bad idea. I do it all the time. But anyway, it is a matter of preference, not the officially promulgated fact that extending JFrame is a bad idea.Marelda
This is generally bad design. If you do not extend JFrame features (which is generally the case), having a JFrame property in a class is better design.Suspensory
I just think it should be personal taste, not a de jure standard.Marelda
@tbodt: When you extend a JFrame, your methods are in the same list as the JFrame methods. When you use a JFrame, your methods are separate from the JFrame methods. You should only extend any class if you're going to override one or more of the class methods. Composition over inheritance.Wertz
Design patterns, pesign datterns. Who cares, unless it really matters, which it doesn't usually?Marelda
And anyway, my answer got accepted. Not yours. I wonder if that means something.Marelda
Yeah. That means that your answer got accepted. And by the way, design patterns matter.Suspensory
I am still in learning process and this code is for practice. I know inheritence and composition well, and in projects I will take care of such things.Accompany
Just use JFrame, not extend it if you don't want to change the default behaviour. Simple enough.Unscrupulous
I agree, JFrame and others shouldn't be extended. It means that your own class also inherits all the public members declared in JFrame as well, with methods you rarely ever use.. It just bloats your own class.Sybaris
K
11

Simply write one line in the constructor:

setResizable(false);

This will make it impossible to resize the frame.

Karlotta answered 3/8, 2013 at 10:21 Comment(0)
A
8

This Code May be Help you : [ Both maximizing and preventing resizing on a JFrame ]

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setResizable(false);
Adjacency answered 3/8, 2013 at 10:16 Comment(1)
Hi. thanks. but setExtendedState(JFrame.MAXIMIZED_BOTH); is not very good idea (one can drag frame and it will be resized!). instead it's better to write: setSize( Toolkit.getDefaultToolkit().getScreenSize());Comeaux
A
7

it's easy to use:

frame.setResizable(false);
Arliearliene answered 3/8, 2013 at 10:14 Comment(0)
S
7

Just in case somebody didn't understand the 6th time around:

setResizable(false);
Subscript answered 7/12, 2016 at 16:16 Comment(0)
L
5

You can use this.setResizable(false); or frameObject.setResizable(false);

Legitimist answered 3/8, 2013 at 18:19 Comment(0)
D
1

If you are defining class like this

className extend JFrame{}

Use this code

this.setResizable(false);

or

setResizable(false);
Dehypnotize answered 26/10, 2021 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.