[java] 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();

}

}

This question is related to java swing jframe resizable

The answer is


Simply write one line in the constructor:

setResizable(false);

This will make it impossible to resize the frame.


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

setResizable(false);

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

Use setResizable on your JFrame

yourFrame.setResizable(false);

But extending JFrame is generally a bad idea.


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


it's easy to use:

frame.setResizable(false);

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to jframe

JFrame background image JFrame: How to disable window resizing? Adding image to JFrame How to make a JFrame button open another JFrame class in Netbeans? How to layout multiple panels on a jFrame? (java) How to add text to JFrame? JFrame.dispose() vs System.exit() JPanel vs JFrame in Java Adding JPanel to JFrame Limiting the number of characters in a JTextField

Examples related to resizable

JFrame: How to disable window resizing? How to make a stable two column layout in HTML/CSS