Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TextField getting a Focus on a Scene Load

abhinay_aFeb 6 2013 — edited Feb 6 2013
Hey, its a vey simple question but m not able to achieve it doing what so ever.

I was trying the code,
public class Demo extends Application
{
	
	
	public static void main(String[] args)
	{
		launch(args);
	}
	
	
	
	@Override 
	public void start(Stage stage)
	{
		stage.setTitle("Sample");
		Scene scene = new Scene(new Group(), 450, 250);
		
		
		BorderPane borderPane = new BorderPane();
		borderPane = newScreen();
		
		Group root = (Group)scene.getRoot();
		root.getChildren().add(borderPane);
		stage.setScene(scene);
		stage.show();
	}

	public BorderPane newScreen()
	 {
		 BorderPane  borderPane = new BorderPane();
		 
		 GridPane gridPane = new GridPane();
		 Label cName = new Label("Company Name");
		 Label cAbout = new Label("About");
		 Label cContact = new Label("Contact");
		 final TextField tCompanyName = new TextField();
		 TextField tAbout = new TextField();
		 TextField tContactUs = new TextField();
		 Button add = new Button("Add");
		 
		 
		 tCompanyName.requestFocus();
		    
		 gridPane.add(cName, 0, 0);
		 gridPane.add(tCompanyName, 1, 0);
		 gridPane.add(cAbout, 0, 1);
		 gridPane.add(tAbout, 1, 1);
		 gridPane.add(cContact, 0, 2);
		 gridPane.add(tContactUs, 1, 2);
		 gridPane.add(add, 0, 3);
		 gridPane.setAlignment(Pos.CENTER);
		 borderPane.setCenter(gridPane);
		 
		 
		 return borderPane;
	 }

		
	}
I want that whenever the scene is loaded the focus must be on the first textField "tCompanyName", but requestFocus() doesnt seem to work. Can anyone explain me why ?

Edited by: abhinay_a on Feb 6, 2013 2:53 AM
This post has been answered by jsmith on Feb 6 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 6 2013
Added on Feb 6 2013
2 comments
177 views