Junejo
Junejo

Reputation: 747

JFrame: How to disable window resizing?

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

}

}

Upvotes: 50

Views: 155087

Answers (8)

Nethmal
Nethmal

Reputation: 11

If you are defining class like this

className extend JFrame{}

Use this code

this.setResizable(false);

or

setResizable(false);

Upvotes: 1

Caffe Latte
Caffe Latte

Reputation: 1743

it's easy to use:

frame.setResizable(false);

Upvotes: 7

Yaroslav
Yaroslav

Reputation: 4659

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

setResizable(false);

Upvotes: 7

tbodt
tbodt

Reputation: 16987

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

setResizable(false);

After this call, the window will not be resizable.

Upvotes: 128

Deepak S. Gavkar
Deepak S. Gavkar

Reputation: 457

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

Upvotes: 5

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

Simply write one line in the constructor:

setResizable(false);

This will make it impossible to resize the frame.

Upvotes: 11

Butani Vijay
Butani Vijay

Reputation: 4239

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

Upvotes: 8

mael
mael

Reputation: 2254

Use setResizable on your JFrame

yourFrame.setResizable(false);

But extending JFrame is generally a bad idea.

Upvotes: 16

Related Questions