dismiss modal JDialog

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpenguin
    New Member
    • Aug 2007
    • 41

    dismiss modal JDialog

    I am coding a reversi game as a java applet. I am using modal dialogs to inform the user if they beet the computer. I can close the dialog by the window decorations 'close', I want to hide the decoration and have my own close button-

    Code:
    import java.applet.*;
    import java.lang.*;
    import javax.swing.*;
    import java.awt.*;
    import java.net.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    Code:
    	private Frame findParentFrame(){ 
        	Container c = this; 
        	while(c != null){ 
          		if (c instanceof Frame) 
            		return (Frame)c; 
    
          	c = c.getParent(); 
        	} 
        return (Frame)null; 
      }
    Code:
    public void endGame(){
    		Frame f = findParentFrame(); 
    		final JDialog dialog = new JDialog(f, "Game Over", true);
    			JButton close = new JButton("Play Again");
    		if (counter_black > counter_white) {
    			dialog.setLayout(new GridLayout(2, 1));
    			dialog.add( new Label("You\nWon!") );
    			dialog.add( close );
    			dialog.pack();
    		} else if (counter_black < counter_white) {
    			dialog.setLayout(new GridLayout(2, 1));
    			dialog.add( new Label("You\nLost!") );
    			dialog.add( close );
    			dialog.pack();
    		} else {
    			dialog.setLayout(new GridLayout(2, 1));
    			dialog.add( new Label("Draw!") );
    			dialog.add( close );
    			dialog.pack();
    		} dialog.setLocationRelativeTo(null);
    		dialog.setVisible(true);
    		repaint();
    		close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);            }
            });
       	}
    The dialog displays correctly, but doesn't close with the button
  • jpenguin
    New Member
    • Aug 2007
    • 41

    #2
    :solved:

    Code:
    	public void endGame(){
    		Frame f = findParentFrame(); 
    		final JDialog dialog = new JDialog(f, "Game Over", true);
    			JButton close = new JButton("Play Again");
    		if (counter_black > counter_white) {
    			dialog.setLayout(new GridLayout(2, 1));
    			dialog.add( new Label("You\nWon!") );
    			dialog.add( close );
    			//dialog.pack();
    		} else if (counter_black < counter_white) {
    			dialog.setLayout(new GridLayout(2, 1));
    			dialog.add( new Label("You\nLost!") );
    			dialog.add( close );
    			//dialog.pack();
    		} else {
    			dialog.setLayout(new GridLayout(2, 1));
    			dialog.add( new Label("Draw!") );
    			dialog.add( close );
    			//dialog.pack();
    		} dialog.setLocationRelativeTo(null);
    		close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();            }
            });
            dialog.pack();
    		dialog.setVisible(true);
    		repaint();
    		
       	}

    Comment

    Working...