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

}

}
2
  • please why 1. setBound(), 2. non_resizeable 3. setVisible(true); must be last code line in contructor
    – mKorbel
    Commented Aug 3, 2013 at 10:13
  • 2
    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.
    – Azad
    Commented Aug 3, 2013 at 10:25

8 Answers 8

128

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

setResizable(false);

After this call, the window will not be resizable.

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?
    – luke1985
    Commented Jun 4, 2020 at 10:23
  • It is only not resizable by user (code). It still can be resized by some internal event, unfortunately.
    – Martin
    Commented Jun 7, 2023 at 19:52
16

Use setResizable on your JFrame

yourFrame.setResizable(false);

But extending JFrame is generally a bad idea.

10
  • 2
    -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
    Commented Aug 3, 2013 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
    Commented Aug 3, 2013 at 10:23
  • 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. Commented Aug 3, 2013 at 10:28
  • 3
    Yeah. That means that your answer got accepted. And by the way, design patterns matter.
    – mael
    Commented Aug 3, 2013 at 10: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. Commented Aug 24, 2017 at 15:25
11

Simply write one line in the constructor:

setResizable(false);

This will make it impossible to resize the frame.

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);
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()); Commented Aug 17, 2022 at 17:53
7

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

setResizable(false);
7

it's easy to use:

frame.setResizable(false);
5

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

1

If you are defining class like this

className extend JFrame{}

Use this code

this.setResizable(false);

or

setResizable(false);

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