Reputation: 23
My program is about a supermarket. I have created a JButton called b1 in the delivery() method. I want the JFrame window to close when the user presses the button b1. But unfortunately i do not know how to do that. Please help. Below is the delivery() method of my program:
public static void delivery()
{
JFrame f = new JFrame("Name");
f.setVisible(true);
f.setSize(600,200);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocation(700,450);
JPanel p = new JPanel();
final JLabel l = new JLabel("Enter your name: ");
final JTextField jt = new JTextField(20);
JButton b1 = new JButton("Ok");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input = jt.getText();
}
});
p.add(b1);
p.add(l);
p.add(jt);
f.add(p);
String b = JOptionPane.showInputDialog(null, "Please enter your address in one single line:");
JOptionPane.showMessageDialog(null, "The ordered stuff will be delivered to " +input+ " who lives in: " +b , "Delivery" , JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "Thank you for shopping at Paradise 24/7. Hope to see you again." , "Shopping Done!" , JOptionPane.PLAIN_MESSAGE);
}
Upvotes: 1
Views: 219
Reputation: 3652
setVisible() should do the trick for ya
public static void delivery()
{
JFrame f = new JFrame("Name");
f.setVisible(true);
f.setSize(600,200);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocation(700,450);
JPanel p = new JPanel();
final JLabel l = new JLabel("Enter your name: ");
final JTextField jt = new JTextField(20);
JButton b1 = new JButton("Ok");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input = jt.getText();
f.setVisible(false);
}
});
p.add(b1);
p.add(l);
p.add(jt);
f.add(p);
String b = JOptionPane.showInputDialog(null, "Please enter your address in one single line:");
JOptionPane.showMessageDialog(null, "The ordered stuff will be delivered to " +input+ " who lives in: " +b , "Delivery" , JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "Thank you for shopping at Paradise 24/7. Hope to see you again." , "Shopping Done!" , JOptionPane.PLAIN_MESSAGE);
}
Upvotes: 2
Reputation: 109813
with setModal, ModalityTypes to prevent Mouse
and Keyboard
events outside JDialog
if is required
create only one istance of JDialog
as local variable, setDefaultCloseOperations(HIDE_ON_CLOSE), then you'll use setVisible(true/false) for whole apllication life cycle
Upvotes: 7