How to show make a hidden form visible in javafx - java

I have an application that is designed to be a sandwich shop system and I have a main menu form that is my main class i.e. this class holds my main class and is the first form to open when the application is executed.
When I click on the create new standard order button I have it so it will display a menu of items to chose from. I have this code behind the create new standard order button so it will hide the first window and display the next window.
btnStdOrder.setOnAction(e -> {
((Node)e.getSource()).getScene().getWindow().hide();
NewOrderPopUp.Display();
});
However, how do I go back to this first window? I have tried using the same code as above but because the first form holds my main and start methods I cant recall them again using the above method (or maybe I can I just don't know how to do it). Any help would be greatly appreciated.

If your popUpDisplay method ends with showAndWait(), you can try this:
((Stage)((Node)e.getSource()).getScene().getWindow()).hide();
NewOrderPopUp.Display();
((Stage)((Node)e.getSource()).getScene().getWindow()).show();
I created a sample app that shows this behavior in action.
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication53 extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller - MainScene
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.logging.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
/**
*
* #author blj0011
*/
public class FXMLDocumentController implements Initializable
{
#FXML private Button btnMain;
Stage window;
#Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
btnMain.setOnAction(e -> {
((Stage)((Node)e.getSource()).getScene().getWindow()).hide();
popUpDisplay();
((Stage)((Node)e.getSource()).getScene().getWindow()).show();
});
}
public void popUpDisplay()
{
try
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SceneTwo.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
//stage.initStyle(StageStyle.UNDECORATED);
stage.setTitle("PopUp");
stage.setScene(new Scene(root1));
stage.showAndWait();
}
catch (IOException ex)
{
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
FXML - Main Scene
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" maxHeight="300.0" maxWidth="300.0" minHeight="300.0" minWidth="300.0" prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication53.FXMLDocumentController">
<children>
<Button fx:id="btnMain" layoutX="124.0" layoutY="248.0" mnemonicParsing="false" text="Button" />
<Label layoutX="99.0" layoutY="135.0" text="Main Scene">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
Controller - PopUp
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;
/**
* FXML Controller class
*
* #author blj0011
*/
public class SceneTwoController implements Initializable
{
#FXML Button btnClosePopup;
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
btnClosePopup.setOnAction(e -> {
((Stage)(((Button)e.getSource()).getScene().getWindow())).close();
});
}
}
FXML - PopUp
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication53.SceneTwoController">
<children>
<Label layoutX="257.0" layoutY="155.0" text="Popup">
<font>
<Font size="30.0" />
</font>
</Label>
<Button fx:id="btnClosePopup" layoutX="259.0" layoutY="353.0" mnemonicParsing="false" text="Close Popup" />
</children>
</AnchorPane>

Related

JavaFX error "null pointer Exception" when passing parameter from one Controller to another Controller

i have tried all solution in youtube and google but none of them work. The problem is when i want to switch to new scene along sending data to another scene i got error saying
Null pointer Exception
public class FirstController implements Initializable {
#FXML private TextField textField;
#FXML private Button btn;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void onClick(ActionEvent event){
//switching to new scene
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("/application/Second.fxml"));
try{
loader.load();
}catch(Exception e){ }
SecondController sn=loader.getController();
sn.setText(textField.getText());
Parent p=loader.getRoot();
Stage window=new Stage();
window.setScene(new Scene(p));
window.setTitle("dfd");
window.show();
}
}
main.fxml:
<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<children>
<Button fx:id="btn" layoutX="285.0" layoutY="264.0" mnemonicParsing="false" onAction="#onClick" text="Button" />
<TextField fx:id="textField" layoutX="237.0" layoutY="213.0" />
</children>
</AnchorPane>
Second controller :
public class SecondController implements Initializable {
#FXML private Label labelField;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void setText(String name){
this.labelField.setText(name);
}
}
Second.fxml:
`<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
</children>
</AnchorPane>
Your second FXML file is missing the fx:controller attribute. Therefore, when you load it, there is no controller, and so loader.getController() returns null.
Consequently, when you call sn.setText(...), you get a null pointer exception.
Just add the missing fx:controller attribute to the second FXML file. Assuming that SecondController is in the application package, this will look like:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<AnchorPane prefHeight="450.0" prefWidth="650.0" fx:controller="application.SecondController" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
</children>
</AnchorPane>
Also note that it's very bad practice to silently "squash" exceptions, by using
try {
/* ... */
} catch (Exception e) { }
It's especially bad if the ensuing code depends on the code in the try block succeeding. In particular, in this case if your call to load() results in an exception (which may happen for a multitude of reasons, including the FXML not being found or being invalid, the fx:id and field names, or types,
not matching, method names for handlers not matching or having the wrong parameters, etc.), then the load method will silently fail and the controller will not be set in the loader. This will again result in sn being null.
Here's a complete example using your code, but with the other errors fixed as well as the missing fx:controller attribute inserted:
application/Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("First.fxml")));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
application/First.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FirstController">
<children>
<Button fx:id="btn" layoutX="285.0" layoutY="264.0" mnemonicParsing="false" onAction="#onClick" text="Button" />
<TextField fx:id="textField" layoutX="237.0" layoutY="213.0" />
</children>
</AnchorPane>
application/FirstController.java:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class FirstController implements Initializable {
#FXML private TextField textField;
#FXML private Button btn;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void onClick(ActionEvent event) throws Exception {
//switching to new scene
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("/application/Second.fxml"));
loader.load();
SecondController sn=loader.getController();
sn.setText(textField.getText());
Parent p=loader.getRoot();
Stage window=new Stage();
window.setScene(new Scene(p));
window.setTitle("dfd");
window.show();
}
}
application/Second.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<AnchorPane prefHeight="450.0" prefWidth="650.0" fx:controller="application.SecondController" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
</children>
</AnchorPane>
application/SecondController.java
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class SecondController implements Initializable {
#FXML private Label labelField;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
public void setText(String name){
this.labelField.setText(name);
}
}

Scene Builder Exception in Application (NullPointerException)

I have installed Scene Builder and opened a new FXML Project just to test it out. I have seen on other computers that when a new FMXL Project is opened, the default code generates a button that displays text when clicked. However, when I try to run the main method of the default FXML Project, I get a stack error with a NullPointerException. I have searched many question threads on how to fix it but none of them really give me a concrete answer. Any help would be appreciated.
## JavaFXApplication2.java ##
package javafxapplication2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author Hafiz
*/
public class JavaFXApplication2 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
## FXMLDocumentController.java ##
}
package javafxapplication2;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
/**
*
* #author Hafiz
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
## FXMLDocument.fxml ##
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
I think that your problem is caused by private method "handleButtonAction". Turn it to public and try then. Greetings

javafx fxml program error(no main method)

i'm fairly new to javafx and fxml. I'm trying to teach myself! However, when I was creating a program for a simple login GUI I came into an issue when I finally tried to run my program. It told me there was no main method in my class and I'm unsure of how to fix it. Any ideas?
My program creates a login screen and when you enter "test" for the username and password it'll take you to another scene.
Here is my Login.java
package com;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
/**
*
* #author Tyler
*/
public class Login extends Application{
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Login");
stage.show();
}
}
Here is my LoginController.java
package com;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* #author Tyler
*/
public class LoginController implements Initializable {
#FXML
private Label lblMessage;
#FXML
private TextField txtUsername;
#FXML
private PasswordField txtPassword;
#FXML
private void btnLoginAction(ActionEvent event) throws Exception{
if(txtUsername.getText().equals("test") && txtPassword.getText().equals("test")){
((Node) (event.getSource())).getScene().getWindow().hide();
Parent parent = FXMLLoader.load(getClass().getResource("DateSelection.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.setTitle("Date Selection");
stage.show();
}else{
lblMessage.setText("Username or Password is invalid!");
}
}
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Here is my Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" fx:id="lblMessage" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.LoginController">
<children>
<PasswordField fx:id="txtPassword" layoutX="200.0" layoutY="200.0" prefHeight="30.0" prefWidth="200.0" promptText="Password" />
<TextField fx:id="txtUsername" layoutX="200.0" layoutY="140.0" prefHeight="30.0" prefWidth="200.0" promptText="Username" />
<Button fx:id="btnLogin" layoutX="269.0" layoutY="251.0" mnemonicParsing="false" onAction="#btnLoginAction" prefHeight="30.0" text="Login">
<font>
<Font size="14.0" />
</font></Button>
<Label fx:id="lblMessage" layoutX="283.0" layoutY="71.0" text="Label" />
</children>
</AnchorPane>
Here is my DateSelectionController.java
package com;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
/**
* FXML Controller class
*
* #author Tyler
*/
public class DateSelectionController implements Initializable {
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Here is my DateSelection.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.DateSelectionController">
<children>
<Label layoutX="191.0" layoutY="164.0" text="Welcome">
<font>
<Font name="System Bold" size="50.0" />
</font>
</Label>
</children>
</AnchorPane>
From Oracle:
The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.
So one solution is to make sure it's being built in a way that fully supports the JavaFX tools. The other solution is to add a main method to starts the application. That would avoid any potential problems like this, and doesn't cause any problems in the case where it's not needed.
Your main method should look like this:
public static void main(String[] args){
Application.launch(Login.class, args);
}
That will simply pass control on to JavaFX to handle like it would normally.

JavaFX8 SceneBuilder Custom control behavior

I am trying to create a custom control in JavaFX8. What I would like to achieve is an AnchorPane with a header and a footer that can each contain controls. I've noticed that any events I create on the control will not fire in "design mode". How would I implement dragging and dropping of new Nodes into my control using SceneBuilder?
Here is a simple example. I have this control.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root minHeight="300" minWidth="300" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane fx:id="test" prefHeight="209.0" prefWidth="191.0">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TextField fx:id="textField" layoutX="14.0" layoutY="14.0" />
<HBox fx:id="hbox" layoutX="14.0" layoutY="40.0" prefHeight="100.0" prefWidth="100.0" style="-fx-background-color: RED;" />
</children>
</AnchorPane>
</children>
</fx:root>
Here is the controller:
///*
// * To change this license header, choose License Headers in Project Properties.
// * To change this template file, choose Tools | Templates
// * and open the template in the editor.
// */
package javafxbeancontroltest;
//
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
//
///**
// *
// * #author ajmiro
// */
public class FXMLDocumentController extends AnchorPane
{
#FXML
StringProperty labelText = new SimpleStringProperty("Initial Value");
public String getLabelText() { return labelText.get(); }
public void setLabelText(String newText) {labelText.set(newText);}
public StringProperty labetTextProperty() { return labelText; }
public Button getButton() {return button;}
#FXML
private AnchorPane test;
public AnchorPane getTest() {return test;}
#FXML
private Label label;
#FXML
public Button button;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
//label.setText("Hello World!");
label.setText(getLabelText());
//label.setText(textField.getText());
}
public FXMLDocumentController() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/javafxbeancontroltest/FXMLDocument.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
How would I implement being able to drag a control onto the red HBOX at design time in SceneBuilder 2.0?

Is there a way to select a text field in code without clicking on it (javafx)?

I am not sure how to or even if it is possible to select text fields in code. I could not find anything on the subject. If the is indeed a way to do so i have a template that could be modified to demonstrate how this could be done.
Here is the template/sample code:
Main class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
Scene scene = new Scene(root,600,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Test");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainControl class:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class MainControl implements Initializable {
#FXML
Button button;
#FXML
TextField field1;
#FXML
TextField field2;
boolean field1selected=true;
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void switchTextFeild(ActionEvent event){
/*
The code for switch on witch TextField is selected would go here
*/
}
}
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainControl">
<children>
<TextField fx:id="field1" layoutX="6.0" layoutY="2.0" prefHeight="394.0" prefWidth="149.0" />
<TextField fx:id="field2" layoutX="445.0" layoutY="2.0" prefHeight="394.0" prefWidth="149.0" />
<Button fx:id="button" layoutX="177.0" layoutY="14.0" mnemonicParsing="false" onAction="#switchTextFeild" prefHeight="95.0" prefWidth="239.0" text="Switch" textAlignment="CENTER" wrapText="true">
<font>
<Font size="24.0" />
</font>
</Button>
</children>
</AnchorPane>
You can use
field1.requestFocus();
in your initialize() method, so your TextField field1 will be focused after your app is started.
But notice, you have to wrap the requestFocus() call within a
Platform.runLater(new Runnable() {
#Override
public void run() {
field1.requestFocus();
}
});
because this should be done on the JavaFX Application Thread and not on the Launcher Thread, so if you would only call field1.requestFocus() this wont have any effect on our TextField.
This is very simple
filedname.requestFocus();
fieldname.selectAll();
you have to first get the focus then use selectAll()function . if you will not use requestFocus() function then selectAll() will not work
The solution is simple for classic java. You can use code below for select text in a textField without doubleclicking on it (no idea about javafx).
field1.requestFocus();
field1.setSelectionStart(0);
field1.setSelectionEnd(field1.getText().length());

Categories

Resources