Read JavaFx Application - javafx-8

I created a small javaFx Program. The Program just displays random points. Now I want to get and read the generated Stage from a different class and read the co-ordinates. Is it possible ?
I tried creating a class variable to get stage but its null always.
I added a class variable and assigned the prepared Stage object to the variable. I am then trying to get the Stage object from the variable.
public class DCGUI extends Application {
private Stage primaryStage1;
public static void main(String[] args) {
launch(args);
Platform.exit();
}
#Override
public void start(Stage primaryStage) throws Exception {
Random r = new Random(64);
// List<Integer> points = r.ints(1000, 0, 400).boxed().collect(Collectors.toList());
List<Node> cList = new ArrayList<>();
Line line = null;
Circle c = null;
for (int i = 0; i < 50001; i++) {
//System.out.println(r.nextInt());
c = new Circle(r.nextInt(400), r.nextInt(400), 0.0125);
c.setStroke(Color.RED);
c.setId(i+"");
cList.add(c);
}
Group group = new Group(cList);
Scene scene = new Scene(group, 400, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Dynamic Connectivity");
primaryStage1 = primaryStage;
primaryStage.show();
}

Let's assume the following code example. By calling the getMainStage() method you can have access to your Stage object.
Of course, the main.fxml file is binded with the MainController class
Main.java
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader("path/to/main.fxml").toFile().toURI().toURL());
MainController.setMainStage(stage); <---- !
Parent root = loader.load();
stage.setScene(new Scene(root, 1400, 850));
stage.show();
}
public static void main(String[] args) {
launch(args);
}}
MainController.java
public class MainController{
// fxml view elements...
private static Stage mainStage;
//...
public static Stage getMainStage() {
return mainStage;
}
}

Related

How to run JavaFX Window in other Class

as in title, i need help with Running JavaFX in other class. For exmaple, from Main i want to type something like 'new TempClass()' and then i expect new Window but i got nothing xD Thank you in advance for solve the problem! <3
Main:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
new TempClass();
}
public static void main(String[] args) {
launch(args); // when i launch(args) the 'start' func starting btw
}
}
TempClass:
public class TempClass extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane root = new Pane();
root.setPrefSize(300,300);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is one of few ways which solved my problem:
Main:
public class Main {
public static void main(String[] args) {
Application.launch(TempClass.class, args);
}
}
TempClass:
public class TempClass extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane root = new Pane();
root.setPrefSize(300,300);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}

How to position controls binding there position into the stage's size using the controller class in JavaFx?

I found some thing similar in this link
How to call functions on the stage in JavaFX's controller file
and here is what I found in one of the answers
StageTrackingSample.java
public class StageTrackingSample extends Application {
#Override public void start(final Stage stage) throws Exception {
final FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"stagetracking.fxml"
)
);
final Parent root = (Parent) loader.load();
final StageTrackingController controller = loader.getController();
controller.initData(stage);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
StageTrackingController.java
public class StageTrackingController {
#FXML private Label stageX;
public void initialize() {}
public void initData(final Stage stage) {
stageX.textProperty().bind(
Bindings.format(
"(%1$.2f, %2$.2f)",
stage.xProperty(),
stage.yProperty()
)
);
}
}
I wanted to position the progressIndicator in the middle of the window, so I tried this in my controller class
Controller.java
public void initInterface(Stage stage) {
progressIndicator.layoutXProperty().bind(stage.widthProperty().divide(2));
progressIndicator.layoutYProperty().bind(stage.heightProperty().divide(2));
}
and this in Main.java
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
final FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
final Parent root = loader.load();
final Controller controller = loader.getController();
final Scene scene = new Scene(root);
controller.initInterface(primaryStage);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
it doesn't work even when I tried passing the scene or the anchorpane(which is defined in the fxml file) as a parameter into initInterface method, it seems that it has problem with binding progressIndicator properties
by using the layoutXProperty and layoutYProperty and binding them to the Stage's width and height you must be trying to put it in the lower right hand corner of the stage. You can achieve this much easier, and JavaFX insists you do so, by using layouts in your scene and making the scene fill the entire area in question.
"From the Javadocs for the layoutX property:
If the node is managed and has a Region as its parent, then the layout region will set layoutX according to its own layout policy. If the node is unmanaged or parented by a Group, then the application may set layoutX directly to position it.
What this means is that the LayoutX/Y properties are controlled by the parent (and so it should be able to 'set' them). However, when you bind them they cannot be set anymore resulting in " A bound value cannot be set" exception."
Here is a good tutorial on regions and how to get things to layout in SceneBuilder as you please. If you're not using SceneBuilder I recommend it.
https://www.youtube.com/watch?v=zvgWgpGZVKc&list=PL6gx4Cwl9DGBzfXLWLSYVy8EbTdpGbUIG&index=35

How to access an object in a class A from a class B

