Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mmmdreg

macrumors 65816
Original poster
Apr 14, 2002
1,393
0
Sydney, Australia
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?
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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() ; 
   } 
 }
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.