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 :

écoute d'un jtextfield


Sujet :

AWT/Swing Java

  1. #1
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut écoute d'un jtextfield
    bonjour,

    j'ai un jtextfield et je voudrai lorsque l'on appuie sur un bouton que l'on prenne en compte la valeur de ce jtextfield mais SANS qu'il soit nécessaire d'appuyer sur la touche entrée, est-ce possible ?

    merci

    greg

  2. #2
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    Tu veux probablement appliquer un DocumentListener à ton JTextField. Cela te permettra de gérer l'évènement de changement de son contenu.

  3. #3
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut re
    j'ai ajouté un documentlistener à ma classe j'ai ajouter les méthodes mais comment prendre que le JTextfield comment le discriminer ? j'ai plusieurs jtextfield ...

  4. #4
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    J'ai pas trop compris ce que tu as dit. C'est ça que tu cherches ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    JTextField txt = new JTextField();
    txt.getDocument().addDocumentListener(this);

  5. #5
    Membre éclairé Avatar de DjGonk
    Profil pro
    Inscrit en
    Février 2007
    Messages
    88
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Février 2007
    Messages : 88
    Par défaut
    Si tu utilises un DocumentListener il faut que ta classe implemente DocumentListener. Ex :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    implements DocumentListener {
     
        public void insertUpdate(DocumentEvent e) {
            //TODO
        }
        public void removeUpdate(DocumentEvent e) {
             //TODO
        }
        public void changedUpdate(DocumentEvent e) {
             //TODO
        }
    Ensuite tu abonnes ta classe au document de ton (ou tes) JTextField comme l'a dit muad'dib,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    JTextField txt = new JTextField();
    txt.getDocument().addDocumentListener(this);
    ensuite pour savoir quel est le JTextField qui a declencher un DocuementEvent tu fait

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
        public void removeUpdate(DocumentEvent e) {
             if(e.getSource() == getJTextFieldEcoutable()){
                  //TODO le code à faire
             }
        }
    Voila un lien :

    http://java.sun.com/docs/books/tutor...tlistener.html

  6. #6
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut merci


    c'est surtout la dernière partie:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    if(e.getSource() == getJTextFieldEcoutable()){
                  //TODO le code à faire
             }
    comment là dedans je fais pour savoir si c'est mon JTEXT1 ou mon JTEXT2 ??? le e.getSource() ne fonctionne pas !!

  7. #7
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(e.getDocument() == txt1.getDocument())

  8. #8
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut re
    txt1 n'est pas connu dans document listener, je me trompe ? j'ai ajouter cela:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    if ( doc.getProperty("name").equals("Text Field 1")){
    				String st;
    				// reste à récupérer la valeur contenu //dans TExtField1
    		}
    comment je peux faire ?

  9. #9
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    Il faut que tu adaptes txt1 au nom de ton JTextField. Chez moi, j'ai mis
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    JTextField txt1 = new JTextField();

  10. #10
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut
    ben dans changeupdate je n'ai pas accès à la méthode getsource() ...ou alors je ne code pas là ou il faut ?

  11. #11
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    Relis mon message... Je n'ai jamais dit d'utiliser getSource()

  12. #12
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut


    autant pour moi !

    je dois alors absolument placer mes JTextField en static ? car ils ne sont pas connus de changedupdate() ?

  13. #13
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    Non ce n'est absolument pas nécessaire. Postes ton code pour voir.

  14. #14
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut voici
    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
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    // Java & Bézier
    // (C) 2002 Emmanuel Turquin
    //
    // Main.java
    //
    // This program is free software; you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation; either version 2 of the License, or
    // (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    //
    // You should have received a copy of the GNU General Public License
    // along with this program; if not, write to the Free Software
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     
    package Ma;
     
    import java.awt.*;
     
    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.Document;
     
     
     
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.net.URL;
     
    import jnb.*;
    import extended_classes.*;
     
     
    public class Main extends JApplet implements ActionListener, DocumentListener{
     
    	protected static final String	version = "1.0";
     
    	protected static JnBPanel	panel;
    	protected static MenuBar	menuBar;
    	protected static StatusBar	statusBar;
    	//protected static JPanel 	toolBar;
    	protected static File_g file_g;
    	protected static int	windowWidth = 840;
    	protected static int	windowHeight = 580;
    	protected JTextArea textArea;
    	protected String newline = "\n";
    	static int px=10, py=10;
    	JTextField textFieldPointX;
     
    	public void init(){
    		//Try to get window's width and height from applet parameters.
    		try {
    			String windowWidthString = getParameter("WIDTH");
     
    			if (windowWidthString != null) {
    				try {
    					windowWidth = Integer.parseInt(windowWidthString);
    				} catch (NumberFormatException e) {
    					//Use default width.
    				}
    			}
    		} catch (NullPointerException e) {
    			//Applet used as a stand-alone program, no parameter.
    		}
    		try {
    			String windowHeightString = getParameter("HEIGHT");
     
    			if (windowHeightString != null) {
    				try {
    					windowHeight = Integer.parseInt(windowHeightString);
    				} catch (NumberFormatException e) {
    					//Use default width.
    				}
    			}
    		} catch (NullPointerException e) {
    			//Applet used as a stand-alone program, no parameter.
    		}
     
    		//Initialize the layout.
    		getContentPane().setLayout(new BorderLayout());
    		panel = new JnBPanel();
    		getContentPane().add(panel);
     
    		//Initialize the Menu bar.
    		menuBar = new MenuBar();
    		getContentPane().add("North", menuBar);
     
    		//Initialize the Tool bar
     
    		JToolBar toolBar = new JToolBar("Still draggable");
    		addButtons(toolBar);
    		toolBar.setFloatable(true);
    		toolBar.setRollover(true);
     
    		//Create the text area used for output.  Request
    		//enough space for 5 rows and 30 columns.
    		/*textArea = new JTextArea(5, 30);
    		textArea.setEditable(false);
    		JScrollPane scrollPane = new JScrollPane(textArea);
     
    		//Lay out the main panel.
    		setPreferredSize(new Dimension(450, 130));
    		add(toolBar, BorderLayout.PAGE_START);
    		add(scrollPane, BorderLayout.CENTER);
     
     
    */
    		//getContentPane().add("East", toolBar);
     
    		//Initialize the Status bar.
    		statusBar = new StatusBar("Welcome to Java & Bézier " + version + "!");
    		//getContentPane().add("South", statusBar);
    		JPanel p = new  JPanel();
    		JPanel pchild = new  JPanel();
     
    		pchild.add(toolBar, BorderLayout.EAST);
    		pchild.add(statusBar, BorderLayout.WEST);
    		getContentPane().add("South", pchild);
     
     
    	}
     
     
    	static final private String PREVIOUS = "previous";
    	static final private String UP = "up";
    	static final private String NEXT = "next";
    	static final private String SOMETHING_ELSE = "other";
    	static final private String TEXT_ENTERED = "text";
     
     
    	protected void addButtons(JToolBar toolBar) {
    		JButton button = null;
     
    		JLabel labelPointX = new JLabel("Point X ");
    		toolBar.add(labelPointX);
     
    		//Point X textedit
    		JTextField textFieldPointX = new JTextField("10");
    		textFieldPointX.setColumns(4);
    		textFieldPointX.addActionListener(this);
    		textFieldPointX.getDocument().addDocumentListener(this);
     
    		textFieldPointX.setActionCommand("TEXT_PointX");
    		toolBar.add(textFieldPointX);
     
    		JLabel labelPointY = new JLabel(" Y ");
    		toolBar.add(labelPointY);
     
    		//Point Y textedit
    		JTextField textFieldPointY = new JTextField("10");
    		textFieldPointY.setColumns(4);
    		textFieldPointY.addActionListener(this);
    		textFieldPointY.getDocument().addDocumentListener(this);
    		textFieldPointY.getDocument().putProperty("name", "TF2");
    		textFieldPointY.setActionCommand("TEXT_PointY");
    		toolBar.add(textFieldPointY);
     
    		//first button
    		button = makeNavigationButton("Back24", "Add",
    				"Back to previous something-or-other",
    		"Add");
    		toolBar.add(button);
     
    		//second button
    		button = makeNavigationButton("Up24", "Delete",
    				"Up to something-or-other",
    		"Delete");
    		toolBar.add(button);
     
    		//separator
    		toolBar.addSeparator();
     
    		JLabel labelTrace = new JLabel(" Bezier ");
    		toolBar.add(labelTrace);
     
    		//third button
    		button = makeNavigationButton("Forward24", NEXT,
    				"Forward to something-or-other",
    		"Trace");
    		toolBar.add(button);
     
    		//separator
    		toolBar.addSeparator();
     
    		JLabel labelGridX = new JLabel(" Grid X ");
    		toolBar.add(labelGridX);
     
    		//grid X textedit
    		JTextField textFieldGridX = new JTextField("10");
    		textFieldGridX.setColumns(3);
    		textFieldGridX.addActionListener(this);
    		textFieldGridX.setActionCommand(TEXT_ENTERED);
    		toolBar.add(textFieldGridX);
     
    		JLabel labelGridY = new JLabel(" Y ");
    		toolBar.add(labelGridY);
     
     
    		//grid Y button
    		JTextField textFieldGridY = new JTextField("12");
    		textFieldGridY.setColumns(3);
    		textFieldGridY.addActionListener(this);
    		textFieldGridY.setActionCommand(TEXT_ENTERED);
     
    		toolBar.add(textFieldGridY);
     
    		//separator
    		toolBar.addSeparator();
     
    		JLabel labelRectX = new JLabel(" Rect X ");
    		toolBar.add(labelRectX);
     
    		//rectangle dx
    		JTextField textFieldRectDX = new JTextField("300");
    		textFieldRectDX.setColumns(3);
    		textFieldRectDX.addActionListener(this);
    		textFieldRectDX.setActionCommand(TEXT_ENTERED);
    		toolBar.add(textFieldRectDX);
     
    		JLabel labelRectY = new JLabel(" Y ");
    		toolBar.add(labelRectY);
     
    		//rectangle dy
    		JTextField textFieldRectDY = new JTextField("700");
    		textFieldRectDY.setColumns(3);
    		textFieldRectDY.addActionListener(this);
    		textFieldRectDY.setActionCommand(TEXT_ENTERED);
    		toolBar.add(textFieldRectDY);
     
    	}
     
    	public void changeUpdate(DocumentEvent e){
     
    	}
     
    	protected JButton makeNavigationButton(String imageName,
    			String actionCommand,
    			String toolTipText,
    			String altText) {
    		//Look for the image.
    		String imgLocation = "images/"
    			+ imageName
    			+ ".gif";
    		URL imageURL = ToolBarDemo2.class.getResource(imgLocation);
     
    		//Create and initialize the button.
    		JButton button = new JButton();
    		button.setActionCommand(actionCommand);
    		button.setToolTipText(toolTipText);
    		button.addActionListener(this);
     
    		if (imageURL != null) {                      //image found
    			button.setIcon(new ImageIcon(imageURL, altText));
    		} else {                                     //no image found
    			button.setText(altText);
    			System.err.println("Resource not found: "
    					+ imgLocation);
    		}
     
    		return button;
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		String cmd = e.getActionCommand();
    		String description = null;
     
    		System.out.println("ee");
     
    		// Handle each button.
    		if ("Add".equals(cmd)) { //Add point at px, py
     
     
    			if (Main.panel.board.size() == 0 ){
    				Point2DList pl = new Point2DList();
     
    				Main.panel.board.add(pl);
    			}
    			Point2DList pl =  (Point2DList)Main.panel.board.getCurrent();
    			jnb.Point2D point = new jnb.Point2D(px, py);
     
    			pl.add(pl.getCurrentIndex() + 1, point);
    			repaint();
     
     
     
    		} else if ("TEXT_PointX".equals(cmd)){
    			JTextField src = new JTextField();
    			src = (JTextField) e.getSource();
    			String tex = src.getText();
    			px = Integer.parseInt(tex);
    		} else if ("TEXT_PointY".equals(cmd)){ 
    			JTextField src = new JTextField();
    			src = (JTextField) e.getSource();
    			String tex = src.getText();
    			py = Integer.parseInt(tex);
     
     
    		} else if ("Delete".equals(cmd)) { // second button clicked
    			Main.panel.board.setRect();
    			Main.panel.Recta(100,100,150,150);
     
    		} else if (NEXT.equals(cmd)) { // third button clicked
    			description = "taken you to the next <something>.";
    		} else if (NEXT.equals(cmd)) { // fourth button clicked
    			description = "done something else.";
    		} else if (TEXT_ENTERED.equals(cmd)) { // text field
    			JTextField tf = (JTextField)e.getSource();
    			int RectX;
    			//tf.getText().valueOf(RectX);
    			Rect toto = new Rect();
    			toto.setRect(10, 10, 300, 100);
    			Main.panel.repaint();
    			Main.panel.Recta(100,100,150,150);
     
    		}
     
     
    	}
    	protected void displayResult(String actionDescription) {
    		textArea.append(actionDescription + newline);
    		textArea.setCaretPosition(textArea.getDocument().getLength());
    	}
     
    	public static void main(String s[]) {
    		JFrame f = new JFrame("Java & Bézier " + version);
    		JApplet applet = new Main();
     
    		f.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {System.exit(0);}
    		});
    		f.getContentPane().add(applet, BorderLayout.CENTER);
    		f.pack();
    		f.setSize(new Dimension(windowWidth, windowHeight));
    		applet.init();
    		f.setVisible(true);
    	}
     
    	@Override
    	public void changedUpdate(DocumentEvent arg0) {
    		// TODO Auto-generated method stub
     
    		if( arg0.getDocument() == textFieldPointX.getDocument()){
    		 String st = textFieldPointX.getText();
    		 px = Integer.parseInt(st);
    		 System.out.println("xxxxx=" + px);
    		}
     
     
    	}
     
    	@Override
    	public void insertUpdate(DocumentEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void removeUpdate(DocumentEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
    }
     
     
    class JnBPanel extends JPanel implements MouseListener, MouseMotionListener{
     
    	BufferedImage		bi;
    	Graphics2D			big;
    	boolean			firstTime = true;
    	boolean			pressOut = false;
    	boolean			mouseOutside = false;
    	boolean			antiAliasing = true;
    	Board2D			board;
    	protected int		width, height;
    	protected int npx, npy;
     
    	public void Recta(int x, int y, int dx, int dy){
    		Rect toto = new Rect();
    		toto.setRect(20, 20, 80, 400);
    		toto.drawRect(big);
    		Main.panel.repaint();
    		/*toto.update(big);
    		toto.show();
    		toto.setVisible(true);
    		toto.repaint();*/
     
    	}
     
     
     
    	public JnBPanel(){
     
    		setBackground(Color.white);
    		addMouseMotionListener(this);
    		addMouseListener(this);
    	}
     
    	public void mousePressed(MouseEvent e){
    		int i;
     
    		//BUTTON1 or BUTTON2
    		if ((e.getModifiers() & (InputEvent.BUTTON1_MASK | InputEvent.BUTTON2_MASK)) > 0){
    			if (board.isVisible()){
    				if ((i = board.contains(e.getX(), e.getY())) >= 0){
    					board.setCurrent(i);
     
    					Point2DList pl =  (Point2DList)board.getCurrent();
     
    					pl.setCurrent(pl.contains(e.getX(), e.getY()));
    					System.out.println("ex="+e.getX()+"ey="+e.getY());
    					Main.statusBar.displayMsg("Point (X:" + e.getX() + ", Y:" + e.getY() + ") selected.");
    					updateLocation(e);
    				}
    				else {
    					if (board.size() == 0 || (e.getModifiers() & InputEvent.BUTTON2_MASK) > 0){
    						Point2DList pl = new Point2DList();
     
    						board.add(pl);
    					}
    					Point2DList pl =  (Point2DList)board.getCurrent();
    					jnb.Point2D point = new jnb.Point2D(e.getX(), e.getY());
     
    					pl.add(pl.getCurrentIndex() + 1, point);
    					Main.statusBar.displayMsg("Point (X:" + e.getX() + ", Y:" + e.getY() + ") added.");
    				}
    			}
    			else {
    				pressOut = true;	
    			}
    		}
     
    		//BUTTON3
    		else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) > 0){
    			if(board.isVisible()){
    				if ((i = board.contains(e.getX(), e.getY())) >= 0){
    					Point2DList pl =  (Point2DList)board.get(i);
    					if (pl.remove(pl.contains(e.getX(), e.getY())) == 1)
    						board.remove(pl);
    					else
    						board.setActive(pl);
    					Main.statusBar.displayMsg("Point (X:" + e.getX() + ", Y:" + e.getY() + ") removed.");
    					repaint();
    				}
    			}
    		}
    	}
     
    	public void mouseDragged(MouseEvent e){
    		if((e.getModifiers() & (InputEvent.BUTTON1_MASK | InputEvent.BUTTON2_MASK)) > 0 && !mouseOutside && !pressOut) {
    			updateLocation(e);
    		}
    	}
     
    	public void mouseReleased(MouseEvent e){
    		if(board.isVisible() && ((e.getModifiers() & InputEvent.BUTTON3_MASK) == 0)){
    			updateLocation(e);
    		}
    		else {
    			pressOut = false;
    		}
    	}
     
    	public void mouseMoved(MouseEvent e){
    		Main.statusBar.displayMsgBG("(X:" + e.getX() + ", Y:" + e.getY() + ")");
    	}
     
    	public void mouseExited(MouseEvent e){
    		mouseOutside = true;
    	}
     
    	public void mouseEntered(MouseEvent e){
    		mouseOutside = false;
    	}
     
    	public void mouseClicked(MouseEvent e){}
     
    	public void updateLocation(MouseEvent e){
    		Dimension dim = getSize();
    		Point2DList pl =  (Point2DList)board.getCurrent();
     
    		if (pl == null)
    			return;
     
    		jnb.Point2D point = (jnb.Point2D)pl.getCurrent();
    		int x, y;
     
    		if (board.getGrid()){
    			x = jnb.Math.round(e.getX(), board.getGridSpacing());
    			y = jnb.Math.round(e.getY(), board.getGridSpacing());
    		} else {
    			x = e.getX();
    			y = e.getY();
    		}
    		Main.statusBar.displayMsgBG("Point moved to (X:" + x + ", Y:" + y + ").");
    		if ((e.getModifiers() & InputEvent.BUTTON1_MASK) > 0)
    			point.setLocation(x, y, dim);
    		else if ((e.getModifiers() & InputEvent.BUTTON2_MASK) > 0)
    			pl.setLocation(x, y, dim);
    		repaint();
    	}
     
    	public boolean isAntiAliased() {
    		return antiAliasing;
    	}
     
    	public void setAntiAliasing(boolean aa) {
    		antiAliasing = aa;
    		if (antiAliasing)
    			big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		else
    			big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);	
    	}
     
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		update(g);
    	}
     
    	public void update(Graphics g){
    		Dimension dim = getSize();
    		int w = dim.width;
    		int h = dim.height;
    		//System.out.println("ici");
    		if(firstTime){
    			width = w;
    			height = h;
    			board = new Board2D(dim);
    			bi = (BufferedImage)createImage(w, h);
    			big = bi.createGraphics();
    			big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    			big.setColor(Color.white);
    			firstTime = false;
    		}
    		if (w != width || h != height) {
    			width = w;
    			height = h;
    			board.setDimension(dim);
    			bi = (BufferedImage)createImage(w, h);
    			big = bi.createGraphics();
    			big.setColor(Color.white);
    		}
     
    		// Clears the drawing area.
    		big.clearRect(0, 0, w, h);
     
    		// Draws and fills the points and curve(s) on the board.
    		board.draw(big);
    		Rect.drawRect(big);
     
    		// Draws the buffered image to the screen.
    		g.drawImage(bi, 0, 0, this);
    	}
    }

  15. #15
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    Tu n'as pas encore plus long comme code ? Quand on a un bug on fait ce qu'on appelle un test case, oú l'on allège au maximum son code pour tenter d'en localiser la source. Chez moi le code suivant fonctionne parfaitement.
    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
    public class TestMem extends JFrame implements DocumentListener {
       JTextField txt = new JTextField();
     
       public TestMem() {
          txt.getDocument().addDocumentListener(this);
          add(txt);
       }
     
       public void changedUpdate(DocumentEvent e) {
          if (e.getDocument() == txt.getDocument()) {
             System.out.println("changed");
          }
       }
     
       public void insertUpdate(DocumentEvent e) {
          if (e.getDocument() == txt.getDocument()) {
             System.out.println("inserted");
          }
       }
     
       public void removeUpdate(DocumentEvent e) {
          if (e.getDocument() == txt.getDocument()) {
             System.out.println("removed");
          }
       }
     
       public static void main(String args[]) {
          new TestMem().setVisible(true);
       }
    }
    Et à priori chez toi ça devrait fonctionner également. Ton problème doit se situer ailleurs.

  16. #16
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut
    désolé pour le code

    j'ai placé le jtextfield dans un jtoolbar, ça peut venir de là ? il faut faire un addDocumentListener() du toolbar ?

    merci pour l'aide c'est sympa, je suis autodidacte et j'ai beaucoup de mal à comprendre

  17. #17
    Membre Expert
    Avatar de muad'dib
    Homme Profil pro
    Développeur Java
    Inscrit en
    Janvier 2003
    Messages
    1 013
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2003
    Messages : 1 013
    Par défaut
    A priori non ça ne pose pas de problème. Ton problème semble être la visibilité de ton JTextField. Es-tu sûr qu'il est bien déclaré dans la même classe? Le cas échéant il faut que tu mettes en place un accesseur.

  18. #18
    Membre confirmé
    Inscrit en
    Septembre 2008
    Messages
    145
    Détails du profil
    Informations personnelles :
    Âge : 68

    Informations forums :
    Inscription : Septembre 2008
    Messages : 145
    Par défaut re
    ben en fait j'ai placé le code dans update et c'était plutot dans insertupdate... on apprend tous les jours

    merci bcp pour ton aide

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

Discussions similaires

  1. [JtextField]Creer un masque pour Ip
    Par bibx dans le forum Composants
    Réponses: 8
    Dernier message: 11/01/2005, 18h31
  2. [JTextField] Problème de setText()
    Par deathwing dans le forum Composants
    Réponses: 4
    Dernier message: 09/06/2004, 11h54
  3. [JTextField][JDBC] Problème d'affichage
    Par deathwing dans le forum JDBC
    Réponses: 4
    Dernier message: 12/05/2004, 15h50
  4. Intercepter la tabulation sur un JTextField
    Par Fladnag dans le forum Composants
    Réponses: 2
    Dernier message: 29/03/2004, 12h05
  5. [JTextField] filtrer la saisie
    Par Merfolk dans le forum Composants
    Réponses: 7
    Dernier message: 04/03/2004, 20h57

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