IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Agents de placement/Fenêtres Java Discussion :

Fermer une JDialog avec un JButton


Sujet :

Agents de placement/Fenêtres Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    14
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Mai 2009
    Messages : 14
    Par défaut Fermer une JDialog avec un JButton
    Bonjour,

    Il s'agit peut être d'une question anodine, mais je n'ai pas trouvé la solution.

    Avant de balancer le code voici ce que je voudrai faire:

    J'ai crée un JDialog contenant un JButton Ok, sur lequel j'ai mis un event. L'event se trouve dans une autre classe comme préconiser dans le tutoriel de Baptiste Wicht (le tuto ici).
    Pour le moment j'y ai mis qu'une instruction (fait un beep). Le beep fonctionne, preuve que ce n'est pas l'event qui ne fonctionne pas.
    Je voudrai qu'en appuyant sur le bouton ok la JDialog se ferme...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    package filrouge;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class c_EventAPopos_but_close extends AbstractAction{
     
    	public cAPropos pan;
    	public c_EventAPopos_but_close (String titre){
    		super(titre);
    	}
    	public  void actionPerformed(ActionEvent e){
    		java.awt.Toolkit.getDefaultToolkit().beep();
    	}
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
     
    package filrouge;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class cAPropos extends JFrame {
    	public cAPropos(){
    		build();
    	}
    	private void build(){
     
    		JDialog dialog = new JDialog();
    /* --------------------------------------------------------------------------/
     *	CONTENU
    --------------------------------------------------------------------------- */
    // FOND
    		JPanel background = new JPanel();
    		dialog.add(background);
     
    		background.setLayout(new BoxLayout(background, BoxLayout.Y_AXIS));
     
    // FOND LOGO INFORMATION
    		JPanel pan_logo_info = new JPanel();
    		background.add(pan_logo_info);
     
    // PANEL LOGO
    		JPanel pan_logo = new JPanel();
    		pan_logo_info.add(pan_logo);
     
    // LOGO
    		JLabel lab_logo = new JLabel();		
    		ImageIcon img_logo = new ImageIcon("img/DukeWave.gif");
    		lab_logo.setIcon(img_logo);
    		pan_logo.add(lab_logo);
     
    // PANEL INFORMATIONS
    		JPanel pan_info = new JPanel();
    		pan_logo_info.add(pan_info);
     
    		pan_info.setLayout(new GridLayout(5, 2)); 
     
    // INFORMATIONS
    		JLabel lab_product_a = new JLabel("product :");
    		pan_info.add(lab_product_a);
    		JLabel lab_product_b = new JLabel("Un Air de Java");
    		pan_info.add(lab_product_b);
     
     
    		JLabel lab_version_a = new JLabel("version :");
    		pan_info.add(lab_version_a);
    		JLabel lab_version_b = new JLabel("v0.1a");
    		pan_info.add(lab_version_b);
     
    		JLabel lab_date_a = new JLabel("Date creation : ");
    		pan_info.add(lab_date_a);
    		JLabel lab_date_b = new JLabel("03/09/2009");
    		pan_info.add(lab_date_b);
     
    		JLabel lab_date_maj_a = new JLabel("Maj : ");
    		pan_info.add(lab_date_maj_a);
    		JLabel lab_date_maj_b = new JLabel("03/09/2009");
    		pan_info.add(lab_date_maj_b);
     
    		JLabel lab_author_a = new JLabel("Author : ");
    		pan_info.add(lab_author_a);
    		JLabel lab_author_b = new JLabel("VAN HOVE David");
    		pan_info.add(lab_author_b);
     
    // PANEL BOUTON OK
    		JPanel pan_ok = new JPanel();
    		background.add(pan_ok);
    			c_EventAPopos_but_close evt = new c_EventAPopos_but_close("ok");
    			evt.pan = this;
    			JButton but_ok = new JButton(evt);
    			pan_ok.add(but_ok);
    			pan_ok.setLayout(new FlowLayout(FlowLayout.LEFT));
     
     
     
    /* --------------------------------------------------------------------------/
     *	CARACTERISTIQUE DE LA FENETRE
    --------------------------------------------------------------------------- */
    		dialog.setVisible(true);
    		dialog.setSize(200, 200);
    		dialog.setResizable(false);
    		dialog.setDefaultCloseOperation(dialog.DISPOSE_ON_CLOSE);
    		dialog.pack(); 
    	}
     
    }

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 277
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 277
    Par défaut
    Tu n'as qu'à passer en paramètre du constructeur de ton action, une référence à ton JDialog.
    Comme ça, dans la méthode actionPerformed, tu pourras utiliser cette référence pour fermer ton JDialog.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    14
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Mai 2009
    Messages : 14
    Par défaut
    Citation Envoyé par fr1man Voir le message
    Tu n'as qu'à passer en paramètre du constructeur de ton action, une référence à ton JDialog.
    Comme ça, dans la méthode actionPerformed, tu pourras utiliser cette référence pour fermer ton JDialog.
    Ca doit être moi ou la fatigue, mais je ne comprend toujours pas.

  4. #4
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 277
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 277
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    public class c_EventAPopos_but_close extends AbstractAction{
    	
    	public cAPropos pan;
    	public c_EventAPopos_but_close (String titre, JDialog pan){
    		super(titre);
                    this.pan = pan;
    	}
    	public  void actionPerformed(ActionEvent e){
    		java.awt.Toolkit.getDefaultToolkit().beep();
                    pan.setVisible(false);
                    //ou
                    pan.dispose();
    	}
     
    }
    
    et dans ta classe cApropos:
    
    JPanel pan_ok = new JPanel();
    background.add(pan_ok);
    c_EventAPopos_but_close evt = new c_EventAPopos_but_close("ok", dialog);
    evt.pan = this;
    JButton but_ok = new JButton(evt);
    pan_ok.add(but_ok);
    pan_ok.setLayout(new FlowLayout(FlowLayout.LEFT));

  5. #5
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Salut,


    On peut également utiliser SwingUtilities pour récupérer la fenêtre contenant le composant à l'origine de l'évènement :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    	public  void actionPerformed(ActionEvent e){
    		java.awt.Toolkit.getDefaultToolkit().beep();
    		Window window = SwingUtilities.windowForComponent((Component)e.getSource());
    		window.dispose();
    	}
    a++

    PS : C'est quoi ces noms de classes bizarroïdes

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    14
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Mai 2009
    Messages : 14
    Par défaut
    C'est bon, c'est résolu!
    Un peu long à la détente, mais ça fonctionne.
    Voici les codes sans les fautes honteuses que contenaient le premier Post:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
     
    package filrouge;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class cAPropos extends JDialog {
    	public cAPropos(){
    		build();
    	}
    	private void build(){
     
    		JDialog dialog = new JDialog();
    /* --------------------------------------------------------------------------/
     *	CONTENU
    --------------------------------------------------------------------------- */
    // FOND
    		JPanel background = new JPanel();
    		dialog.add(background);
     
    		background.setLayout(new BoxLayout(background, BoxLayout.Y_AXIS));
     
    // FOND LOGO INFORMATION
    		JPanel pan_logo_info = new JPanel();
    		background.add(pan_logo_info);
     
    // PANEL LOGO
    		JPanel pan_logo = new JPanel();
    		pan_logo_info.add(pan_logo);
     
    // LOGO
    		JLabel lab_logo = new JLabel();		
    		ImageIcon img_logo = new ImageIcon("img/DukeWave.gif");
    		lab_logo.setIcon(img_logo);
    		pan_logo.add(lab_logo);
     
    // PANEL INFORMATIONS
    		JPanel pan_info = new JPanel();
    		pan_logo_info.add(pan_info);
     
    		pan_info.setLayout(new GridLayout(5, 2)); 
     
    // INFORMATIONS
    		JLabel lab_product_a = new JLabel("product :");
    		pan_info.add(lab_product_a);
    		JLabel lab_product_b = new JLabel("Un Air de Java");
    		pan_info.add(lab_product_b);
     
     
    		JLabel lab_version_a = new JLabel("version :");
    		pan_info.add(lab_version_a);
    		JLabel lab_version_b = new JLabel("v0.1a");
    		pan_info.add(lab_version_b);
     
    		JLabel lab_date_a = new JLabel("Date creation : ");
    		pan_info.add(lab_date_a);
    		JLabel lab_date_b = new JLabel("03/09/2009");
    		pan_info.add(lab_date_b);
     
    		JLabel lab_date_maj_a = new JLabel("Maj : ");
    		pan_info.add(lab_date_maj_a);
    		JLabel lab_date_maj_b = new JLabel("03/09/2009");
    		pan_info.add(lab_date_maj_b);
     
    		JLabel lab_author_a = new JLabel("Author : ");
    		pan_info.add(lab_author_a);
    		JLabel lab_author_b = new JLabel("VAN HOVE David");
    		pan_info.add(lab_author_b);
     
    // PANEL BOUTON OK
    		JPanel pan_ok = new JPanel();
    		background.add(pan_ok);
    			JButton but_ok = new JButton(new c_EventAPopos_but_close(this,"ok"));
    			pan_ok.add(but_ok);
    			pan_ok.setLayout(new FlowLayout(FlowLayout.LEFT));
     
     
     
    /* --------------------------------------------------------------------------/
     *	CARACTERISTIQUE DE LA FENETRE
    --------------------------------------------------------------------------- */
    		dialog.setVisible(true);
    		dialog.setSize(200, 200);
    		dialog.setResizable(false);
    		dialog.setDefaultCloseOperation(dialog.DISPOSE_ON_CLOSE);
    		dialog.pack(); 
    	}
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    package filrouge;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class c_EventAPopos_but_close extends AbstractAction{
     
    	private cAPropos dialog;
     
    	public c_EventAPopos_but_close (cAPropos dialog,String titre){
    		super(titre);
    		this.dialog = dialog;
    	}
    	public  void actionPerformed(ActionEvent e){
    		java.awt.Toolkit.getDefaultToolkit().beep();
    		dialog.getParent().setVisible(false);
    	}
     
    }
    Un grand merci à fr1man tout de même pour l'astuce.

  7. #7
    Membre confirmé

    Inscrit en
    Février 2008
    Messages
    36
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Février 2008
    Messages : 36
    Par défaut close JDialog
    salut,

    Pour fermer le JDialog il suffit d'appeler la méthode setVisisble(false);

    Dans votre cas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     
              pan.setVisible(false);
              pan.dispose();//Pour libérer les ressources.
    Faire attention l'objet pan doit être la boîte de dialogue à fermer //C'est logique.
    Si ce n'est pas claire envoyer la classe où vous avez créer l'objet pan;

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fermer une popup avec onclick
    Par griese dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 06/07/2006, 17h27
  2. fermer une application avec la croix
    Par nemo69500 dans le forum C++Builder
    Réponses: 10
    Dernier message: 19/06/2006, 15h56
  3. Fermer une application avec ShellExecute
    Par astano dans le forum Langage
    Réponses: 4
    Dernier message: 20/02/2006, 20h07
  4. Fermer une page avec plusieur cadre
    Par 12_darte_12 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 22/11/2005, 12h29
  5. [VB6]Fermer une application avec VB
    Par Mylou dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 04/04/2003, 21h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo