Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Problems with setDefaultCloseOperation()

  1. #1
    chittolina is offline Member
    Join Date
    Apr 2012
    Location
    Florianópolis, Brazil
    Posts
    1
    Rep Power
    0

    Default Problems with setDefaultCloseOperation()

    Hey guys. I need to organize my panels. I want to when i hit the button "Cadastrar nova conta", my PainelInicial screen close automatically. I wanted to use the method setDefaultCloseOperation, but my events are an a inner class, so i don't know how to do it.

    PS: Sorry about my bad english and don't worry about my classes names and method names.

    I hope you guys can help me.



    Here is my code:
    Java Code:
    public class PainelInicial extends JFrame {
        
        
        
        
        public PainelInicial() {
            super("Banco IFSC");
            this.setResizable(false);
            this.setSize(350,70);
            add(montaPainelInicial());
            
            
            
        }
        
        
        
        public JPanel montaPainelInicial() {
            JPanel p = new JPanel();
            JButton bAcessa = new JButton("Acessar sua conta");
            JButton bCadastra = new JButton("Cadastrar nova conta");
            bCadastra.addActionListener(new CadastraConta());
            p.add(bAcessa);
            p.add(bCadastra);
            
            
            
            return p;
        }
        
        public static void main(String [] args) {
            PainelInicial p = new PainelInicial();
            p.show();
            
        }
        
        
        
    }
    
    
        class CadastraConta implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            PainelCadastro p = new PainelCadastro();
            p.show();
          
             
        }
            
            
            
            
        }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Problems with setDefaultCloseOperation()

    Here's some code I just copied out of one of my classes that extends JFrame:

    Java Code:
    		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		addWindowListener(new WindowAdapter() {
    			@Override
    			public void windowClosing(WindowEvent e) {
    				MainWindow.this.weapon.shutdown();
    			}
    		});
    This disables the default close operation and replaces it with an anonymous event handler that calls my application's shutdown method. The shutdown method closes any open resources and then calls System.exit().

    You can do the same thing in your button's action listener. If you do not want the app to exit when you click the window manager's close button, don't use the WindowListener like I did.
    Last edited by kjkrum; 04-18-2012 at 04:44 AM.
    chittolina likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 2
    Last Post: 11-09-2011, 03:50 AM
  2. Need help with setDefaultCloseOperation
    By noneofthem in forum New To Java
    Replies: 1
    Last Post: 02-23-2011, 12:03 PM
  3. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  4. many to many problems
    By cecily in forum JDBC
    Replies: 1
    Last Post: 08-02-2007, 05:51 PM
  5. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 PM

Tags for this Thread

Posting Permissions

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