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 :

Problème action listener


Sujet :

AWT/Swing Java

  1. #1
    Membre averti
    Avatar de l4r3nZu
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2010
    Messages
    154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2010
    Messages : 154
    Points : 423
    Points
    423
    Par défaut Problème action listener
    Bonjour,

    je rencontre un probleme avec mes evenement souris :s ils ne s'éxécute pas ...

    et aussi j'aimerai connaitre la meilleur methode pour attribuer un identifiant a mon bouton, dans ma classe JButton j'ai trouvé la méthode .setMnemonic.

    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
    100
    101
    102
    103
    104
    105
    106
    107
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Vector;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.event.CaretEvent;
    import javax.swing.event.CaretListener;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.CaretEvent;
    import javax.swing.event.CaretListener;
     
    public class int_fenConfiguration extends JFrame implements ActionListener
    {
    	int JP_fenConfiguration_sizeY=40;
    	configuration CL_config; 
    	JButton JB_add ;
    	JButton JB_del ;
    	JButton JB_set ;
    	JLabel JL_nom;
    	JPanel JP_fenConfiguration;
     
    	int I_ligne;
    	int I_colonne;
    	String S_nomCellule;
     
     
    	public int_fenConfiguration ()
    	{
    		JP_fenConfiguration = new JPanel();
    		JB_add = new JButton(new ImageIcon("img/BT_add.png" )); 
    		JB_del = new JButton(new ImageIcon("img/BT_del.png" )); 
    		JB_set = new JButton(new ImageIcon("img/BT_set.png" )); 
     
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		CL_config = new configuration(); 
     
     
    		CL_config.nom.add("laurent");
    		CL_config.nom.add("laurent");
    		CL_config.nom.add("laurent");
    		CL_config.nom.add("laurent");
    		CL_config.nom.add("laurent");
    		CL_config.nom.add("laurent");
    		CL_config.nom.add("laurent");
    		CL_config.nom.add("laurent");
     
    		for (int i=0 ; i <  CL_config.nom.size() ; i++)
    		{
    			JL_nom = new JLabel(CL_config.nom.elementAt(i).toString());
    			JL_nom.setPreferredSize(new Dimension(100,20));
     
    			JB_set = new JButton(new ImageIcon("img/BT_set.png" ));
    			JB_set.setMnemonic(i);
    			JB_set.addActionListener(this);
     
    			JB_del = new JButton(new ImageIcon("img/BT_del.png" ));
    			JB_del.setMnemonic(i);
    			JB_del.addActionListener(this);
     
    			JP_fenConfiguration.add(JB_set);
    			JP_fenConfiguration.add(JL_nom);
    			JP_fenConfiguration.add(JB_set);
    			JP_fenConfiguration.add(JB_del);
     
    			JP_fenConfiguration_sizeY+=30;
    		};
     
     
    		this.setSize(250, JP_fenConfiguration_sizeY);
    		this.setContentPane(JP_fenConfiguration);
    		this.setResizable(false);
    		this.setVisible(true);
     
     
     
      }
    	 public void actionPerformed(ActionEvent e)
    	    {
    	        if(e.getSource() == JB_set)
    	        {
    				System.out.print("hello");
    	        }
    	    }
    }
    merci d'avance,
    Cordialement.

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    299
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2009
    Messages : 299
    Points : 422
    Points
    422
    Par défaut
    Peut-être cherches-tu quelque chose comme cela ? :
    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
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    import javax.swing.*;
     
    @SuppressWarnings("serial")
    public class Test extends JFrame implements ActionListener {
    	JButton jb_add, jb_del, jb_set;
    	JLabel jl_nom;
    	JPanel jp_fenconfiguration;	
     
    	public Test () {
    		jp_fenconfiguration = new JPanel();
     
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		for (int i=0 ; i <  10 ; i++) {
    			jl_nom = new JLabel("Laurent " + i);
    			jl_nom.setPreferredSize(new Dimension(100,20));
     
    			jb_add = new JButton("+");
    			jb_add.addActionListener(this);
    			jb_add.setActionCommand("add_" + jl_nom.getText());
     
    			jb_set = new JButton("#");
    			jb_set.addActionListener(this);
    			jb_set.setActionCommand("set_" + jl_nom.getText());
     
    			jb_del = new JButton("-");
    			jb_del.addActionListener(this);
    			jb_del.setActionCommand("del_" + jl_nom.getText());
     
    			jp_fenconfiguration.add(jl_nom);
    			jp_fenconfiguration.add(jb_add);
    			jp_fenconfiguration.add(jb_set);
    			jp_fenconfiguration.add(jb_del);
    		};
     
     
    		this.setSize(350, 400);
    		this.setContentPane(jp_fenconfiguration);
    		this.setResizable(false);
    		this.setVisible(true);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		 System.out.println(e.getActionCommand());
    	 }
     
    	 public static void main(String[] args) {
    		new Test();
    	}
    }
    Par contre, je te conseil de revoir ta façon de nommer tes class et tes variables.
    Car cela peut prêter à confusion !
    Par exemple, lorsqu'on lit : "CL_config.nom.add("laurent");", on a l'impression que tu accèdes à un attribut static de ta class CL_config, alors que ce n'est pas ça !
    Voilà le premier lien que google m'a retourné à ce sujet.

  3. #3
    Membre averti
    Avatar de l4r3nZu
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2010
    Messages
    154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2010
    Messages : 154
    Points : 423
    Points
    423
    Par défaut
    Merci ca marche niquel !!!

    J'ai pas mal avancé , je ne sais pas si je m'y suis bien pri pour l'implémentation des evenement souris enfin ca marche c'est presque l'essentiel

    Par contre je suis confronté a un deuxieme probleme,
    je ne sais pas comment réactualiser ma JFrame ou plutot mon JPannel ken je supprime un élément :s

    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
     
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    import javax.swing.*;
     
    public class int_fenConfiguration extends JFrame implements ActionListener {
    	JButton jb_add, jb_del, jb_set;
    	JLabel jl_nom;
    	JPanel jp_fenconfiguration;	
    	int JP_fenConfiguration_sizeY=40;
    	configuration CL_config=new configuration() ;
     
    	public int_fenConfiguration () 
    	{
    		jp_fenconfiguration = new JPanel();
    		this.chargement(jp_fenconfiguration);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
    		this.setSize(350, JP_fenConfiguration_sizeY);
    		this.setContentPane(jp_fenconfiguration);
    		this.setResizable(false);
    		this.setVisible(true);
     
    	};
    	public void chargement (JPanel jpan)
    	{
    		jpan= new  JPanel();
     
    		CL_config = CL_config.LOAD();
     
    		for (int i=0 ; i <  CL_config.nom.size() ; i++)
    		{
    		jl_nom = new JLabel(CL_config.nom.elementAt(i).toString());
    		jl_nom.setPreferredSize(new Dimension(100,20));
     
     
    		jb_add = new JButton("+");
    		jb_add.addActionListener(this);
    		jb_add.setActionCommand(i+"add_" + jl_nom.getText());
    		jb_add.setMnemonic(i);
     
    		jb_set = new JButton("#");
    		jb_set.addActionListener(this);
    		jb_set.setActionCommand(i+"set_" + jl_nom.getText());
    		jb_set.setMnemonic(i);
     
    		jb_del = new JButton("-");
    		jb_del.addActionListener(this);
    		jb_del.setActionCommand(i+"del_" + jl_nom.getText());
    		jb_del.setMnemonic(i);
     
    		jp_fenconfiguration.add(jl_nom);
    		jp_fenconfiguration.add(jb_add);
    		jp_fenconfiguration.add(jb_set);
    		jp_fenconfiguration.add(jb_del);
    		JP_fenConfiguration_sizeY+=30;
    		}
     
    	}
    	public void actionPerformed(ActionEvent e) 
    	{
    		 System.out.println(e.getActionCommand());
     
    		 String clique_nom = e.getActionCommand().substring(4);
    		 String clique_action = e.getActionCommand().substring(0,3);
     
    		 System.out.println(clique_nom);
    		 System.out.println(e.getActionCommand().charAt(1));
    		 if(e.getActionCommand().charAt(1)=='a')
    		 {
    			 System.out.println("add");
     
    		 }
    		 if(e.getActionCommand().charAt(1)=='s')
    		 {
    			 System.out.println("set");
    		 }
    		 if(e.getActionCommand().charAt(1)=='d')
    		 {
    			 System.out.println("del");
    			 int nb = Integer.parseInt(e.getActionCommand().substring(0,1));
    			 System.out.println(nb);
    			 CL_config.nom.remove(nb);
    			 CL_config.SAVE();
    			 this.chargement(jp_fenconfiguration);
     
     
    		 }
    	 }
     
    }

    CLASS CONFIG :

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
     
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Vector;
     
     
    public class configuration implements Serializable  
    {
    	Vector nom;
    	Vector regle;
    	Vector exeption;
     
    	public configuration()
    	{
    		nom = new Vector();
    		regle = new Vector();
    		exeption = new Vector();
    		nom.add("1");
    		regle.add("1");
    		exeption.add("1");
    		nom.add("2");
    		regle.add("2");
    		exeption.add("2");
    		nom.add("3");
    		regle.add("3");
    		exeption.add("3");
    		nom.add("4");
    		regle.add("4");
    		exeption.add("4");
    		nom.add("5");
    		regle.add("5");
    		exeption.add("5");
    		nom.add("6");
    		regle.add("6");
    		exeption.add("6");
    	}
     
    	public void ADD(String name,exeption exep,regle rg)
    	{ 
    		nom.addElement(name);
    		exeption.addElement(exep);
    		regle.addElement(rg);
    	}
    	public void SET(int index,String name,exeption exep,regle rg) 
    	{
    		nom.set(index , name );
    		exeption.set(index,exep);
    		regle.set(index, rg);	
    	}
    	public void DEL(int index)
    	{
    		nom.remove(index);
    		exeption.remove(index);
    		regle.remove(index);
    	}
     
    	public void SAVE()
    	{
    		try 
    		{
     
    			// ouverture d'un flux de sortie vers le fichier "personne.serial"
    			FileOutputStream fos = new FileOutputStream("configuration.dat");
     
    			// création d'un "flux objet" avec le flux fichier
    			ObjectOutputStream oos= new ObjectOutputStream(fos);
    			try {
    				// sérialisation : écriture de l'objet dans le flux de sortie
    				oos.writeObject(this); 
    				// on vide le tampon
    				oos.flush();
    			} finally 
    			{
    				//fermeture des flux
    				try 
    				{
    					oos.close();
    				} finally 
    				{
    					fos.close();
    				}
    			}
    		} catch(IOException ioe) 
    		{
    			ioe.printStackTrace();
    		}
     
     
    	}
    	public configuration LOAD ()
    	{
    		configuration config=null;
    		try {
    			// ouverture d'un flux d'entrée depuis le fichier "personne.serial"
    			FileInputStream fis = new FileInputStream("configuration.dat");
    			// création d'un "flux objet" avec le flux fichier
    			ObjectInputStream ois= new ObjectInputStream(fis);
    			try {	
    				// désérialisation : lecture de l'objet depuis le flux d'entrée
    				config = (configuration) ois.readObject(); 
    			} finally {
    				// on ferme les flux
    				try {
    					ois.close();
    				} finally {
    					fis.close();
    				}
    			}
    		} catch(IOException ioe) {
    			ioe.printStackTrace();
    		} catch(ClassNotFoundException cnfe) {
    			cnfe.printStackTrace();
    		}
    		if(config != null) {
    			System.out.println(config + " a ete deserialise");
    		}
    		return config;
     
     
    	}
     
    }
    En tout cas merci pour tes reponses elle m'ont été super util
    Cordialement.

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    299
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2009
    Messages : 299
    Points : 422
    Points
    422
    Par défaut
    Citation Envoyé par l4r3nZu Voir le message
    Par contre je suis confronté a un deuxieme probleme,
    je ne sais pas comment réactualiser ma JFrame ou plutot mon JPannel ken je supprime un élément :s
    J'avoue ne pas avoir tout lu , mais en général il faut utiliser validate();

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

Discussions similaires

  1. Problème Action Listener
    Par coqalcoolique dans le forum Débuter
    Réponses: 2
    Dernier message: 30/06/2014, 10h41
  2. Problème avec action listener
    Par pinps dans le forum Débuter
    Réponses: 1
    Dernier message: 11/11/2013, 21h39
  3. [JTree] Problème d'action listener
    Par beuhnoix dans le forum Composants
    Réponses: 3
    Dernier message: 26/05/2009, 18h56
  4. problème de listener (oracle 9i)
    Par bouhn dans le forum Oracle
    Réponses: 1
    Dernier message: 23/01/2006, 14h59
  5. [VBA-E] Problème Action "Autofill"
    Par beurnoir dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 02/11/2005, 15h38

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