47

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();

}

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Junejo
  • 717
  • 1
  • 7
  • 14
  • please why 1. setBound(), 2. non_resizeable 3. setVisible(true); must be last code line in contructor – mKorbel Aug 03 '13 at 10:13
  • 2
    Have thought about using [**LayoutManagers**](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)? 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. – Azad Aug 03 '13 at 10:25

8 Answers8

124

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

setResizable(false);

After this call, the window will not be resizable.

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • 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? – luke1985 Jun 04 '20 at 10:23
  • It is only not resizable by user (code). It still can be resized by some internal event, unfortunately. – Martin Jun 07 '23 at 19:52
16

Use setResizable on your JFrame

yourFrame.setResizable(false);

But extending JFrame is generally a bad idea.

mael
  • 2,214
  • 1
  • 19
  • 20
  • 1
    -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. – tbodt Aug 03 '13 at 10:19
  • 2
    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. – mael Aug 03 '13 at 10:23
  • I just think it should be personal taste, not a de jure standard. – tbodt Aug 03 '13 at 10:24
  • 5
    @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. – Gilbert Le Blanc Aug 03 '13 at 10:28
  • Design patterns, pesign datterns. Who cares, unless it really matters, which it doesn't usually? – tbodt Aug 03 '13 at 10:29
  • And anyway, my answer got accepted. Not yours. I wonder if that means something. – tbodt Aug 03 '13 at 10:29
  • 3
    Yeah. That means that your answer got accepted. And by the way, design patterns matter. – mael Aug 03 '13 at 10:32
  • 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. – Junejo Aug 03 '13 at 10:34
  • Just use `JFrame`, not extend it if you don't want to change the default behaviour. Simple enough. – WesternGun Feb 08 '17 at 11:32
  • 1
    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. – Dorian Gray Aug 24 '17 at 15:25
11

Simply write one line in the constructor:

setResizable(false);

This will make it impossible to resize the frame.

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38
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);
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
  • 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());` – Mohammad_Hosein Aug 17 '22 at 17:53
7

it's easy to use:

frame.setResizable(false);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Caffe Latte
  • 1,693
  • 1
  • 14
  • 32
7

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

setResizable(false);
Yaroslav
  • 4,543
  • 5
  • 26
  • 36
5

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

Deepak S. Gavkar
  • 447
  • 8
  • 21
1

If you are defining class like this

className extend JFrame{}

Use this code

this.setResizable(false);

or

setResizable(false);
Nethmal
  • 11
  • 3