# Solved: Closing a java message box



## GUSMAN (Dec 19, 2001)

Hi all I have created a simple message box:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent.*;
import java.awt.event.ActionListener.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;

class MessageBox extends JFrame implements ActionListener
{
private JTextArea txtArea;
private JButton OK;



public MessageBox()
{
setSize(300,100);
setTitle("ATTENTION !");
JPanel panel1 = new JPanel();
txtArea = new JTextArea("Incorrect Account Number or Password");
txtArea.setEditable(false);
panel1.add (txtArea);


JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
JButton OK = new JButton("OK");
panel2.add(OK);

getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel2,BorderLayout.CENTER);
getContentPane().add(panel1,BorderLayout.NORTH);
OK.addActionListener(this);
setVisible(true);


}


public void actionPerformed(ActionEvent e)
{
if (e.getSource() == OK)

{
//Method ???????????????
}
}

} 

What do I put in the OK button to simply close the message box when ok is pressed? I cant seem to find any info anywhere.

Much app

Gus


----------



## covert215 (Apr 22, 2006)

for a standard confirmation box, it returns constants like YES_OPTION, NO _OPTION, or CANCEL_OPTION...it could be something like if([MESSAGEBOX NAME] == OK_OPTION) [close method]...either that, or clicking ok should return a value, like 1 or 0....use an if statement to test for the value

is that clear?


----------



## Chicon (Jul 29, 2004)

To close a JFrame, use the method *dispose* .


----------



## GUSMAN (Dec 19, 2001)

Hi 

I tried typing dispose but nothing happens. Do I have to write something before it? I have tried a few things but it does not like it

Cheers both for the replies

Gus


----------



## Chicon (Jul 29, 2004)

```
[SIZE=3]
[B]
if (e.getSource() == OK)
{
    [COLOR=Red]this.dispose();[/COLOR]
}
[/B]
[/SIZE]
```


----------



## GUSMAN (Dec 19, 2001)

Great Stuff

Cheers

Gus


----------

