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

Composants Java Discussion :

obtenir la saisie de mon JTextField dans ma classe ADO


Sujet :

Composants Java

  1. #1
    Membre éclairé Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Par défaut obtenir la saisie de mon JTextField dans ma classe ADO
    bonsoir,

    je n'arrive pas a obtenir ce que je saisi dans mon JTextField dans ma classe ADO bien que j'ai déclarée ma classe Fenetre en static.
    Quand je run ma fenetre j'obtiet bien a l'affichage ce que je saisi mais des que je veux transferer ce que j'ai saisi a ma classe ADO par un fen.getLogin();
    ca m'affichage rien.

    voici mes 2 classes:

    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
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
     
     
    package modeles;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
     
    import ado.ADO;
     
     
     
    @SuppressWarnings("serial")
    public class Fenetre extends JPanel implements ActionListener{
     
    	private JPanel jpa1, jpa2, jpa3, jpa4, jpa5;
    	private JLabel jla1, jla2;
    	JTextField login;
    	private JPasswordField password;
    	private JTabbedPane jtp;
    	private JButton bvalider, b1, b2, b3, b4, b5;
    	private ImageIcon icon, icon1, icon2, icon3;
     
    	private static ADO ado = new ADO();
     
    	public Fenetre() {
    		super(new BorderLayout());
     
    		jtp = new JTabbedPane(JTabbedPane.TOP);
    		icon = createImage("/connect.png");
    		icon1 = createImage("/application_home.png");
    		icon2 = createImage("/add.png");
    		icon3 = createImage("/chart_line_add.png");
     
     
    		//JComponent p1 = makeTextPanel("Connexion");
    		//jtp.setMnemonicAt(1, KeyEvent.VK_1);
     
    		jpa1 = new JPanel();
    		jpa1.setLayout(new GridLayout(2,2));
     
    		//Onglet Connexion:
    		jpa2 = new JPanel();
    		jpa2.setLayout(new FlowLayout());
    		jla1 = new JLabel("Login: ");
    		jla2 = new JLabel("Password: ");
    		login = new JTextField(10);
    		password = new JPasswordField(10);
    		bvalider = new JButton("Submit");
    		jpa1.add(jla1);
    		jpa1.add(login);
    		jpa1.add(jla2);
    		jpa1.add(password);
    		jpa2.add(jpa1);
    		jpa2.add(bvalider);
     
    		//p1.add(jpa2);
    		jtp.addTab("Connexion", icon,jpa2);
     
    		//Onglet Accueil:
    		jpa3 = new JPanel();
    		jpa3.setLayout(new FlowLayout());
    		b1 = new JButton("Ajout d'un Patient");
    		b2 = new JButton("Suivi d'un patient");
    		b3 = new JButton("Liste des patients");
    		b4 = new JButton("Etat des patients");
    		b5 = new JButton("Recherche d'un patient");
     
    		bvalider.addActionListener(this);
     
    		//add the tabbedpane to this panel
    		add(jtp);
     
    		//the following line enables to use scrolling tabs
    		jtp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
     
    	}
    	protected JComponent makeTextPanel(String text) {
    		JPanel panel = new JPanel(false);
    		JLabel filler = new JLabel(text);
    		filler.setHorizontalAlignment(JLabel.CENTER);
    		panel.setLayout(new GridLayout(1, 1));
    		panel.add(filler);
    		return panel;
    	}
    	/** Returns an ImageIcon or null if the path was invalid.*/
    	protected static ImageIcon createImage(String path) {
    		java.net.URL imgURL = Fenetre.class.getResource(path);
    		if(imgURL != null){
    			return new ImageIcon(imgURL);
    		}else{
    		System.err.println("couldn't find file: "+path);
    		return null;
    	}
    	}
    	/**Create the GUI and show it. For thread safety.
             * this method should be invoked from
             * the event dispatch thread.
             */
    	public static void createAndShowGUI(){
    		//create and set up the window
    		JFrame frame = new JFrame("DiabetoMédical");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Add content ti the window.
    		frame.add(new Fenetre(), BorderLayout.CENTER);
     
    		//Display the window
    		frame.pack();
    		frame.setVisible(true);
    	}
    	public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    //Turn off metal's use of bold fonts
    		UIManager.put("swing.boldMetal", Boolean.FALSE);
    		createAndShowGUI();
                }
            });
        }
     
    	public String getLogin(){
    		return login.getText();
    	}
    	public String getPassword(){
    		return password.getText();
    	}
    	public void OngletAccueil(){
    		//JComponent p2 = makeTextPanel("Accueil");
    		jtp.addTab("Accueil",icon1,jpa3);
    	}
    	public void OngletAjoutPatient(){
    		JComponent p3 = makeTextPanel("Ajout d'un patient");
    		jtp.addTab("Ajout d'un patient",icon2, p3);
    	}
    	public void OngletAjoutSuiviPatient(){
    		JComponent p4 = makeTextPanel("Suivi Patient");
    		jtp.addTab("Suivi Patient",icon3, p4);
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		Object source = e.getSource();
    		System.out.println("login: "+getLogin());
    		if(bvalider ==source){
    			ado.connexion();
    			ado.consultation();
    			ado.deconnexion();
    		}	
    	}
     
    }
    classe ADO :
    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
     
    package ado;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    import modeles.Fenetre;
     
     
    public class ADO {
     
    	/*public ADO(Connection connection, String login, String password) {
    		super();
    		this.connection = connection;
    		this.login = login;
    		this.password = password;
    	}
    	public ADO() {
    		super();
    		// TODO Auto-generated constructor stub
    	}*/
    	private static final String DRIVER = "org.apache.derby.jdbc.ClientDriver";
    	private static final String URL = "jdbc:derby://localhost:1527/Diabetomedical";
    	private static final String USER = ";user=derby";
    	private static final String PWD = ";password=derby";
    	private Connection connection;
    	private String login;
    	private String password;
    	private Fenetre fen = new Fenetre();
    	//private Ecouteur eco;
     
    	public void connexion(){
    		try{
    		Class.forName(DRIVER);
    		connection = DriverManager.getConnection(URL+USER+PWD);
    		System.out.println("Démarrer et prêt à accepter les connexions");
    		}catch(ClassNotFoundException e){e.printStackTrace();
    		}catch(SQLException ex){ex.printStackTrace();}
     
    	}
    	public void deconnexion(){
    		try {
    			connection.close();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	public void consultation(){
    		//Statement stmt;
            //ResultSet result;
            try {
            	Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery( "SELECT * FROM connexion" );
                //ResultSet rs1 = stmt.executeQuery("SELECT COUNT(*) FROM connexion where login='" + fen.getLogin() + "' and password='" + fen.getPassword()+ "'");
                    while (rs.next() ) {
                        //int numColumns = rs.getMetaData().getColumnCount();
                    	//System.out.println("rs1"+rs);
                    	System.out.println("login: "+fen.getLogin());
                    	System.out.println("password: "+fen.getPassword());
                    	System.out.println("rs1.getObject"+rs.getObject(2));
                        //peut causer probleme car il peut rechercher dans toutes les colonnes des ligne de connexion les infos fournies
                        if(rs.getObject(2).equals(fen.getLogin()) && rs.getObject(3).equals(fen.getPassword())){
                        	System.out.println("apres");
                        	fen.OngletAccueil();
     
     
                        	//eco = new Ecouteur(3);
                        	//break;
                        }
                      /* for ( int i = 1; i <= numColumns; i++ ) {
                           //Column numbers start at 1.
                           //Also there are many methods on the result set to return
                           // the column as a particular type. Refer to the Sun documentation
                           // for the list of valid conversions.
                           System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
                        }*/
                    }
                deconnexion();
            } catch(SQLException e) {
                e.printStackTrace();
            }
     
     
    	}
     
     
     
    	}

  2. #2
    Membre éclairé Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Par défaut
    personne ?

  3. #3
    Membre Expert
    Avatar de krachik
    Inscrit en
    Décembre 2004
    Messages
    1 964
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 1 964
    Par défaut
    Bonjour
    Excuses moi mais c'est evident que quand tu recupere le texte saisie dans le JTextField avec la classe Fenetre dans la classe ADO il ait rien.
    Voila pourquoi ça ne marche pas(je vais essayer d'être clair):
    Dans la classe Fenetre il y a ton main dans lequel tu fais createAndShowGUI();
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    //Turn off metal's use of bold fonts
    		UIManager.put("swing.boldMetal", Boolean.FALSE);
    		createAndShowGUI();
                }
            });
        }
    Et c'est cette methode qui est supposé crée une "Fenetre" ,la preuve là
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public static void createAndShowGUI(){
    		//create and set up the window
    		JFrame frame = new JFrame("DiabetoMédical");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//Add content ti the window.
    		frame.add(new Fenetre(), BorderLayout.CENTER);
    		
    		//Display the window
    		frame.pack();
    		frame.setVisible(true);
    }
    Jusque la tout est bon mais le probleme se pose dans ta classe ADO quand tu fais au début
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    private Fenetre fen = new Fenetre();
    Tu recrée une autre "Fenetre" qui n'a plus rien à voir avec celle que tu viens de créer dans la classe "Fenetre".Donc normal que quand tu fais un getLogin dessus qu'il renvoie rien

    AU passage dans ton code il y a des petites choses corrgirer et à rendre proprement
    Exemple
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public void actionPerformed(ActionEvent e) {
    		//Object source = e.getSource();
                    JButton source =(JButton) e.getSource();//Vu que bvalider est un JButton
    		System.out.println("login: "+getLogin());
    		if(bvalider ==source){
    			ado.connexion();
    			ado.consultation();
    			ado.deconnexion();
    		}	
    }
    Alors si tu tiens quand même à recuperer ton login dans la fenetre ADO avec ce code que tu presentes la il faut declarer (pas tres propre à mon goût ),il faut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
            public static JTextField login;
    	public static JPasswordField password;
            private String loginRecu;//Ligne que j'ai rajouté
    et rajouter dans la gestion d'evenement

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public void actionPerformed(ActionEvent e) {
    		//Object source = e.getSource();
                    JButton source =(JButton) e.getSource();//Vu que bvalider est un JButton
    		System.out.println("login: "+getLogin());
    		if(bvalider ==source){
                            this.loginRecu=login.getText();//Reperer le texte uen fois qu'on appuyer sur le bouton "Submit"
    			ado.connexion();
    			ado.consultation();
    			ado.deconnexion();
    		}	
    }
    Et comme ça dans ta classe ADO tu recupere directement le login tapé en faisant par exemple
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    System.out.println("login"+Fenetre.loginRecu);
    Voila j'espere avoir été assez simple et clair
    Cordialement

  4. #4
    Membre éclairé Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Par défaut modele mvc pour swing
    j'ai changé :
    private String loginRecu;
    par :
    public static String loginRecu;

    et ca marche nickel

    merci

    En fait, comment le rendrait tu propre à ton goût.
    je voulais utiliser le pattern mvc en séparant la classe d'une part Fenetre extends JPanel, Ecouteur implements actionListener, la classe ADO ou DAO pour la connexion mais je ne sais pas comment m'y prendre car j'ai constaté des erreurs de compile quand j'ai voulu faire avec ce pattern.
    J'ai donc pris le modele de la classe Fenetre d'un tuto de sun.
    ce qui me genait s'était ca:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    public Fenetre() {
            super(new BorderLayout());
    et ce machin la :
    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
     public static void createAndShowGUI(){
            //create and set up the window
            JFrame frame = new JFrame("DiabetoMédical");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Add content ti the window.
            frame.add(new Fenetre(), BorderLayout.CENTER);
     
            //Display the window
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            createAndShowGUI();
                }
            });
        }
    car j'avais toujours l'habitude de créer des classe extends JFrame et ensuite l'appeler dans mon main.
    je constate qu'il y'a beaucoup d'exemple comme celui la avec ces machins qui me paraissent difficile a adapter parce que je ne comprend pas ces machins.

    bref, c'est sur que c'est pas trés propre.
    saurait-tu m'expliquer c'est machin ou m'apporter quelque chose de propre en concordance avec ce qui te parait propre.

    merci pour ton aide

  5. #5
    Membre éclairé Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Par défaut
    plus là!

Discussions similaires

  1. Obtenir la position d'un Dessin dans mon JPanel
    Par Koliko dans le forum Débuter
    Réponses: 3
    Dernier message: 02/01/2014, 17h55
  2. [AC-2003] Saisie d'espace impossible dans mon code
    Par yael44 dans le forum VBA Access
    Réponses: 4
    Dernier message: 13/12/2009, 13h05
  3. saisie des caractéres arabe dans un jTextField
    Par RouRa22 dans le forum Composants
    Réponses: 12
    Dernier message: 08/09/2008, 10h57
  4. Mémoriser les saisies de mon USF dans mon classeur
    Par etorria dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 20/03/2008, 22h59

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