-
Bug
-
Resolution: Unresolved
-
P4
-
8, 17, 19, 20
-
None
Run the attached test case. Note that after the test is complete the caret stops being updated. With fix for JDK-4512626 the caret stays non-blinking, without it the caret disappears. If you click a button and then click back to the textField causing the focus to change caret will start working as expected.
-- Cut
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Robot;
import java.lang.reflect.InvocationTargetException;
public class CaretThing {
static JFrame frame;
static JTextField textField;
static Robot robot;
static JButton button;
static void printBlinkRate() {
System.err.println("Caret blink rate: " + textField.getCaret().getBlinkRate());
}
static void requestFocus(Component component) throws InterruptedException, InvocationTargetException {
robot.waitForIdle();
robot.delay(300);
SwingUtilities.invokeAndWait(() -> {
System.err.println("Requesting focus on " + component);
printBlinkRate();
component.requestFocus();
printBlinkRate();
});
}
static void changeEditable() throws InterruptedException, InvocationTargetException {
robot.waitForIdle();
robot.delay(300);
SwingUtilities.invokeAndWait(()-> {
System.err.println("Changing editable");
printBlinkRate();
textField.setEditable(!textField.isEditable());
printBlinkRate();
});
}
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame("Hey");
frame.setLocationRelativeTo(null);
textField = new JTextField("Some long field value");
textField.setEditable(false);
frame.setLayout(new BorderLayout());
frame.add(textField, BorderLayout.NORTH);
button = new JButton("Button");
button.addActionListener((e)-> printBlinkRate());
frame.add(button, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
button.requestFocus();
textField.getCaret().setBlinkRate(250);
});
requestFocus(textField);
// requestFocus(button); // comment this to get 0 blink rate on editable field
changeEditable();
requestFocus(textField);
SwingUtilities.invokeAndWait(CaretThing::printBlinkRate);
}
}
-- Cut
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Robot;
import java.lang.reflect.InvocationTargetException;
public class CaretThing {
static JFrame frame;
static JTextField textField;
static Robot robot;
static JButton button;
static void printBlinkRate() {
System.err.println("Caret blink rate: " + textField.getCaret().getBlinkRate());
}
static void requestFocus(Component component) throws InterruptedException, InvocationTargetException {
robot.waitForIdle();
robot.delay(300);
SwingUtilities.invokeAndWait(() -> {
System.err.println("Requesting focus on " + component);
printBlinkRate();
component.requestFocus();
printBlinkRate();
});
}
static void changeEditable() throws InterruptedException, InvocationTargetException {
robot.waitForIdle();
robot.delay(300);
SwingUtilities.invokeAndWait(()-> {
System.err.println("Changing editable");
printBlinkRate();
textField.setEditable(!textField.isEditable());
printBlinkRate();
});
}
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(() -> {
frame = new JFrame("Hey");
frame.setLocationRelativeTo(null);
textField = new JTextField("Some long field value");
textField.setEditable(false);
frame.setLayout(new BorderLayout());
frame.add(textField, BorderLayout.NORTH);
button = new JButton("Button");
button.addActionListener((e)-> printBlinkRate());
frame.add(button, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
button.requestFocus();
textField.getCaret().setBlinkRate(250);
});
requestFocus(textField);
// requestFocus(button); // comment this to get 0 blink rate on editable field
changeEditable();
requestFocus(textField);
SwingUtilities.invokeAndWait(CaretThing::printBlinkRate);
}
}
- duplicates
-
JDK-8299051 Caret doesn't blink if non-editable textfield becomes editable
-
- Closed
-