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 :

[JFormattedTextField] Adresse IP


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité
    Invité(e)
    Par défaut [JFormattedTextField] Adresse IP
    Bonjour,

    J'ai créé un JFrame qui demande à l'utilisateur de rentrer l'adresse IP du server.
    Cette JFrame est composée de 4 JFormattedTextField avec un zoli label entre chacun pour faire le zoli point.

    J'ai ajouté un MaskFormatter comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    maskFormatter = new MaskFormatter("###");
    maskFormatter.setValidCharacters("0123456789");
    que je met donc ensuite à chaque JFormattedTextField. Par défaut j'ai aussi mis l'adresse local (champ1: 127, champ2: 0, champ3: 0, champ4: 1).

    Avec mon main, j'ouvre la fenêtre la fenêtre, je fais une boucle infinie en attendant que l'utilisateur presse le bouton "connexion". Dès qu'il est pressé je récupère le texte de chaque JFormattedTextField. J'y met des points entre et hop je forme mon IP.

    Le problème est que lorsque je récupère un champs n'ayant qu'un ou deux chiffres dedans (comme par exemple le 2ème champ avec 0), je récupère en fait des espaces et donc quand je fourni l'IP au socket et bien forcément ça passe pas

    Comment pourrais-je faire pour ne pas récupérer ces espaces en même temps que je récupère la partie d'IP ?

    Merci et bonne journée.

  2. #2
    Membre chevronné Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Par défaut
    Ben avec un trim()

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String sansEspace = textField.getText().trim() ;

  3. #3
    Membre émérite
    Avatar de sironimo
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    669
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Mai 2004
    Messages : 669
    Par défaut
    Par contre c'est quoi l'intérêt du :

    Citation Envoyé par nicofromChina
    Avec mon main, j'ouvre la fenêtre la fenêtre, je fais une boucle infinie en attendant que l'utilisateur presse le bouton "connexion"

  4. #4
    Invité
    Invité(e)
    Par défaut
    Mais oui !

    J'y pensais même plus :p

    J'étais trop dans l'idée qu'un problème venait de ma fenêtre avec l'IP...

    Par contre j'ai toujours un problème mais autre. Lorsque je rentre dans un des JFormattedTextField puis dès que j'en sort (via Tab ou en cliquant dans un autre JFormattedTextField) SAUF s'il y a 3 chiffres de rentrés. Voici le code de la JFrame:
    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
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    package client;
     
    //import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.text.MaskFormatter;
    import java.awt.Dimension;
    import javax.swing.SwingConstants;
    import javax.swing.JFormattedTextField;
    import java.awt.Font;
    import java.awt.Toolkit;
    import java.awt.Rectangle;
    import javax.swing.JButton;
    import java.awt.Insets;
    import java.util.Locale;
    import java.text.ParseException;
     
     
    public class IpAddress extends JFrame {
     
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JLabel jLabel = null;
    	private JFormattedTextField jFormattedTextField1 = null;
    	private MaskFormatter maskFormatter = null;  //  @jve:decl-index=0:
    	private JButton jButton = null;
    	private JFormattedTextField jFormattedTextField2 = null;
    	private JFormattedTextField jFormattedTextField3 = null;
    	private JFormattedTextField jFormattedTextField4 = null;
    	private JLabel jLabelPoint1 = null;
    	private JLabel jLabelPoint2 = null;
    	private JLabel jLabelPoint3 = null;
    	private boolean clickOnConnexion = false;
     
    	public IpAddress() {
    		super();
    		initialize();
    	}
     
    	private void initialize() {
    		this.setSize(300, 163);
    		this.setVisible(true);
    		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    	    this.setLocation(dim.width/2 - this.getWidth()/2, dim.height/2 - this.getHeight()/2);
    		this.setResizable(false);
    		this.setContentPane(getJContentPane());
    		this.setTitle("IP Address");
    		this.addWindowListener(new java.awt.event.WindowAdapter() {
    			public void windowClosing(java.awt.event.WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
     
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jLabelPoint3 = new JLabel();
    			jLabelPoint3.setBounds(new Rectangle(194, 43, 8, 26));
    			jLabelPoint3.setHorizontalTextPosition(SwingConstants.CENTER);
    			jLabelPoint3.setText(".");
    			jLabelPoint3.setVerticalAlignment(SwingConstants.BOTTOM);
    			jLabelPoint3.setVerticalTextPosition(SwingConstants.BOTTOM);
    			jLabelPoint3.setHorizontalAlignment(SwingConstants.CENTER);
    			jLabelPoint2 = new JLabel();
    			jLabelPoint2.setBounds(new Rectangle(147, 43, 8, 26));
    			jLabelPoint2.setHorizontalTextPosition(SwingConstants.CENTER);
    			jLabelPoint2.setText(".");
    			jLabelPoint2.setVerticalAlignment(SwingConstants.BOTTOM);
    			jLabelPoint2.setVerticalTextPosition(SwingConstants.BOTTOM);
    			jLabelPoint2.setHorizontalAlignment(SwingConstants.CENTER);
    			jLabelPoint1 = new JLabel();
    			jLabelPoint1.setBounds(new Rectangle(100, 43, 8, 26));
    			jLabelPoint1.setHorizontalAlignment(SwingConstants.CENTER);
    			jLabelPoint1.setHorizontalTextPosition(SwingConstants.CENTER);
    			jLabelPoint1.setVerticalAlignment(SwingConstants.BOTTOM);
    			jLabelPoint1.setVerticalTextPosition(SwingConstants.BOTTOM);
    			jLabelPoint1.setText(".");
    			jLabel = new JLabel();
    			jLabel.setText("Please enter the IP address of the server:");
    			jLabel.setPreferredSize(new Dimension(235, 30));
    			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
    			jLabel.setHorizontalTextPosition(SwingConstants.TRAILING);
    			jLabel.setBounds(new Rectangle(0, 0, 292, 30));
    			jLabel.setName("ipAddress");
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(jLabel, null);
    			jContentPane.add(getJFormattedTextField1(), null);
    			jContentPane.add(getJButton(), null);
    			jContentPane.add(getJFormattedTextField2(), null);
    			jContentPane.add(getJFormattedTextField3(), null);
    			jContentPane.add(getJFormattedTextField4(), null);
    			jContentPane.add(jLabelPoint1, null);
    			jContentPane.add(jLabelPoint2, null);
    			jContentPane.add(jLabelPoint3, null);
    		}
    		return jContentPane;
    	}
     
    	private JFormattedTextField getJFormattedTextField1() {
    		if (jFormattedTextField1 == null) {
    			try {
    				maskFormatter = new MaskFormatter("###");
    				maskFormatter.setValidCharacters("0123456789");
    				jFormattedTextField1 = new JFormattedTextField(maskFormatter);
    				jFormattedTextField1.setBounds(new Rectangle(61, 43, 40, 26));
    				jFormattedTextField1.setLocale(new Locale("en", "fr", ""));
    				jFormattedTextField1.setName("ip");
    				jFormattedTextField1.setPreferredSize(new Dimension(123, 25));
    				jFormattedTextField1.setToolTipText("Please enter here the server IP address");
    				jFormattedTextField1.setMargin(new Insets(0, 2, 0, 2));
    				jFormattedTextField1.setColumns(15);
    				jFormattedTextField1.setHorizontalAlignment(JFormattedTextField.CENTER);
    				jFormattedTextField1.setText("127");
    				jFormattedTextField1.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 13));
    			}catch (ParseException e) { 
    				e.printStackTrace(); 
    		    }
    		}
    		return jFormattedTextField1;
    	}
     
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton();
    			jButton.setBounds(new Rectangle(96, 80, 103, 27));
    			jButton.setText("Connexion");
    			jButton.addKeyListener(new java.awt.event.KeyAdapter() {
    				public void keyPressed(java.awt.event.KeyEvent e) {
    					if(e.getKeyCode() == 10){
    						clickOnConnexion = true;
    					}
    				}
    			});
    			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
    				public void mouseClicked(java.awt.event.MouseEvent e) {
    					clickOnConnexion = true;
    				}
    			});
    		}
    		return jButton;
    	}
     
    	private JFormattedTextField getJFormattedTextField2() {
    		if (jFormattedTextField2 == null) {
    			try {
    				maskFormatter = new MaskFormatter("###");
    				maskFormatter.setValidCharacters("0123456789");
    				jFormattedTextField2 = new JFormattedTextField(maskFormatter);
    				jFormattedTextField2.setBounds(new Rectangle(108, 43, 40, 26));
    				jFormattedTextField2.setLocale(new Locale("en", "fr", ""));
    				jFormattedTextField2.setName("ip");
    				jFormattedTextField2.setPreferredSize(new Dimension(123, 25));
    				jFormattedTextField2.setToolTipText("Please enter here the server IP address");
    				jFormattedTextField2.setMargin(new Insets(0, 2, 0, 2));
    				jFormattedTextField2.setColumns(15);
    				jFormattedTextField2.setHorizontalAlignment(JFormattedTextField.CENTER);
    				jFormattedTextField2.setText("0");
    				jFormattedTextField2.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 13));
    			}catch (ParseException e) { 
    				e.printStackTrace(); 
    		    }
    		}
    		return jFormattedTextField2;
    	}
     
    	private JFormattedTextField getJFormattedTextField3() {
    		if (jFormattedTextField3 == null) {
    			try {
    				maskFormatter = new MaskFormatter("###");
    				maskFormatter.setValidCharacters("0123456789");
    				jFormattedTextField3 = new JFormattedTextField(maskFormatter);
    				jFormattedTextField3.setBounds(new Rectangle(155, 43, 40, 26));
    				jFormattedTextField3.setLocale(new Locale("en", "fr", ""));
    				jFormattedTextField3.setName("ip");
    				jFormattedTextField3.setPreferredSize(new Dimension(123, 25));
    				jFormattedTextField3.setToolTipText("Please enter here the server IP address");
    				jFormattedTextField3.setMargin(new Insets(0, 2, 0, 2));
    				jFormattedTextField3.setColumns(15);
    				jFormattedTextField3.setHorizontalAlignment(JFormattedTextField.CENTER);
    				jFormattedTextField3.setText("0");
    				jFormattedTextField3.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 13));
    			}catch (ParseException e) { 
    				e.printStackTrace(); 
    		    }
    		}
    		return jFormattedTextField3;
    	}
     
    	private JFormattedTextField getJFormattedTextField4() {
    		if (jFormattedTextField4 == null) {
    			try {
    				maskFormatter = new MaskFormatter("###");
    				maskFormatter.setValidCharacters("0123456789");
    				jFormattedTextField4 = new JFormattedTextField(maskFormatter);
    				jFormattedTextField4.setBounds(new Rectangle(202, 43, 40, 26));
    				jFormattedTextField4.setLocale(new Locale("en", "fr", ""));
    				jFormattedTextField4.setName("ip");
    				jFormattedTextField4.setPreferredSize(new Dimension(123, 25));
    				jFormattedTextField4.setToolTipText("Please enter here the server IP address");
    				jFormattedTextField4.setMargin(new Insets(0, 2, 0, 2));
    				jFormattedTextField4.setColumns(15);
    				jFormattedTextField4.setHorizontalAlignment(JFormattedTextField.CENTER);
    				jFormattedTextField4.setText("1");
    				jFormattedTextField4.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 13));
    			}catch (ParseException e) { 
    				e.printStackTrace(); 
    		    }
    		}
    		return jFormattedTextField4;
    	}
     
    	public JFormattedTextField getTextField1() {
    		return jFormattedTextField1;
    	}
    	public JFormattedTextField getTextField2() {
    		return jFormattedTextField2;
    	}
    	public JFormattedTextField getTextField3() {
    		return jFormattedTextField3;
    	}
    	public JFormattedTextField getTextField4() {
    		return jFormattedTextField4;
    	}
    	public boolean getClickOnConnexion() {
    		return clickOnConnexion;
    	}
    	public void setClickOnConnexion(boolean clickOnConnexion) {
    		this.clickOnConnexion = clickOnConnexion;
    	}
    	public void setFocusOnTextField1 ()
    	{
    		jFormattedTextField1.setFocusable(true);
    	}
     
    }
    Une idée ? Et si au passage vous savez quel filtre il faut utiliser pour limiter les nombres entre 0 et 255 je suis preneur.

    Sironimo, tu vois une autre façon de faire ?

  5. #5
    Membre émérite
    Avatar de sironimo
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    669
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Mai 2004
    Messages : 669
    Par défaut
    Non c'est juste le terme boucle infinie qui me dérange en fait. Tu attends un événement clavier ou souris sur ce bouton.

    Pour moi, boucle infinie correspond à while(bool), donc pour moi dans mon idée, tu faisais passer bool à false à l'événement. Mais je suis d'accord qu'on est en attente d'un événement utilisateur et que pendant ce temps-là, l'exécution du main ne peut se poursuivre.

    Pour les filtres, je sais pas trop, la méthode facile serait de contrôler la valeur à chaque changement de focus.

    Par exemple, il est sur le premier jtextfield, il passe au second, le focus change de composant et tu vérifies donc la valeur dans le composant précédent, en vérifiant bien qu'elle soit compris entre 0 et 255 ou alors tu fais les vérifications au moment du clic sur le bouton

  6. #6
    Invité
    Invité(e)
    Par défaut
    Et t'as pas une idée pour
    Lorsque je rentre dans un des JFormattedTextField puis dès que j'en sort (via Tab ou en cliquant dans un autre JFormattedTextField) SAUF s'il y a 3 chiffres de rentrés. Voici le code de la JFrame:
    ?

Discussions similaires

  1. Réponses: 1
    Dernier message: 26/02/2009, 10h59
  2. JFormattedTextField et adresse ip?
    Par altadeos dans le forum NetBeans
    Réponses: 7
    Dernier message: 18/01/2007, 13h16
  3. [JFormattedTextField ]validité d'une adresse mail
    Par gege2mars dans le forum Composants
    Réponses: 3
    Dernier message: 14/01/2005, 16h55
  4. Adresse des polices de caractères dans la RAM video ?
    Par Anonymous dans le forum x86 16-bits
    Réponses: 5
    Dernier message: 27/05/2002, 17h29

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