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

 Java Discussion :

KeyEvent qui ne répond pas


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Par défaut KeyEvent qui ne répond pas
    Salut,

    Problème d'un keyEvent, je dois appuyer entrer pour oui est échappe pour non.
    Mais il ne m'affiche même pas mes System.out.println(...)
    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
     
    public static void closeApplication() {
    	final IhmConverter ihm = IhmConverter.getInstance();
    	final Object[] options = { "Yes, please", "Oops no!" };
     
    	JOptionPane optionPane = new JOptionPane(Final.getMsgExit(), // the message
    			JOptionPane.QUESTION_MESSAGE, // the type of message
    			JOptionPane.YES_NO_OPTION, // the type of dialog box
    			null, // A icon
    			options, // Array String for buttons text
    			options[0] // Where 0 is the index of preset button - one that
    						// will be chosen if we press Enter
    	);
     
    	JDialog dialog = optionPane.createDialog(Final.getMsgExitTitle());
    	dialog.setAlwaysOnTop(true);
    	dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    	dialog.toFront();
    	dialog.setFocusable(true);
    	dialog.setVisible(true);
     
    	dialog.addKeyListener(new KeyListener() {
    		@Override
    		public void keyPressed(KeyEvent e) {
    			int keyEsc = e.getKeyCode();
    			System.out.println(options[0].toString() +" = "+ JOptionPane.YES_OPTION);
    			if (options[0].equals(JOptionPane.YES_OPTION)
    					|| keyEsc == KeyEvent.VK_ENTER) {
    				System.out.println(options[0].toString());
    				e.consume();
    				ihm.setVisible(false);
    				ihm.dispose();
    				System.exit(0);
    			} else if (options[1].equals(JOptionPane.NO_OPTION)
    					|| keyEsc == KeyEvent.VK_ESCAPE) {
    				e.consume();
    				// do nothing
    				System.out.println(keyEsc);
    				System.out.println(options[1].toString());
    			}
    		}
     
    		@Override
    		public void keyReleased(KeyEvent arg0) {
    		}
     
    		@Override
    		public void keyTyped(KeyEvent arg0) {
    		}
    	});
    }
    Quand j'utilise le bouton exit de mon JToolBar pour fermer l'application il ferme la boite de dialogue que je fasse oui ou non ou échappe et, quand je ferme l'application avec Alt+F4 il ferme l'application aussi bien avec le bout oui ou non ou échappe.
    Pourquoi ?

    MERCI D'avance

  2. #2
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Par défaut
    J'ai essayer de faire ça autrement mais j'ai un autre message d'erreur, ce qui est étrange est que ça concerne un autre bouton.
    Voici ma classe ActionEvents qui fait ActionListener et KeyListener :
    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
    package EVENT;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    
    import BROWSE.MyFileChooser;
    import IHM.IhmConverter;
    import IHM.ToolBar;
    
    /**
     * My action event
     * @author t0163126
     */
    public class ActionEvents implements ActionListener, KeyListener   
    {
    	private ToolBar toolBar;
    	private IhmConverter ihm;
    	private UtilityMethods utiMethods = new UtilityMethods();
    	
    	//Constructor for the tool bar
    	public ActionEvents(ToolBar toolBar) {
    		this.toolBar = toolBar;
    	}
    	//Constructor for Ihm
    	public ActionEvents(IhmConverter ihm) {
    		this.ihm = ihm;
    	}
    	//Constructor for Utility methods
    	public ActionEvents(UtilityMethods utiMethods) {
    		this.utiMethods = utiMethods;
    	}
    
    	/**
    	 * Button management 
    	 */
    	public void actionPerformed(ActionEvent e)
    	{
    		//Load button
    		if (e.getSource() == toolBar.getBtnLoad()) btnLoad_click();
    		//Exit button
    		if (e.getSource() == toolBar.getBtnExit()) btnExit_click();
    		//Yes button for the dialog box
    		if (e.getSource() == utiMethods.getBtnYes()) btnYes_click();
    		//No button for the dialog box
    		if (e.getSource() == utiMethods.getBtnNo()) btnNo_click();
    	}
    	
    	//============================================================
    	//Manage all the buttons
    	/**
    	 * Exit the application
    	 */
    	private void btnExit_click()
    	{
    		utiMethods.closeApplication(ihm);
    	}
    	
    	/**
    	 * Load button
    	 */
    	private void btnLoad_click() 
    	{
    		JFileChooser fc = new MyFileChooser();
    
    		//Open the dialog box
    		int returnVal = fc.showOpenDialog(null);
    		if (returnVal == JFileChooser.APPROVE_OPTION) {
    			//if you validate
    			utiMethods.createJTabbed(fc.getSelectedFile());
    			
    		} else if (returnVal == JFileChooser.CANCEL_OPTION) {
    			//if you cancel
    			System.out.println("You chose to cancel this action ");	
    		}
    	}
    	
    	/**
    	 * if you press Yes in the dialog box you quit the application
    	 */
    	private void btnYes_click() {
    		ihm.setVisible(false);
    		ihm.dispose();
    		System.exit(0);
    	}
    	
    	/**
    	 * if you press No in the dialog box you return in the application
    	 */
    	private void btnNo_click() {
    		ihm.dispose();
    	}
    	
    	@Override
    	public void keyPressed(KeyEvent e) {
    		int keyEsc = e.getKeyCode();
    		
    		if (keyEsc == KeyEvent.VK_ENTER) {
    			e.consume();
    			ihm.setVisible(false);
    			ihm.dispose();
    			System.exit(0);
    		} else if (keyEsc == KeyEvent.VK_ESCAPE) {
    			e.consume();
    			// do nothing
    		}
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0) {	
    	}
    
    	@Override
    	public void keyTyped(KeyEvent arg0) {	
    	}
    }
    Le message d'erreur est :
    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
     
    Exception occurred during event dispatching:
    java.lang.NullPointerException
    	at EVENT.ActionEvents.actionPerformed(ActionEvents.java:44)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(Unknown Source)
    	at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
    	at javax.swing.JComponent.processKeyBindings(Unknown Source)
    	at javax.swing.JComponent.processKeyEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.Dialog$1.run(Unknown Source)
    	at java.awt.Dialog$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.awt.Dialog.show(Unknown Source)
    	at java.awt.Component.show(Unknown Source)
    	at java.awt.Component.setVisible(Unknown Source)
    	at java.awt.Window.setVisible(Unknown Source)
    	at java.awt.Dialog.setVisible(Unknown Source)
    	at EVENT.UtilityMethods.closeApplication(UtilityMethods.java:250)
    	at EVENT.ActionWindow.windowClosing(ActionWindow.java:24)
    	at java.awt.Window.processWindowEvent(Unknown Source)
    	at javax.swing.JFrame.processWindowEvent(Unknown Source)
    	at java.awt.Window.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Et celui qui à l'écouteur :
    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
     
    public void closeApplication(final IhmConverter ihm) {
    		btnYes = new JButton();
    		btnYes.addActionListener(new ActionEvents(this));
    		btnYes.setText("Yes, please");
    		btnYes.setToolTipText("Click Yes to quit the application");
    		btnYes.setMnemonic('Y');
     
    		btnNo = new JButton();
    		btnNo.addActionListener(new ActionEvents(this));
    		btnNo.setText("Oops no!");
    		btnNo.setToolTipText("Click No to return in the application");
    		btnNo.setMnemonic('N');
     
    		final Object[] options = { btnYes, btnNo };
     
    		JOptionPane optionPane = new JOptionPane(Final.getMsgExit(), // the message
    				JOptionPane.QUESTION_MESSAGE, // the type of message
    				JOptionPane.YES_NO_OPTION, // the type of dialog box
    				null, // A icon
    				options, // Array String for buttons text
    				options[0] // Where 0 is the index of preset button - one that
    							// will be chosen if we press Enter
    		);
     
    		JDialog dialog = optionPane.createDialog(Final.getMsgExitTitle());
    		dialog.setAlwaysOnTop(true);
    		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    		dialog.toFront();
    		dialog.setFocusable(true);
    		dialog.setVisible(true);
     
    		dialog.addKeyListener(new ActionEvents(ihm));
    	}
    Merci pour vos aides...

  3. #3
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Par défaut
    Après d'autre tests, j'ai mis par exemple les boutons load et exit en commentaire, de plus j'ai supprimer le setVisible(false) et le dispose() pour que les bouton yes et no répondent.
    Et quand j'enlève les commentaires :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Exception occurred during event dispatching:
    java.lang.NullPointerException
    	at EVENT.ActionEvents.actionPerformed(ActionEvents.java:44)
    Que ce passe t-il svp ?

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    le champ toolBar dans ta classe ActionEvents est null, ce qui est normal car tu ne l'initialise jamais.

  5. #5
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Par défaut
    Merci pour ta réponse.

    Pourquoi, car quand je fait
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    btnLoad.addActionListener(new ActionEvents(this));
    Il met la valeur de la classe ToolBar en cours normalement et donc initialisé non ?
    Car quand je fait un new ToolBar dans le actionPerformer(), met boutons réagissent plus

  6. #6
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Par défaut
    Je suis bête le toolbar ne me sert à rien dans mon ActionEvents il me suffit d'utiliser le getToolBar() de mon IhmConverter pour y accéder... Merci bien,
    Mais pourquoi mon KeyEvent ne fonctionne toujours pas maintenant qu'il n'y a plus de nullpointer ?
    Les méthodes qui se trouve aussi dans la classe ActionEvents :
    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
     
    @Override
    	public void keyPressed(KeyEvent e) {
    		int keyEsc = e.getKeyCode();
     
    		if (keyEsc == KeyEvent.VK_ENTER) {
    			e.consume();
    			ihm.setVisible(false);
    			ihm.dispose();
    			System.exit(0);
    			System.out.println("I pressed the Enter key");
    		} else if (keyEsc == KeyEvent.VK_ESCAPE) {
    			e.consume();
    			// do nothing
    			System.out.println("I pressed the Echape key");
    		}
    	}
     
    	@Override
    	public void keyReleased(KeyEvent arg0) {	
    	}
     
    	@Override
    	public void keyTyped(KeyEvent arg0) {	
    	}
    Et l'appel dans un autre classe :
    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
    public void closeApplication() {
    		btnYes = new JButton();
    		btnYes.addActionListener(new ActionEvents(this));
    		btnYes.setText("Yes, please");
    		btnYes.setToolTipText("Click Yes to quit the application");
    		btnYes.setMnemonic('Y');
    		
    		btnNo = new JButton();
    		btnNo.addActionListener(new ActionEvents(this));
    		btnNo.setText("Oops no!");
    		btnNo.setToolTipText("Click No to return in the application");
    		btnNo.setMnemonic('N');
    		
    		final Object[] options = { btnYes, btnNo };
    		
    		optionPane = new JOptionPane(Final.getMsgExit(), // the message
    				JOptionPane.QUESTION_MESSAGE, // the type of message
    				JOptionPane.YES_NO_OPTION, // the type of dialog box
    				null, // A icon
    				options, // Array String for buttons text
    				options[0] // Where 0 is the index of preset button - one that
    							// will be chosen if we press Enter
    		);
    
    		dialog = optionPane.createDialog(Final.getMsgExitTitle());
    		dialog.setAlwaysOnTop(true);
    		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    		dialog.toFront();
    		dialog.setFocusable(true);
    		dialog.setVisible(true);
    		
    		dialog.addKeyListener(new ActionEvents(IhmConverter.getInstance()));
    	}
    Merci

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

Discussions similaires

  1. onclick qui ne répond pas de manière aléatoire
    Par dubitoph dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 31/12/2009, 17h30
  2. [AJAX] Destruction d'objet ajax qui ne répond pas
    Par boubacach dans le forum AJAX
    Réponses: 4
    Dernier message: 27/05/2009, 10h12
  3. Dossier qui "ne répond pas"
    Par Hyoga dans le forum Windows XP
    Réponses: 8
    Dernier message: 09/07/2008, 23h48
  4. C# Tuer une application qui ne réponds pas
    Par mayekeul dans le forum C#
    Réponses: 5
    Dernier message: 11/01/2008, 16h08
  5. [VB.NET]Form qui ne répond pas
    Par Pocel dans le forum Windows Forms
    Réponses: 5
    Dernier message: 17/07/2006, 10h42

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