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;
}