I have the following main class:
public class JavaFXApplication4 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Add to the list");
TextArea peopleList = new TextArea();
String name = "name";
String surname = "surname";
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Person p = new Person(name, surname);
}
});
StackPane root = new StackPane();
root.getChildren().addAll(btn, peopleList);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("People list");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
I want that the constructor of the class Personadds name and surname into the TextArea peopleList into the main class.
public class Person {
public String name, surname;
public Person(String n, String s){
}
}
How can I access elements of the main class from other classes?
Make those variables either instance variables or class variables , Since local variable can be accessed only inside the function in which they are declared .
if you use instance variable make them private and Use getters and setters to access them
private TextArea peopleList = new TextArea();
public TextArea getPeopleList(){ // getter to access
return peopleList;
}
public void setPeopleList(TextArea peopleList){
this.peopleList=peopleList;
}
Made the object of MainClass in other class and use them using getters and setters.
Or Made them static/class variables in Main class so can be accesed with the name of Main Class.
static TextArea peopleList = new TextArea();
and in Other class
MainClass.peopleList

get scene from MenuItem

How i can get scene from MenuItem? i tried this code:
public class MainController implements Initializable {
#FXML
private MenuBar menuBar;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML
public void show(ActionEvent event) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(menuBar.getScene().getWindow());
stage.show();
}
}
the above code does not generate an error, but it does not display the window!!!
Well, in your Controller class add a variable for Scene with the getters and setters.
Then you can do something like this:
#Override
public void start(Stage primaryStage)throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml"));
Parent root = loader.load();
MainController mainControls = loader.getController();
Scene scene = new Scene(root, 300, 250);
mainControls.setScene(scene);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
Now the scene is available to all items in that class.
It looks like you're trying to create a Dialog of sorts?
If so don't use the Main scene... create a new Stage and show that with it's content...
Although I believe the JDK comes with Dialog support now.

Launch JavaFX application from another class

I need to start a javafx Application from another "container" class and call functions on the Application, but there doesn't seem to be any way of getting hold of a reference to the Application started using the Application.launch() method. Is this possible?
Thanks
Suppose this is our JavaFX class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class OKButton extends Application {
#Override
public void start(Stage stage) {
Button btn = new Button("OK");
Scene scene = new Scene(btn, 200, 250);
stage.setTitle("OK");
stage.setScene(scene);
stage.show();
}
}
Then we may launch it from another class like this:
import javafx.application.Application;
public class Main {
public static void main(String[] args) {
Application.launch(OKButton.class, args);
}
}
I had the same problem as this and got round it using this hack:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.concurrent.CountDownLatch;
public class StartUpTest extends Application {
public static final CountDownLatch latch = new CountDownLatch(1);
public static StartUpTest startUpTest = null;
public static StartUpTest waitForStartUpTest() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return startUpTest;
}
public static void setStartUpTest(StartUpTest startUpTest0) {
startUpTest = startUpTest0;
latch.countDown();
}
public StartUpTest() {
setStartUpTest(this);
}
public void printSomething() {
System.out.println("You called a method on the application");
}
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
Label label = new Label("Hello");
pane.setCenter(label);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
and then the class you are launching the application from:
public class StartUpStartUpTest {
public static void main(String[] args) {
new Thread() {
#Override
public void run() {
javafx.application.Application.launch(StartUpTest.class);
}
}.start();
StartUpTest startUpTest = StartUpTest.waitForStartUpTest();
startUpTest.printSomething();
}
}
Hope that helps you.
Launch JavaFX in other Class using Button:
class Main extends Application{
public void start(Stage s)throws Exception{
event();
s.show();
}
public void event(){
btn.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent ae){
Stage s = new Stage();
new SubClassName().start(s);
}
});
}
public static void main(String[] args) {
launch(args);
}
}
class SubClassName{
public void start(Stage s){
Pane pane = new Pane();
Scene addFrame = new Scene(pane,280,450);
s.setScene(addFrame);
s.show();
}
}
I'm not sure what you're trying to achieve, but note that you can e.g call from another class Application.launch to start the JavaFX Application thread and Platform.exit to stop it.
The above ways of invoking other javafx class from another sometimes work. Struggling to find an ultimate way to do this brought me to the following walk around:
Suppose this is the javafx class that exteds Application we wish to show from a different class, then we should add the following lines
class ClassToCall extends Application{
//Create a class field of type Shape preferably static...
static Stage classStage = new Stage();
#Override
public void start(Stage primaryStage){
// Assign the class's stage object to
// the method's local Stage object:
classStage = primaryStage ;
// Here comes some more code that creates a nice GUI.....
// ......
}
}
And now from the other place in the project, in order to open the window
that the above class creates do the following:
// Suppose we want to do it with a button clicked:
btn1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//create an object of the class you wish to invoke its
//start() method:
ClassToCall ctc = new ClassToCall();
// Then call its start() method in the following way:
ctc.start(ClassToCall.classStage);
}// End handle(ActionEvent event)
});// End anonymous class