Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a javafx scene without the use of a button event

Tags:

java

javafx

I have created a new scene called quiz_scene and the quiz_scene_controller in a javafx project. This scene simulates a question and answer quiz game. The object of the scenes are the question_label and the answer_text_field or the answer_buttons. In total the number of questions are 7 and after the system directs into the next scene.

I want when I am entering the system to perform a query in an initialize function to a sql database with the questions and answers to fetch the current questions and answers. This procedure needs to be occured once when the system open the quiz_scene (and also every time the answer button is pressed). I tried to add the functionality of the query inside the **@FXML public void initialize()**, however it did not work.

When I tried to add the functionality in a button event the query worked and I am able to change the label and the buttons of the scene using the info from the db. One of the issues I found is the fact that when I am performing the query I am creating a reference to my main class which contains the loader for the quiz_scene. Without event I am not retrieving the current object and I am retrieving a null one. How I can do so without the use of a button? Why my object is null?

The code can be found here: code.

public class quizSceneController{

 private Flow flow;
 public void setMain(Flow flow){ this.flow = flow;}
 @FXML Button answerbutton;
 @FXML Text timeField, scoreField;
 @FXML public Label question, showQuestionCounter, answer, questionId, questionType, questionSpeed;
 NextQuestion nextQuestion;
 public String temp, temp2;
 public GridPane takingTestPane = new GridPane();

 public void chooseNextQuestion(Flow flow){

    try {
        this.flow.nextQ = false;
        this.flow.startTime = System.currentTimeMillis();
        //Choosing the next question
        String sql = "select * from Questions where Subject = ? and Level = ?  and questionId = ?";
        PreparedStatement pst = 
this.flow.connectionQuestions.prepareStatement(sql);
        pst.setString(1, this.flow.textSubjectQTest);
        pst.setString(2, this.flow.user_course_level);
        pst.setString(3, this.flow.list.get(counter));
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {

            temp = rs.getString("Question");
            temp2 = rs.getString("Answer");

            pst.execute();
            pst.close();
        } else {
            System.out.println("No data for this subject");
        }

    } catch (Exception a) {
        System.out.println(a);
    }
 }

 @FXML private void myButton(ActionEvent event){

    chooseNextQuestion(this.flow);
    this.question.setText(temp);
 }
 @FXML public void initialize(){

    chooseNextQuestion(this.flow);
    this.question.setText(temp);
 }
}

Finally the way I am loading the fxml from the main class is the following:

loader = new FXMLLoader();         
loader.setLocation(Flow.class.getResource("/gui/quiz/quiz.fxml"));
quizPane = loader.load();
quizSceneController quiz = loader.getController();

EDIT: How can instead of using the button to initialize the whole scene using the code of chooseNextQuestion?

like image 429
christosh Avatar asked May 16 '17 08:05

christosh


People also ask

How do I make a button not clickable in JavaFX?

In Swing, we can disable a button like this: JButton start = new JButton("Start"); start. setEnabled(false);

What is initialize method in JavaFX?

[...] the controller can define an initialize() method, which will be called once on an implementing controller when the contents of its associated document have been completely loaded [...] This allows the implementing class to perform any necessary post-processing on the content.

Can JavaFX work without Scene Builder?

You don't have to use FXML or SceneBuilder. You can simply create the objects yourself and add them to your Scene/Stage yourself. It's entirely open as to how you implement it. I've implemented a Screen Management library where it handles either FXML or manually created screens.


1 Answers

Your controller needs to implement the Initializable interface

import javafx.fxml.Initializable;

public final class QuizSceneController implements Initializable {

     @Override
     public void initialize(URL location, ResourceBundle resources) {
           // do stuff
     }
}
like image 54
Westranger Avatar answered Oct 01 '22 19:10

Westranger