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

AWT/Swing Java Discussion :

bouton swing sur dialog : pb : pas d'action


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    142
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 142
    Par défaut bouton swing sur dialog : pb : pas d'action
    Bonjour.
    J'ai developpé une application qui ouvre une applet et à l'ouverture, une boite de dialogue s'ouvre pour demander quel fichier texte doit etre lu. Pour cela, j'ai créer une boite de dialogue avec trois boutons : 1 pour le fichier texte 1, 1 pour le fichier texte 2 et 1 pour annuler.
    J'ai d'abord créé ma boite de dialogue avec Dialog et awt et ca marche bien.
    Voici le code :

    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
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.io.*;
    import javax.swing.*;
    public class boiteinit extends Dialog 
    {
    	private interfaceapplet app;
    	private Button bnouveau=null;
    	private Button bancien=null;
    	private Button bannule=null;
    	private Label lmessage=null;
    	private Panel p=null;
     
    	public boiteinit (interfaceapplet app,Frame  frame, String titre)
    	{
    		// Appel du constructeur de la classe Dialog en mode modal
    		super (frame, titre, true);
    		this.app=app;
    	}
    	public void initialisation()
    	{
     
     
    		p =new Panel(null);
     
    		bnouveau=new Button("B1");
    		bancien=new Button("B2");
    		bannule=new Button("Annuler");
    		lmessage=new Label("Choix du fichier à ouvrir");
    		p.setSize(600,600);
    		setLayout(null);
    		lmessage.setBounds(200,0,190,20);
    		bnouveau.setBounds(40,30,200,20);
    		bancien.setBounds(250,30,200,20);
    		bannule.setBounds(160,60,200,20);
     
     
    		p.add(bnouveau);
    		p.add(bancien);
    		p.add(lmessage);
    		p.add(bannule);
    	}
    	public void showBoiteinit (Component   parent,
    		String      titre)
    	{               
    		// Recherche du parent qui est un Frame
    		Component frame = parent;
    		while (   frame != null
    			&& !(frame instanceof Frame))
    			frame = frame.getParent ();
     
    		// Création d'une instance de MessageBox
    		setBackground(Color.gray); 
     
    	  boiteinit boite = new boiteinit (app,(Frame)frame, titre);
    	  boite.setSize(520,130);
    	  boite.setLocationRelativeTo(null);
    	  initialisation();
    	  boite.add(p);
    	  boite.setVisible(true);
    	}   
     
     
    	public boolean action (Event event, Object eventArg)
    	{
    		if(event.arg=="B1")
    		{
     
    			app.monFichier="b1.txt";
     
    		}
    		if(event.arg=="B2")
    		{
    			app.monFichier="b2.txt";
     
    		}
    		if(event.arg=="Annuler")
    		{
    			System.exit(0);
    		}
    		// Destruction de la boite de message
    		dispose ();
    		return true;
    	}
     
     
    }
    J'ai voulu passer en swing mes boutons et mes labels pour que ce soit plus joli et la, je n'ai plus d'action sur les boutons. J'ai fait différentes recherches et j'ai trouvés des codes mais maintenant, je passe bien dans le "clic bouton" mais la boite de dialogue ne se ferme jamais et je ne peux jamais revenir sur mon applet principale.
    Voici mon code :

    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
    92
    93
    94
    95
    96
    97
    98
    99
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.io.*;
    import javax.swing.*;
    public class boiteinit extends JDialog 
    {
    	private interfaceapplet app;
    	private JButton bnouveau=null;
    	private JButton bancien=null;
    	private JButton bannule=null;
    	private JLabel lmessage=null;
    	private JPanel p=null;
     
    	public boiteinit (interfaceapplet app,JFrame  frame, String titre)
    	{
    		super (frame, titre,true);
    		this.app=app;
    		p =new JPanel(null);
     
    		bnouveau=new JButton("B1");
    		bancien=new JButton("B2");
    		bannule=new JButton("Annuler");
    		lmessage=new JLabel("Choix du fichier à ouvrir");
    		p.setSize(600,600);
    		setLayout(null);
    		lmessage.setBounds(200,0,190,20);
    		bnouveau.setBounds(40,30,200,20);
    		bancien.setBounds(250,30,200,20);
    		bannule.setBounds(160,60,200,20);
     
    		this.bnouveau.addActionListener(new ActionListener() 
    				{ 
    					public void actionPerformed(ActionEvent e) 
    					{ 
    						nouveaufichier();
    						boiteinit.this.dispose(); 
     
    					} 
    				}); 
    	this.bancien.addActionListener(new ActionListener() 
    				{ 
    					public void actionPerformed(ActionEvent e) 
    					{ 
    						ancienfichier();
    						boiteinit.this.dispose(); 
    					} 
    				});
    	this.bannule.addActionListener(new ActionListener() 
    				{ 
    					public void actionPerformed(ActionEvent e) 
    					{ 
    						System.exit(0);
    						boiteinit.this.dispose(); 
    					} 
    				});
    		p.add(bnouveau);
    		p.add(bancien);
    		p.add(lmessage);
    		p.add(bannule);
    	}
    	public void nouveaufichier()
    	{
    		app.monFichier="b1.txt";
     
    	}
    	public void ancienfichier()
    	{
    		app.monFichier="b2.txt";
     
    	}
    	public void initialisation()
    	{
    		...
     
     
    	}
    	public void showBoiteinit (Component   parent,
    		String      titre)
    	{               
    		// Recherche du parent qui est un Frame
    		Component frame = parent;
    		while (   frame != null
    			&& !(frame instanceof JFrame))
    			frame = frame.getParent ();
     
     
    		// Création d'une instance de MessageBox
    		setBackground(Color.gray); 
     
    	  boiteinit boite = new boiteinit (app,(JFrame)frame, titre);
    	  boite.setSize(520,130);
    	  boite.setLocationRelativeTo(null);
    	  initialisation();
    	  boite.add(p);
    	  boite.setVisible(true);
    	}
    Mon String app.MonFichier se rempli bien mais je ne retrourne pas sur mon applet pour le charger.

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    142
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 142
    Par défaut
    Personne ne voit????

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    142
    Détails du profil
    Informations personnelles :
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 142
    Par défaut
    Bon, j'ai trouvé et ca marche mais j ne comprend pas pourquoi ca marche maintenant et pas avec le code d'avant. SI quelqu'un peut m'expliquer....

    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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
     
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    public final class boiteinit extends JDialog implements ActionListener
    {
    	private interfaceapplet app;
    	private JButton bnouveau=null;
    	private JButton bancien=null;
    	private JButton bannule=null;
    	private JLabel lmessage=null;
    	private JPanel p=null;
     
    	public boiteinit (interfaceapplet app,Frame  frame)
    	{
    		// Appel du constructeur de la classe Dialog en mode modal
    		super (frame, true);
    		this.app=app;
    		this.setTitle("Choix du fichier à ouvrir");
    	    this.setSize(500, 150);
    	    this.setLocationRelativeTo(getParent());
    	    this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    	    this.setContentPane(getJContentPane());
    	    setBackground(Color.gray); 
    	    this.setLocationRelativeTo(null);
    	    initialisation();
    	    this.setVisible(true);
    	}
    	private JPanel getJContentPane() 
    	{
    	    if (p == null) 
    	    {
    	      p = new JPanel();
    	      p.setLayout(null);
    	      p.add(getBoutonNouveau());
    	      p.add(getBoutonAncien());
    	      p.add(getBoutonAnnuler());
    	    }
    	    return p;
    	}
    	private JButton getBoutonAnnuler() 
    	{
    		if (bannule == null) 
    		{
    			bannule = new JButton("Annuler");
    		    bannule.setBounds(160,60,200,20);
    		    bannule.addActionListener(this);
    		}
    	    return bannule;
    	}
     
    	private JButton getBoutonNouveau()
    	{
    	    if (bnouveau == null) 
    	    {
    	    	bnouveau = new JButton("B1");
    		    bnouveau.setBounds(40,30,200,20);
    			bnouveau.addActionListener(this);
    		}
    	    return bnouveau;
    	}
    	private JButton getBoutonAncien() 
    	{
    		if (bancien == null) 
    		{
    			bancien = new JButton("B2");
    			bancien.setBounds(250,30,200,20);
    			bancien.addActionListener(this);
    		}
    		return bancien;
    	}
    	public void initialisation()
    	{
    		...
    	}
     
    	private void close() 
    	{
    	    this.setVisible(false);
    	    this.dispose();
    	}
    	public void actionPerformed(ActionEvent ae) 
    	{
    	    Object source = ae.getSource();
    	    if (source == bnouveau) 
    	    {
    	    	nouveau fichier();
    				close();
    	    }
     
     
     
    	    if (source == bancien) 
    	    {
    	    	ancienfichier();
    		close();
    	    }
     
     
    	    if (source == bannule)
    	    {
    	    	close();
    	    	System.exit(0);
    	    }
    	}
    }

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

Discussions similaires

  1. Enlever le bouton fermer sur dialog
    Par skurty dans le forum jQuery
    Réponses: 2
    Dernier message: 10/05/2010, 09h24
  2. [HTML] Ancre liée à formulaire fonctionne pas avec certains boutons "submit" sur ie6
    Par 12monkeys dans le forum Balisage (X)HTML et validation W3C
    Réponses: 9
    Dernier message: 28/02/2008, 21h30
  3. Mon client SWING ne se lance pas sur toutes les machines
    Par nicdo77 dans le forum AWT/Swing
    Réponses: 8
    Dernier message: 07/12/2007, 09h46
  4. Boutons qui n'exécutent pas l'action
    Par M@t2802 dans le forum Langage
    Réponses: 2
    Dernier message: 08/08/2007, 11h05
  5. Réponses: 2
    Dernier message: 22/07/2007, 21h39

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