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

Agents de placement/Fenêtres Java Discussion :

Effet indésirable prise focus


Sujet :

Agents de placement/Fenêtres Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Dévelopeur Cobol + Java J2SE
    Inscrit en
    Novembre 2007
    Messages
    73
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dévelopeur Cobol + Java J2SE
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2007
    Messages : 73
    Par défaut Effet indésirable prise focus
    bonjour, j'ai un petit problème esthétique quand ma fenêtre reprend le focus par clic dans le textfield du bas.

    1. dans le jtextfield du haut, je saisis HHHHH
    2. je clique ailleurs que la fenêtre pour qu elle perde le focus
    3. je reviens dans la fenêtre en cliquant dans le champ du bas
    4. j'observe alors que le curseur s'affiche dans le champ du haut un très court instant (inesthétique) avant d'aller dans le second champ.

    Comment faire pour éviter cet effet ?

    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
    package essais.acomposant;
     
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.FocusEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.*;
    import javax.swing.plaf.TextUI;
    import javax.swing.text.*;
     
     
    public class MainOverwritableCaretTextField extends JTextField {
     
      public MainOverwritableCaretTextField() {
        this(null, null, 0);
      }
      public MainOverwritableCaretTextField(String text) {
        this(null, text, 0);
      }
      public MainOverwritableCaretTextField(int columns) {
        this(null, null, columns);
        this.getCaret().setBlinkRate(650);
      }
      public MainOverwritableCaretTextField(String text, int columns) {
        this(null, text, columns);
      }
     
      public MainOverwritableCaretTextField(Document doc, String text, int columns) {
        super(doc, text, columns);
        overwriteCaret = new OverwriteCaret();
        super.setCaret(overwriting ? overwriteCaret : insertCaret);
      }
     
      @Override
      public void setKeymap(Keymap map) {
        if (map == null) {
          super.setKeymap(null);
          sharedKeymap = null;
          return;
        }
     
        if (getKeymap() == null) {
          if (sharedKeymap == null) {
            // Switch keymaps. Add extra bindings.
            removeKeymap(keymapName);
            sharedKeymap = addKeymap(keymapName, map);
            loadKeymap(sharedKeymap, bindings, defaultActions);
          }
          map = sharedKeymap;
        }
        super.setKeymap(map);
      }
     
        @Override
        public void replaceSelection(String content) {
        Document doc = getDocument();
        if (doc != null) {
          // If we are not overwriting, just do the
          // usual insert. Also, if there is a selection,
          // just overwrite that (and that only).
          if (overwriting == true && getSelectionStart() == getSelectionEnd()) {
     
            // Overwrite and no selection. Remove
            // the stretch that we will overwrite,
            // then use the usual code to insert the
            // new text.
            int insertPosition = getCaretPosition();
            int overwriteLength = doc.getLength() - insertPosition;
            int length = content.length();
     
            if (overwriteLength > length) {
              overwriteLength = length;
            }
     
            // Remove the range being overwritten
            try {
              doc.remove(insertPosition, overwriteLength);
            } catch (BadLocationException e) {
              // Won't happen
            }
          }
        }
     
        super.replaceSelection(content);
      }
     
      // Change the global overwriting mode
      public static void setOverwriting(boolean overwriting) {
        MainOverwritableCaretTextField.overwriting = overwriting;
      }
     
      public static boolean isOverwriting() {
        return overwriting;
      }
     
      // Configuration of the insert caret
        @Override
      public void setCaret(Caret caret) {
        insertCaret = caret;
      }
     
      // Allow configuration of a new
      // overwrite caret.
      public void setOverwriteCaret(Caret caret) {
        overwriteCaret = caret;
      }
     
      public Caret getOverwriteCaret() {
        return overwriteCaret;
      }
     
      // Caret switching
      @Override
      public void processFocusEvent(FocusEvent evt) {
        if (evt.getID() == FocusEvent.FOCUS_GAINED) {
          selectCaret();
        }
        super.processFocusEvent(evt);
      }
     
      protected void selectCaret() {
        // Select the appropriate caret for the
        // current overwrite mode.
        Caret newCaret = overwriting ? overwriteCaret : insertCaret;
     
        if (newCaret != getCaret()) {
          Caret caret = getCaret();
          int mark = caret.getMark();
          int dot = caret.getDot();
          caret.setVisible(false);
     
          super.setCaret(newCaret);
     
          newCaret.setDot(mark);
          newCaret.moveDot(dot);
          newCaret.setVisible(true);
        }
      }
     
      protected Caret overwriteCaret;
     
      protected Caret insertCaret;
     
      protected static boolean overwriting = true;
     
      public static final String toggleOverwriteAction = "toggle-overwrite";
     
      protected static Keymap sharedKeymap;
     
      protected static final String keymapName = "OverwriteMap";
     
      protected static final Action[] defaultActions = { new ToggleOverwriteAction() };
     
      protected static JTextComponent.KeyBinding[] bindings = { new JTextComponent.KeyBinding(
          KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0),
          toggleOverwriteAction) };
     
      // Insert/overwrite toggling action
        public static class ToggleOverwriteAction extends TextAction {
        ToggleOverwriteAction() {
          super(toggleOverwriteAction);
        }
     
        @Override
        public void actionPerformed(ActionEvent evt) {
          MainOverwritableCaretTextField.setOverwriting(!MainOverwritableCaretTextField
              .isOverwriting());
          JTextComponent target = getFocusedComponent();
          if (target instanceof MainOverwritableCaretTextField) {
            MainOverwritableCaretTextField field = (MainOverwritableCaretTextField) target;
            field.selectCaret();
          }
        }
      }
     
      public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception evt) {}
     
        JFrame f = new JFrame("Overwrite test");
     
        MainOverwritableCaretTextField tf = new MainOverwritableCaretTextField(20);
    //    tf.setDocument//grb999
    //                 (new JTextFieldLimit(5,true));//grb999
        f.getContentPane().add(tf, "North");
     
     
        tf = new MainOverwritableCaretTextField(20);
        f.getContentPane().add(tf, "South");
     
        f.pack();
        f.setVisible(true);
      }
    }
    class OverwriteCaret extends DefaultCaret {
        @Override
        protected synchronized void damage(Rectangle r) {
            if (r != null) {
              try {
                JTextComponent comp = getComponent();
                TextUI mapper = comp.getUI();
                Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
                int large = r2.x - r.x;
                if (large == 0) {
                  large = MIN_WIDTH;
                }
                comp.repaint(r.x, r.y, large, r.height);
     
                // Swing 1.1 beta 2 compat
                this.x = r.x;
                this.y = r.y;
                this.width = large;
                this.height = r.height;
              } catch (BadLocationException e) {
              }
            }
      }
     
        @Override
      public void paint(Graphics g) {
        if (isVisible()) {
          try {
            JTextComponent comp = getComponent();
            TextUI mapper = comp.getUI();
            Rectangle r1 = mapper.modelToView(comp, getDot());
            Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
            g = g.create();
            g.setColor(comp.getForeground());
            g.setXORMode(comp.getBackground());
            int large = r2.x - r1.x;
            if (large == 0) {
              large = MIN_WIDTH;
            }
            g.fillRect(r1.x, r1.y, large, r1.height);
            g.dispose();
          } catch (BadLocationException e) {
          }
        }
      }
     
      protected static final int MIN_WIDTH = 8;
    }

  2. #2
    Membre confirmé
    Homme Profil pro
    Dévelopeur Cobol + Java J2SE
    Inscrit en
    Novembre 2007
    Messages
    73
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dévelopeur Cobol + Java J2SE
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2007
    Messages : 73
    Par défaut
    re-bonjour, pour simplifier, j'ai juste un problème de reprise de focus :
    1. je veux cliquer dans le champ du haut
    2. cliquer ailleurs que dans ma fenêtre pour qu elle perde le focus
    3. puis reprendre le focus dans le 2eme champ en cliquant dedans, mais sans que le premier champ ne prenne le focus puisque je n'ai pas cliqué dedans

    Je fais tout ça pour éviter un clignotement du curseur dans le champ du haut (c'est moche).

    Malheureusement lors du clic pour reprendre le focus voici ce que j obtiens :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    windowGainedFocus
    focus gained en haut
    mouse pressed en bas
    focus gained en bas
    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
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
     
    public class FocusIssue extends JFrame implements WindowFocusListener {
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new FocusIssue().setVisible(true);
                }
            });
        }
     
        public FocusIssue() {
     
            JTextField tf = new JTextField(10);
            tf.setName("haut");
            tf.addFocusListener(new GFocusListener());
            tf.addMouseListener(new GMouseAdapter());
     
            getContentPane().add(tf, "North");
     
            tf = new JTextField(10);
            tf.addFocusListener(new GFocusListener());
            tf.addMouseListener(new GMouseAdapter());
            tf.setName("bas");
            getContentPane().add(tf, "South");
     
            addWindowFocusListener(this);
     
            pack();
        }
     
        @Override
        public void windowGainedFocus(WindowEvent e) {
            System.out.println("windowGainedFocus");
        }
     
        @Override
        public void windowLostFocus(WindowEvent e) {
            System.out.println("windowLostFocus");
        }
     
        public class GFocusListener extends FocusAdapter {
            @Override
            public void focusGained(FocusEvent e) {
                System.out.println("focus gained en " + ((JComponent) e.getSource()).getName());
            }
        }
     
        public class GMouseAdapter extends MouseAdapter {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("mouse pressed en " + ((JComponent) e.getSource()).getName());
            }
        }
    }

  3. #3
    Membre confirmé
    Homme Profil pro
    Dévelopeur Cobol + Java J2SE
    Inscrit en
    Novembre 2007
    Messages
    73
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Dévelopeur Cobol + Java J2SE
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2007
    Messages : 73
    Par défaut
    ok j ai trouvé une solution, utilisant une combinaison de windowGainedFocus, mouseEntered, et sauvant le dernier champ focusé quand l'application perd le focus.

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

Discussions similaires

  1. Effet indésirable div retractable dans un script
    Par Lekno dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 28/02/2014, 17h15
  2. bouton avec texte : effet indésirable
    Par awalter1 dans le forum Mise en page CSS
    Réponses: 4
    Dernier message: 05/03/2012, 18h44
  3. Effets indésirables avec java.awt.Robot
    Par LGnord dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 20/08/2008, 14h34
  4. [C#]Enlever l'effet visuel du focus
    Par Odulo dans le forum Windows Forms
    Réponses: 1
    Dernier message: 24/07/2007, 09h21
  5. Prise de focus
    Par zoreil dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 28/07/2006, 14h12

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