V
V
Vadim Loginov2016-12-27 21:39:26
Java
Vadim Loginov, 2016-12-27 21:39:26

JavaFX code not working?

The app launches, but when you press the up arrow, nothing happens.
The code:

package sample;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
    private FlowPane pane;
    private Scene scene;
    private Stage stage;

    @FXML
    private Circle crc;

    @Override
    public void start(Stage stage) throws Exception{
        this.stage = stage;
        try {
            pane = (FlowPane) FXMLLoader.load(Main.class.getResource("sample.fxml"));
        } catch (IOException e){
            e.printStackTrace();
        }
        stage.setTitle("Hello World");
        scene = new Scene(pane,600,600);
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {

        launch(args);
    }

    @FXML
    public void initialize(){
        crc.setOnKeyTyped(event -> {
            if (event.getCode() == KeyCode.UP){
                crc.setFill(Color.RED);
            }
        });
    }

}

FXML:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.shape.Circle?>

<FlowPane fx:controller="sample.Main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.92" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Circle fx:id="crc" fill="DODGERBLUE" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
   </children>
</FlowPane>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2016-12-28
@KeLaTy

Perhaps you did these as a test of javaFX. Nevertheless, a couple of tips:
Firstly : the architecture is fundamentally wrong - do not hang anything on the main class that is related to form elements. It is much more correct to make a new controller for sample.fxml - and naturally make it implement it for initialization (implements Initializable) - then the initialize method will be called upon loading.
Secondly : to make it stupidly work: at the end of the public void start method, add

pane.getScene().setOnKeyPressed(event -> {
            if (event.getCode() == KeyCode.UP){
                System.out.println("2");
            }
        });

D
Dmitry Alexandrov, 2016-12-28
@jamakasi666

In theory, it won’t work for you at all. Circle crc is not related to the shape at all.
To make it work, you must first create an instance of Circle in the constructor, and then do pane.getChildren().add(crc) after which the Circle will be added to the pane.
In general, I recommend using controllers for cases of interaction through FXML.
In the form builder, draw a form and there is a magic button that will immediately generate a valid controller code that you just need to copy-paste to your favorite IDE.
I recommend looking at this example to understand how controllers work and how you can catch events through JavaScript and fasten CSS styles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question