JDK-8129400 : Text selection fails when focusing ComboBox
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8u45
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-06-19
  • Updated: 2018-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows 7 Professional 64-bit Service Pack 1

A DESCRIPTION OF THE PROBLEM :
To get the text in an editable ComboBox selected when it gets focused, I added a listener to the 'focused'-property of the editor inside the ComboBox.
In this listener the editor's 'selectAll' method is called. 
I expected the text to be selected when the ComboBox's editor gets the focus, but this doesn't happened.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the provided code. Enter some text inside the ComboBox and switch the focus between the ComboBox's editor and the Button.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When the editor has the focus the text should be selected
ACTUAL -
The text is not selected when the editor gets the focus.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;


public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			BorderPane root = new BorderPane();
			Scene scene = new Scene(root,400,400);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
			
			ComboBox<String> comboBox = new ComboBox<>();
			comboBox.setEditable(true);
			comboBox.setItems(FXCollections.observableArrayList("eins", "zwei"));
			comboBox.focusedProperty().addListener((ov, o, n) -> {
				if (n) {
					System.out.println("Focus!");
					comboBox.getEditor().selectAll();
				}
			});
			
			Button button = new Button("Click me!");
			
			VBox vBox = new VBox();
			vBox.getChildren().addAll(comboBox, button);
			
			root.setCenter(vBox);
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}

---------- END SOURCE ----------


Comments
An easy workaround is to wrap selectAll() inside Platform.runLater(). Note: The description mentions setting the focus listeer on the TextField, but the sample code sets it on the ComboBox. The code is correct. Starting with 8u60, the TextField in a ComboBox will not be focusTraversable. See JDK-8096725.
22-06-2015