Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default remove close button

    Is there any way to remove the 'X' close button from JDialog
    please help me i am stucked

  2. #2
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    You can't remove it but you can make it unresponsive by using:

    Java Code:
    dialog.setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE );

  3. #3
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    i read on a thread that the way to do this is to remove the title bar and adding your own title bar
    now the whole title bar can be removed by
    Java Code:
    setUndecorated(true)
    but i dont know how to set my own title bar

    i also tried the following code
    Java Code:
    public static void removeCloseButton(Component comp) {
        if (comp instanceof JMenu) {
          Component[] children = ((JMenu) comp).getMenuComponents();
          for (int i = 0; i < children.length; ++i)
            removeCloseButton(children[i]);
        }
        else if (comp instanceof AbstractButton) {
          Action action = ((AbstractButton) comp).getAction();
          String cmd = (action == null) ? "" : action.toString();
          if (cmd.contains("CloseAction")) {
            comp.getParent().remove(comp);
          }
        }
        else if (comp instanceof Container) {
          Component[] children = ((Container) comp).getComponents();
          for (int i = 0; i < children.length; ++i)
            removeCloseButton(children[i]);
        }
      }
    but nothing worked out
    Last edited by silversurfer2in; 05-29-2010 at 07:14 PM.

  4. #4
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    to remove the X you firstly need to set java's L&F
    JDialog.setDefaultLookAndFeelDecorated(true);

    setting it 'before' the dialog is created.
    then your removeCloseButton(..) works OK,
    but java's L&F isn't overly popular

  5. #5
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Quote Originally Posted by Michael Dunn View Post
    to remove the X you firstly need to set java's L&F
    JDialog.setDefaultLookAndFeelDecorated(true);

    setting it 'before' the dialog is created.
    then your removeCloseButton(..) works OK,
    but java's L&F isn't overly popular
    I tried it but its not working...
    i am creating JDialog in a mouse listener event
    my code goes like this
    Java Code:
    MouseListener mouseListener = new MouseAdapter() {
    		
    		 JDialog d = new JDialog(frame, "Human Resource");
    		 
    		   public void mouseEntered(MouseEvent me) {
    			   JDialog.setDefaultLookAndFeelDecorated(true);
    			   d.setSize(200,200);
    			   
    
    		        d.setLocation(me.getX(),me.getY());
    		        d.setVisible(true);
    		        removeCloseButton(d);
    
    		   }
    
    		   public void mouseExited(MouseEvent me) {
    			   d.setVisible(false);
    			
    		   }
    	 };
    	 public static void removeCloseButton(Component comp) {
    		    if (comp instanceof JMenu) {
    		      Component[] children = ((JMenu) comp).getMenuComponents();
    		      for (int i = 0; i < children.length; ++i)
    		        removeCloseButton(children[i]);
    		    }
    		    else if (comp instanceof AbstractButton) {
    		      Action action = ((AbstractButton) comp).getAction();
    		      String cmd = (action == null) ? "" : action.toString();
    		      if (cmd.contains("CloseAction")) {
    		        comp.getParent().remove(comp);
    		      }
    		    }
    		    else if (comp instanceof Container) {
    		      Component[] children = ((Container) comp).getComponents();
    		      for (int i = 0; i < children.length; ++i)
    		        removeCloseButton(children[i]);
    		    }
    		  }
    please check this and tell me

  6. #6
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > I tried it but its not working...

    You didn't try it correctly.

    noted previously:
    "setting it 'before' the dialog is created."

    in your code you create the dialog in the class,
    then you set the default L&F in mouseEntered

  7. #7
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Quote Originally Posted by Michael Dunn View Post
    > I tried it but its not working...

    You didn't try it correctly.

    noted previously:
    "setting it 'before' the dialog is created."

    in your code you create the dialog in the class,
    then you set the default L&F in mouseEntered
    what should be the look and feel... i have changed it to window feel
    Java Code:
    try {
        	    // Set L&F to window
        		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    
            } 
            catch (UnsupportedLookAndFeelException e) {
               // handle exception
            }
            catch (ClassNotFoundException e) {
               // handle exception
            }
            catch (InstantiationException e) {
               // handle exception
            }
            catch (IllegalAccessException e) {
               // handle exception
            }
    rest is same...and still its not working...please tell me...i will be very grateful
    Last edited by silversurfer2in; 05-30-2010 at 05:52 PM.

  8. #8
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    here's a working example

    do NOT add any additional code to it, just copy/paste/compile/run,
    and see that it removes the 'X', then play around with it.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Testing
    {
      public void buildGUI()
      {
        JDialog.setDefaultLookAndFeelDecorated(true);
        JDialog d = new JDialog();
        d.setModal(true);
        removeCloseButton(d);
        JPanel p = new JPanel(new GridBagLayout());
        JButton btn = new JButton("Exit");
        p.add(btn,new GridBagConstraints());
        d.getContentPane().add(p);
        d.setSize(400,300);
        d.setLocationRelativeTo(null);
        btn.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            System.exit(0);
          }
        });
        d.setVisible(true);
      }
    
      public void removeCloseButton(Component comp) {
        if (comp instanceof JMenu) {
          Component[] children = ((JMenu) comp).getMenuComponents();
          for (int i = 0; i < children.length; ++i)
            removeCloseButton(children[i]);
        }
        else if (comp instanceof AbstractButton) {
          Action action = ((AbstractButton) comp).getAction();
          String cmd = (action == null) ? "" : action.toString();
          if (cmd.contains("CloseAction")) {
            comp.getParent().remove(comp);
          }
        }
        else if (comp instanceof Container) {
          Component[] children = ((Container) comp).getComponents();
          for (int i = 0; i < children.length; ++i)
            removeCloseButton(children[i]);
        }
      }
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
          }
        });
      }
    }

  9. #9
    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    0

    Default

    Thanx a lot

  10. #10
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: remove close button

    Quote Originally Posted by Michael Dunn View Post
    here's a working example

    do NOT add any additional code to it, just copy/paste/compile/run,
    and see that it removes the 'X', then play around with it.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Testing
    {
      public void buildGUI()
      {
        JDialog.setDefaultLookAndFeelDecorated(true);
        JDialog d = new JDialog();
        d.setModal(true);
        removeCloseButton(d);
        JPanel p = new JPanel(new GridBagLayout());
        JButton btn = new JButton("Exit");
        p.add(btn,new GridBagConstraints());
        d.getContentPane().add(p);
        d.setSize(400,300);
        d.setLocationRelativeTo(null);
        btn.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae){
            System.exit(0);
          }
        });
        d.setVisible(true);
      }
    
      public void removeCloseButton(Component comp) {
        if (comp instanceof JMenu) {
          Component[] children = ((JMenu) comp).getMenuComponents();
          for (int i = 0; i < children.length; ++i)
            removeCloseButton(children[i]);
        }
        else if (comp instanceof AbstractButton) {
          Action action = ((AbstractButton) comp).getAction();
          String cmd = (action == null) ? "" : action.toString();
          if (cmd.contains("CloseAction")) {
            comp.getParent().remove(comp);
          }
        }
        else if (comp instanceof Container) {
          Component[] children = ((Container) comp).getComponents();
          for (int i = 0; i < children.length; ++i)
            removeCloseButton(children[i]);
        }
      }
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
          }
        });
      }
    }
    Hi this is what i was looking for so thank you all i need do now is tweek it to fit my app
    thanks pete

Similar Threads

  1. Replies: 0
    Last Post: 04-28-2010, 01:39 PM
  2. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  3. How do I close a frame with a button?
    By Psyclone in forum AWT / Swing
    Replies: 7
    Last Post: 02-19-2010, 10:43 PM
  4. Button to remove item from list
    By dacool25 in forum Java Applets
    Replies: 6
    Last Post: 10-14-2009, 04:30 AM
  5. how disable the display of close button on the frame
    By kalanidhi in forum New To Java
    Replies: 6
    Last Post: 11-19-2008, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •