Java frame disposing.

Discussion in 'Mac Programming' started by mmmdreg, May 30, 2007.

  1. mmmdreg macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #1
    Hi,

    I have my main class, which creates a window that contains a frame, and that frame contains two panels from two seperate custom classes.

    There is a button in one of the panel classes that wants to create a new "window" and dispose of the one originally created.

    I can't work out how to go about that. Can anyone help?

    Cheers,

    George

    edit: in case that wasn't clear, can a frame be disposed from buttons created in another class?
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    Try this. It might get you closer.

    Todd

    Code:
    import java.awt.* ; 
    import java.awt.event.* ; 
    
    public class MessageDialogDemo extends Frame
    implements ActionListener 
    { 
      Button b ; 
      
      public static void main(String args[])
      { 
        MessageDialogDemo mdd = new MessageDialogDemo() ; 
        mdd.setVisible(true) ; 
        mdd.setSize(200,100) ; 
      } 
      
      MessageDialogDemo()
      { 
        super("Message Dialog Demo"); 
        
        // set layout manager 
        setLayout(new FlowLayout()) ; 
        
        //Create a button 
        b = new Button("Message Dialog") ; 
        b.addActionListener(this) ; 
        add(b) ; 
        
        // Anonymous inner class handles window events 
        addWindowListener(new WindowAdapter()
        { 
          public void windowClosing(WindowEvent we)
          { 
            System.exit(0) ; 
          }
        }) ; 
      } 
    
      public void actionPerformed(ActionEvent ae)
      { 
        String message = "This is a message." ; 
        MessageDialog md = new MessageDialog(this, "Message Dialog", true, message);
        md.show() ; 
       }
     }
     
     class MessageDialog extends Dialog
     implements ActionListener
     {
     Button ok ; 
     
     MessageDialog(Frame parent, String title, boolean mode, String message)
       { 
       super(parent, title, mode) ; 
       
       //create and add "Center" panel 
       Panel pc = new Panel() ; 
       Label label = new Label(message) ; 
       pc.add(label) ; 
       add(pc,"Center") ; 
       
       // create and add a "South" panel 
       Panel ps = new Panel() ; 
       ok = new Button("OK") ; 
       ok.addActionListener(this) ; 
       ps.add(ok) ; 
       add(ps,"South") ; 
       
       //Lay out components and set the intial size of this dialog box 
       pack() ; 
       
       //Anonymous inner class handles window events 
       addWindowListener(new WindowAdapter()  
         { 
         public void windowClosing(WindowEvent we)
           { 
             System.exit(0) ; 
           } 
          }) ; 
       } 
     
       public Insets getInsets() 
         { 
           return new Insets(40,20,20,20) ; 
         } 
     
     public void actionPerformed(ActionEvent ae)
       {  
         dispose() ; 
       } 
     }
     
  3. mmmdreg thread starter macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #3
    Thanks heaps Todd. That was pretty much exactly what I needed -> a windowDeactivated windowListener. All works perfectly now =)
     

Share This Page