Results 1 to 6 of 6

Thread: Disabling the Close Button on a JFrame in Java

  1. #1
    Join Date
    Nov 2009
    Posts
    680

    Disabling the Close Button on a JFrame in Java

    Hello, I am creating my project in java programming language and while working on it, I want to remove the close button from some of the Frames so that user can't able to close that window without clicking on the close button. So, I want to create a window which will not have Close button. I am not having any idea about it, but if you are having any idea for it, then please help me to do that.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Disabling the Close Button on a JFrame in Java

    Hey, this is quiet simple thing to achieve in java. You just need to make use of the line of the code in your program and you will able to remove the close button from that window:

    Code:
    JFrame f = new JFrame();
    int set = f.getDefaultCloseOperation(); 
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Disabling the Close Button on a JFrame in Java

    Hello, if you want to remove the close button from your window of the frame then you just need to make use of the code below and get your problem solved. By using the code below you will able to get the window which is not having any type of close button :
    Code:
    setUndecorated(true);  
    getRootPane().setWindowDecorationStyle(JRootPane.NONE);

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Disabling the Close Button on a JFrame in Java

    Hello, I think you need to make use of the code below and then you will able to get the solution for the removing close button on a JFrame in java:
    Code:
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.HeadlessException;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class closebutton extends JFrame 
    {
      public closebutton() throws HeadlessException 
    {
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        JButton b1 = new JButton("Close");
        b1.addActionListener(new ActionListener() 
    {
          public void actionPerformed(ActionEvent e)
     {
            System.exit(0);
          }
        });
    
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.setSize(new Dimension(200, 200));
        this.getContentPane().add(b1);
      }
    
      public static void main(String[] args) 
    {
        new closebutton().setVisible(true);
      }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Disabling the Close Button on a JFrame in Java

    You just need to add the line of code below in your program:
    Code:
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    For getting more knowledge in java you must need to refer the books below and get your problem solved:

    • Java Language Specification
    • Java Native Interface: Programmer's Guide and Specification
    • Core Servlets and JavaServer Pages
    • Thinking in Java
    • Advanced Programming for the Java 2 Platform

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Disabling the Close Button on a JFrame in Java

    Hello, for solving your problem, it is necessary to use one of the following solution code in your program. So, just use one of the below:
    Code:
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    Or you can simply use the code below:
    Code:
    setUndecorated(true);
    getRootPane().setWindowDecorationStyle(JRootPane.NONE);

Similar Threads

  1. Replies: 4
    Last Post: 10-02-2011, 10:48 AM
  2. JAVA: Image and Video in JFrame
    By Zia 7 in forum Software Development
    Replies: 4
    Last Post: 30-08-2010, 07:35 PM
  3. Insert Music in JFrame From Java
    By D_chapple in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 10:49 AM
  4. Java program to center a JFrame on screen
    By KALANI84 in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 05:48 PM
  5. javascript keyevents for disabling ctrl button on a webpage
    By AbrahamL in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 08:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,558,930.84810 seconds with 17 queries