Entree = click de la souris

sarici Messages postés 12 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 27 mai 2008 - 26 mai 2008 à 11:50
sarici Messages postés 12 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 27 mai 2008 - 26 mai 2008 à 13:46
Bonjour,
Je voudrais savoir comment on peut faire pour que apres avoir saisie un mot dans un JTextField, si je tape entree il lance l'action d'un bouton!!
Merci...

3 réponses

sarici Messages postés 12 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 27 mai 2008
26 mai 2008 à 11:53
Pour plus de clarté, voici le code en question:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package Rotmentale;


/**
 *
 * @author Isabelle and didier
 */


import javax.swing.*;
import javax.swing.text.* ;
import java.awt.*;
import java.util.*;


public class Fconnexion extends JDialog
{
   
    private JTextField jtextlogin;
    private JPasswordField mdp;
    
    public Fconnexion(JFrame maman,Locale loc,Client appli)
    {
        super(maman,true);
        ResourceBundle res = ResourceBundle.getBundle("Rotmentale.traduction", loc );
 super.setTitle((String)res.getObject("Fconnex_titre"));


 JPanel identifier=new JPanel();
        identifier.setBorder(BorderFactory.createTitledBorder((String)res.getObject("Fconnex_identifier")));
 
        Container top = this.getContentPane();
        top.setLayout(new BorderLayout());
       
        identifier.setLayout(new GridLayout(3,2)) ;
        identifier.add(new JLabel("Login :"));
       
        jtextlogin= new JTextField(20);
        identifier.add(jtextlogin);
        identifier.add(new JLabel((String)res.getObject("motdepasse")));
        mdp = new JPasswordField(20);
        identifier.add(mdp);
       
        JButton btvalideriden=new JButton((String)res.getObject("btConnex"));
        btvalideriden.addActionListener(new ActionConnexion(1,appli,this));
        identifier.add(btvalideriden);
       
        JButton btAnnuler=new JButton((String)res.getObject("btAnnuler"));
        btAnnuler.addActionListener(new ActionConnexion(2,appli,this));
        identifier.add(btAnnuler);
       
        top.add(identifier,BorderLayout.NORTH);
      
    }
   
    public void Demarrer()
    {
        this.pack();
 this.setLocation(400, 300);
 this.setSize(300, 130);
 this.setResizable(false);
 this.setVisible(true);
   
    }
}
0
cs_jojolemariole Messages postés 519 Date d'inscription mercredi 21 mars 2007 Statut Membre Dernière intervention 19 décembre 2016 25
26 mai 2008 à 12:16
Il suffit de faire un addActionListener sur le JTextField, en mettant le même listener que pour le bouton.
Je t'ai fait un exemple avec un JPanel qui contient un champ de text et un bouton et on peut modifier l'actionListener des deux composants en même temps grâce à la méthode setListener(...)

JPanel :
<hr size="2" width="100%" />
import java.awt.BorderLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PanelAction extends JPanel {

    private static final long serialVersionUID = 1L;

    private JTextField text;

    private JButton button;

    private ActionListener listener;

    public PanelAction(String buttonText) {

        setLayout(new BorderLayout());
        text = new JTextField();
        button = new JButton(buttonText);
        add(text, BorderLayout.CENTER);
        add(button, BorderLayout.EAST);

    }

    public ActionListener getListener() {
        return listener;
    }

    public void setListener(ActionListener newListener) {
        text.removeActionListener(listener);
        button.removeActionListener(listener);
        listener = newListener;
        text.addActionListener(listener);
        button.addActionListener(listener);

    }

    public JTextField getText() {
        return text;
    }

    public JButton getButton() {
        return button;
    }

}

Classe de test :
<hr size="2" width="100%" />
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class TestPanelAction {

    public static void main(String[] args) {

        PanelAction panel = new PanelAction("Action!");

        JFrame fenetre = new JFrame("Test panel action");
        fenetre.setSize(300, 60);

        fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        fenetre.add(panel);

        fenetre.setVisible(true);

        panel.setListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("hello");
            }

        });

    }

}
0
sarici Messages postés 12 Date d'inscription lundi 17 mars 2008 Statut Membre Dernière intervention 27 mai 2008
26 mai 2008 à 13:46
Est ce que quelque chose de ce style est possible:

J'ai rajouter dans le code Fconnexion (ci dessus)
 mdp.addActionListener(new ActionEntree(1,appli,this));

J'ai la classe ActionEntre
public class ActionEntree implements ActionListener {
    static final int ENTRER = 1;
    protected int tAction;
    Client applic;
    Fconnexion fenetre;
    public ActionEntree(int action,Client appli,Fconnexion fen)    {
        tAction = action;
        applic = appli;
        fenetre = fen;
    }
    public void actionPerformed(ActionEvent e)    {
        JButton bouton;
        JTextField login ;
        JPasswordField mdp;
        JTextField mdpc;
       
        switch (tAction)        {
            case ENTRER :
                mdp = (JPasswordField)e.getSource();
                mdp.addActionListener(new ActionConnexion(1,applic,fenetre));
                break;
        }
    }
}

Et dans le meme style j'ai une classe ActionConnexion qui gere les actions de la classe Fconnexion, mais avec un case CONNEXION (choix1)!!
0
Rejoignez-nous