0

My first question is this: Why is it that you are not able to call some methods from outside your main class? For instance if if I try to put a System.out.println outside the main class, it has to be in a method, but why is this?

My more pressing question is this:

I'm trying to build a simple calculator in JavaFX, I have the following code: Main

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{
    Parent root = 
        FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 450));
    primaryStage.setResizable(false);
    Controller.disableDisplay(); // This is essentially what id like to do
    primaryStage.show();
}


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

Controller

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class Controller {
@FXML
private TextField display;
@FXML
private Button AC;
@FXML
private Button plusMinus;
@FXML
private Button percent;
@FXML
private Button divide;
@FXML
private Button seven;
@FXML
private Button eight;
@FXML
private Button nine;
@FXML
private Button times;
@FXML
private Button four;
@FXML
private Button five;
@FXML
private Button six;
@FXML
private Button minus;
@FXML
private Button one;
@FXML
private Button two;
@FXML
private Button three;
@FXML
private Button plus;
@FXML
private Button zero;
@FXML
private Button comma;
@FXML
private Button equal;

    public void disableDisplay() {
        display.setDisable(true);
    }
}

fxml

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>

<GridPane fx:controller="sample.Controller"
      xmlns:fx="http://javafx.com/fxml"
      alignment="center" hgap="10" vgap="10"
      stylesheets="/css/sample.css">

<VBox>
    <TextField fx:id="display" text="0" />
    <HBox>
        <Button fx:id="AC" styleClass="button" text="AC"/>
        <Button fx:id="plusMinus" styleClass="button" text="+/-"/>
        <Button fx:id="percent" styleClass="button" text="P"/>
        <Button fx:id="divide" styleClass="button" text="/"/>
    </HBox>
    <HBox>
        <Button fx:id="seven" styleClass="button" text="7"/>
        <Button fx:id="eight" styleClass="button" text="8"/>
        <Button fx:id="nine" styleClass="button" text="9"/>
        <Button fx:id="times" styleClass="button" text="X"/>
    </HBox>
    <HBox>
        <Button fx:id="four" styleClass="button" text="4"/>
        <Button fx:id="five" styleClass="button" text="5"/>
        <Button fx:id="six" styleClass="button" text="6"/>
        <Button fx:id="minus" styleClass="button" text="-"/>
    </HBox>
    <HBox>
        <Button fx:id="one" styleClass="button" text="1"/>
        <Button fx:id="two" styleClass="button" text="2"/>
        <Button fx:id="three" styleClass="button" text="3"/>
        <Button fx:id="plus" styleClass="button" text="+"/>
    </HBox>
    <HBox>
        <Button fx:id="zero" styleClass="button" text="0"/>
        <Button fx:id="comma" styleClass="button" text=","/>
        <Button fx:id="equal" styleClass="button" text="="/>
    </HBox>
</VBox>
</GridPane>

This is just a random example of a method I would like to implement, but I cannot do this as it gives the following message:

Non-static method 'disableDisplay()' cannot be referenced from a static context.

But making the disableDisplay method static doesn't resolve the issue either, so is there a de facto way to do something like this that I'm not aware of, or am I approaching this in the wrong way?

Jay Elston
  • 1,978
  • 1
  • 19
  • 38
Behar
  • 35
  • 11
  • "is there a de facto way to do something like this that im not aware of?" Yup: see https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D Apr 24 '18 at 23:46
  • As for your first question: pretty much all executable code has to go in a method. A method is a block of executable code that is called either explicitly (mostly), or sometimes as part of the lifecycle of the application (e.g. the `main()` method, or `start()` method in JavaFX, which are called when the application starts running, or an event handler, which is called when an event occurs). In either case, the method is executed at a specific time. If you have executable code outside a method, it just doesn't make sense - *when* is that code supposed to be executed? – James_D Apr 24 '18 at 23:49
  • 1
    Of course, the code you are trying to execute is completely equivalent to just calling `display.setDisable(true)` in the [`initialize()` method](https://docs.oracle.com/javase/9/docs/api/javafx/fxml/doc-files/introduction_to_fxml.html#controllers), or even just adding `disable="true"` to the `` FXML element. – James_D Apr 24 '18 at 23:56

0 Answers0