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?