close JDialog on button click
Hi,
I am brand new to Java programming, I am trying to implement an actionlistener to a button on a JDialog that closes that same JDialog on a click event.
Below you can find my code so far. My question is: how to close the JDialog frame with a buttonclick, the button is defined in the JPanel.
Code :
public class JAddCustomer extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
public JAddCustomer() {
JPanel cp = new customerPanel();
this.setSize(300, 300);
this.setTitle("Add Customer");
this.setContentPane(cp);
}
}
class customerPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel lblName, lblSurname, lblStreet, lblNr, lblPc, lblCity;
private JTextField txtName, txtSurname, txtStreet, txtNr, txtPc, txtCity;
private JButton btnAdd;
public customerPanel() {
setLayout(null);
Add a = new Add();
lblName = new JLabel("Voornaam: ");
lblName.setBounds(20, 20, 100, 20);
lblName.setHorizontalAlignment(JLabel.RIGHT);
[...]
btnAdd.addActionListener(a);
add(lblName);
[...]
}
class Add implements ActionListener {
public void actionPerformed(ActionEvent e) {
JCustomer customer = new JCustomer(txtName.getText(), txtSurname.getText(), new JAddress(txtStreet.getText(), txtNr.getText(), txtCity.getText(), txtPc.getText()));
System.out.print(customer.toString());
[COLOR="Red"]HERE I WANT TO IMPLEMENT THE CODE TO CLOSE THE JDIALOG[/COLOR]
}
}
}
Thank you in advance,
Christophe
Re: close JDialog on button click
A few ways to do this...a) pass a reference of the JFrame to the Panel and make the appropriate calls b) Using SwingUtilities.getWindowAncestor to retrieve the parent window and make the appropriate calls. The appropriate call to remove the window entirely is dispose(), to hide it would be setVisible(false)
Re: close JDialog on button click
Hi,
thank you for the quick reply. The second method with the SwingUtilities did the trick for me.
Just for the record: I tried to apply your first suggestion but don't know quite how to make that reference, I added "private JDialog dialog = new JAddCustomer();" to the constructor of the panel, and the dialog.dispose() method in the actionlistener. That generated an error though. Can show me an example of how to pass the reference from the dialog to the panel?
As I said, I am quite the newbie here...
Best regards,
Christophe
Re: close JDialog on button click
Something like this....
Code :
JFrame frame = new JFrame();
MyPanel panel = new MyPanel(frame);
JButton close = new JButton(""Close);
close.addActionListener(pane);
panel.add(close);
frame.getContentPane().add(panel);
Code :
public class MyPanel extends JPanel implements ActionListener{
private JFrame frame;
public MyPanel(JFrame frame){
super();
this.frame = frame;
}
public void actionPerformed(ActionEvent e){
if ( e.getActionCommand().equals("Close"){
frame.dispose();
}
}
}
Re: close JDialog on button click
the dialog reference declaration needs to be stored in the class, not the constructor. Simply move it somewhere else not in a method. Also, you shouldn't be declaring a new JDialog, but need to get the reference from the parent dialog. This must be passed to the constructor somehow:
Code :
// inside of CustomerPanel
private JDialog dialog;
public customerPanel(JDialog dialog)
{
this.dialog = dialog;
// rest of constructor stuff
}
Code :
// inside of JAddCustomer
public JAddCustomer() {
JPanel cp = new customerPanel(this);
this.setSize(300, 300);
this.setTitle("Add Customer");
this.setContentPane(cp);
}
edit: Dang, beaten to it :P