Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: close JDialog on button click

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy 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.
    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
    Last edited by helloworld922; April 4th, 2010 at 05:49 PM. Reason: please use [code] tags!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  3. The Following User Says Thank You to copeg For This Useful Post:

    Christophe (April 5th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: close JDialog on button click

    Something like this....
    JFrame frame = new JFrame();
    MyPanel panel = new MyPanel(frame);
    JButton close = new JButton(""Close);
    close.addActionListener(pane);
    panel.add(close);
    frame.getContentPane().add(panel);

    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();
            }
        }
    }

  6. The Following User Says Thank You to copeg For This Useful Post:

    Christophe (April 5th, 2010)

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    // inside of CustomerPanel
    private JDialog dialog;
     
    public customerPanel(JDialog dialog)
    {
         this.dialog = dialog;
         // rest of constructor stuff
    }

    // 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

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    Christophe (April 5th, 2010)

Similar Threads

  1. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM
  2. Want to move my button away from center.
    By Fendaril in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2009, 10:05 PM
  3. Click to start and drawString fonts
    By Campos in forum Java Applets
    Replies: 3
    Last Post: July 24th, 2009, 02:24 PM
  4. [SOLVED] Implementing push button
    By IDK12 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 10th, 2009, 10:13 AM
  5. Java program to open and close computer CD/DVD drive
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 28th, 2009, 12:04 PM