Home:ALL Converter>buttons not showing in java fx application

buttons not showing in java fx application

Ask Time:2018-10-20T23:39:55         Author:Kashif Mehmood 3314-FBASBSSEF1

Json Formatter

The buttons that i have made in sample.fxml are not showing after compilation i created them using scenebuilder. I tried compiling again doing rebuilds too. After trying everything i became helpless and the problem was still their after all that . A little help will be good.

main.java

package sample;

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 {

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            Scene scene = new Scene(root, 300, 275);
            primaryStage.setTitle("Hello World");

            primaryStage.show();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

controller.java

package sample;

import javafx.event.ActionEvent;

import java.util.Random;

public class Controller {

    public void generateRandom(ActionEvent event)
    {
        Random rand = new Random();
        int number = rand.nextInt(50)+1;
        System.out.println(Integer.toString(number));
    }
}

sample.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="476.0" prefWidth="497.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="clickme" layoutX="192.0" layoutY="299.0" mnemonicParsing="false" onAction="#generateRandom" prefHeight="52.0" prefWidth="93.0" text="Click me" />
      <Label layoutX="155.0" layoutY="152.0" prefHeight="71.0" prefWidth="166.0" />
   </children>
</AnchorPane>

Author:Kashif Mehmood 3314-FBASBSSEF1,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/52907342/buttons-not-showing-in-java-fx-application
yy