So I have this code here:
// import statements are ommitted
public class HomePage implements Initializable{
#FXML
private volatile MenuButton fullUserName;
public void initialize(URL url, ResourceBundle resourceBundle)
{
}
public void loadHomePage(Stage primaryStage) throws IOException {
Parent HomePage = FXMLLoader.load(LoginScreen.class.getResource("HomePage.fxml"));
Timer animTimer = new Timer();
animTimer.scheduleAtFixedRate(new TimerTask() {
int i=0;
#Override
public void run() {
if (i<100) {
primaryStage.setX(primaryStage.getX() - 4);
primaryStage.setY(primaryStage.getY() - 1);
primaryStage.setWidth(primaryStage.getWidth()+8.11);
primaryStage.setHeight(primaryStage.getHeight()+3.19);
} else {
this.cancel();
}
i++;
}
}, 0, 10);
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(10 * 101), ev -> {
primaryStage.setResizable(true);
primaryStage.setScene(new Scene(HomePage));
fullUserName.setText("User"); // Got NullPointerException at this line
}));
timeline.play();
}
public void start(Stage primaryStage) {
try {
Pane root = new Pane();
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
loadHomePage(primaryStage);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
HomePage.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="9.0" minWidth="16.0" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.HomePage">
<top>
<GridPane maxHeight="-Infinity" minHeight="-Infinity" prefHeight="32.5" style="-fx-background-color: #aeccc9;" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="11.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="27.5" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="11.5" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="10.0" />
<ColumnConstraints hgrow="ALWAYS" maxWidth="-Infinity" minWidth="-Infinity" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="15.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<MenuButton fx:id="fullUserName" mnemonicParsing="false" GridPane.columnIndex="4">
<items>
<MenuItem mnemonicParsing="false" text="Logout" />
<MenuItem mnemonicParsing="false" text="Exit" />
</items>
<graphic>
<ImageView fitHeight="22.0" fitWidth="22.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Cool-50.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets bottom="-1.5" left="-1.5" right="-1.5" top="-1.5" />
</padding>
</MenuButton>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Title" GridPane.columnIndex="3">
<font>
<Font name="Arial Rounded MT Bold" size="22.0" />
</font>
</Text>
</children>
</GridPane>
</top>
</BorderPane>
So as you can see, I got a NullPointerException when executing fullUserName.setText("User"); which means that the "fullUserName" menu button is not initialized.
Is it possible for the program to wait until fullUserName (or any other FXML variables for that matter) has been initialized so that I can execute the statement?
Related
Following is the fxml I am using for one of the Javafx forms:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane fx:id="CustomerAddLabel" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="683.0" prefWidth="824.0" style="-fx-background-color: aliceblue; -fx-border-color: black; -fx-border-radius: 5;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="c195.View_Controller.CustomerScreenController">
<children>
<Label alignment="TOP_CENTER" layoutX="300.0" layoutY="14.0" prefHeight="38.0" prefWidth="226.0" style="-fx-border-color: gray; -fx-border-radius: 5;" text="Customer" textAlignment="CENTER">
<font>
<Font name="System Bold Italic" size="25.0" />
</font>
</Label>
<AnchorPane layoutX="16.0" layoutY="102.0" prefHeight="404.0" prefWidth="363.0" style="-fx-background-color: white;">
<children>
<TableView fx:id="CustomerTable" layoutY="1.0" style="-fx-border-color: black; -fx-border-radius: 5;">
<columns>
<TableColumn fx:id="CustomerIDColumn" prefWidth="63.0" text="ID" />
<TableColumn fx:id="CustomerNameColumn" prefWidth="175.0" text="Customer Name" />
<TableColumn fx:id="CustomerPhoneColumn" prefWidth="123.0" text="Phone" />
<TableColumn fx:id="CustomerAddressColumn" prefWidth="123.0" text="Address" />
<TableColumn fx:id="CustomerPostalCodeColumn" prefWidth="123.0" text="Postal Code" />
<TableColumn fx:id="CustomerDivisionColumn" prefWidth="123.0" text="Division" />
<TableColumn fx:id="CustomerCountryColumn" prefWidth="123.0" text="Country" />
</columns>
</TableView>
</children>
</AnchorPane>
<ButtonBar layoutX="587.0" layoutY="564.0" prefHeight="40.0" prefWidth="200.0">
</ButtonBar>
<ButtonBar layoutX="500.0" layoutY="613.0" prefHeight="40.0" prefWidth="200.0">
<buttons>
<Button fx:id="CustomerBackButton" mnemonicParsing="false" onAction="#CustomerBackButtonHandler" text="Back" />
<Button fx:id="CustomerDeleteButton" mnemonicParsing="false" onAction="#CustomerDeleteButtonHandler" text="Delete" />
</buttons>
</ButtonBar>
<Label fx:id="CustomerLabel" layoutX="542.0" layoutY="52.0" prefHeight="40.0" prefWidth="90.0" textFill="#1924e8">
<font>
<Font name="System Bold Italic" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
Following is a snippet of the corresponding controller:
public class CustomerScreenController implements Initializable {
#FXML
private TableView<Customer> CustomerTable;
#FXML
private TableColumn<Customer, String> CustomerIDColumn;
#FXML
private TableColumn<Customer, String> CustomerNameColumn;
#FXML
private TableColumn<Customer, String> CustomerPhoneColumn;
#FXML
private TableColumn<Customer, String> CustomerAddressColumn;
#FXML
private TableColumn<Customer, String> CustomerPostalCodeColumn;
#FXML
private TableColumn<Customer, String> CustomerDivisionColumn;
#FXML
private TableColumn<Customer, String> CustomerCountryColumn;
#FXML
private Button CustomerBackButton;
private Parent root;
private Stage stage;
private ObservableList<Customer> customerOL = FXCollections.observableArrayList();
#Override
public void initialize(URL url, ResourceBundle rb) {
PropertyValueFactory<Customer, String> custCustomerIDFactory = new PropertyValueFactory<>("customerID");
PropertyValueFactory<Customer, String> custNameFactory = new PropertyValueFactory<>("customerName");
PropertyValueFactory<Customer, String> custPhoneFactory = new PropertyValueFactory<>("phone"); //String value "CustomerPhone" calls getCustomerPhone method
PropertyValueFactory<Customer, String> custCountryFactory = new PropertyValueFactory<>("country");
PropertyValueFactory<Customer, String> custDivisionFactory = new PropertyValueFactory<>("division");
PropertyValueFactory<Customer, String> custAddressFactory = new PropertyValueFactory<>("address");
PropertyValueFactory<Customer, String> custPostalCodeFactory = new PropertyValueFactory<>("postalCode");
CustomerIDColumn.setCellValueFactory(custCustomerIDFactory);
CustomerNameColumn.setCellValueFactory(custNameFactory);
CustomerPhoneColumn.setCellValueFactory(custPhoneFactory);
CustomerCountryColumn.setCellValueFactory(custCountryFactory);
CustomerDivisionColumn.setCellValueFactory(custDivisionFactory);
CustomerAddressColumn.setCellValueFactory(custAddressFactory);
CustomerPostalCodeColumn.setCellValueFactory(custPostalCodeFactory);
try {
updateCustomerTableView();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
My UI is appearing like this:
This is appearing cropped. And the full column name Country is not visible. But if I resize it to maximum, then I do see the buttons. How can I make it scrollable? Both the full pane and the TableView inside it so that this issue doesn't appear on the screen.
It looks like you are using AnchorPane incorrectly. I would use VBox or BorderPane. This example uses VBox.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="683.0" prefWidth="824.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label alignment="CENTER" prefHeight="38.0" prefWidth="226.0" style="-fx-border-color: gray; -fx-border-radius: 5;" text="Customer" textAlignment="CENTER">
<font>
<Font name="System Bold Italic" size="25.0" />
</font>
<VBox.margin>
<Insets bottom="60.0" top="20.0" />
</VBox.margin>
</Label>
<TableView fx:id="CustomerTable" style="-fx-border-color: black; -fx-border-radius: 5;" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="CustomerIDColumn" prefWidth="63.0" text="ID" />
<TableColumn fx:id="CustomerNameColumn" prefWidth="175.0" text="Customer Name" />
<TableColumn fx:id="CustomerPhoneColumn" prefWidth="123.0" text="Phone" />
<TableColumn fx:id="CustomerAddressColumn" prefWidth="123.0" text="Address" />
<TableColumn fx:id="CustomerPostalCodeColumn" prefWidth="123.0" text="Postal Code" />
<TableColumn fx:id="CustomerDivisionColumn" prefWidth="123.0" text="Division" />
<TableColumn fx:id="CustomerCountryColumn" prefWidth="123.0" text="Country" />
</columns>
<VBox.margin>
<Insets left="10.0" right="10.0" />
</VBox.margin>
</TableView>
<ButtonBar>
<buttons>
<Button fx:id="CustomerBackButton" mnemonicParsing="false" onAction="#CustomerBackButtonHandler" text="Back" />
<Button fx:id="CustomerDeleteButton" mnemonicParsing="false" onAction="#CustomerDeleteButtonHandler" text="Delete" />
</buttons>
<VBox.margin>
<Insets bottom="20.0" right="20.0" top="20.0" />
</VBox.margin>
</ButtonBar>
</children>
</VBox>
I wrote a code which adds buttons in a GridPane, everything works as it should when I run the code. Only problem is that the buttons in their cells are too far from each other, the "best" outcome I got is the one below, but it's not good since there's still a barrier between the buttons (and they aren't in the center of the grid).
The code for creating the buttons and adding them to the grid:
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
b = new Button(" ");
b.setMinSize(30, 30);
b.setMaxSize(30, 30);
b.setStyle("-fx-font-size:11");
bCont.TheBoard.add(b, i, j);
// bCont.TheBoard.setVgap(5.0);
// bCont.TheBoard.setHgap(5.0);
// GridPane.setMargin(b, new Insets(5, 5, 5, 5));
GridPane.setHalignment(b, HPos.CENTER);
GridPane.setValignment(b, VPos.CENTER);
GridPane.setVgrow(b, Priority.ALWAYS);
GridPane.setHgrow(b, Priority.ALWAYS);
}
}
ColumnConstraints constraints = new ColumnConstraints();
constraints.setPercentWidth(85 / columns);
RowConstraints rowconst = new RowConstraints();
rowconst.setPercentHeight(85 / rows);
for (int t = 0; t < columns; t++) {
bCont.TheBoard.getColumnConstraints().add(constraints);
}
for (int t = 0; t < rows; t++)
bCont.TheBoard.getRowConstraints().add(rowconst);
Example of the output (first image is board size of 3x3, second is 10x10):
And in-case the FXML code of the board is also needed:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="853.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MS.BoardCONTROLLER">
<children>
<GridPane fx:id="TheBoard" alignment="TOP_CENTER" layoutX="200.0" layoutY="118.0" AnchorPane.bottomAnchor="90.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<HBox alignment="BOTTOM_LEFT" fillHeight="false" layoutX="14.0" layoutY="560.0" prefHeight="51.0" prefWidth="290.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="700.0" AnchorPane.topAnchor="560.0">
<children>
<Button fx:id="BackMenu" mnemonicParsing="false" onAction="#BackMenu" text="Back to menu" />
<Button fx:id="ResetBoard" mnemonicParsing="false" onAction="#ResetBoard" text="Reset" textFill="RED">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Button>
<Button fx:id="Music" mnemonicParsing="false" onAction="#Music" text="Music">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</children>
</AnchorPane>
Thank you.
More info - if I make the scene's window smaller as shown in the image below, the cells will be closer to each other (and if I make it even smaller they will "step" on each other):
Try this code
Parent root = FXMLLoader.load(App.class.getResource("App.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Example");
primaryStage.setScene(scene);
primaryStage.show();
int i=0;
int j=0;
GridPane gridPane = (GridPane)scene.lookup("#mygrid");
for(i=0;i<30;i++) {
for(j=0;j<12;j++) {
Button button1 = new Button(" ");
gridPane.add(button1, i, j, 1, 1);
}
}
FXML
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.zigma.Controller">
<children>
<GridPane AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" fx:id="mygrid">
</GridPane>
<BorderPane maxHeight="50.0" minHeight="50.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0">
<center>
<Button fx:id="mybutton" maxHeight="50"
mnemonicParsing="false" prefHeight="50.0"
prefWidth="200.0" text="Button" visible="false" />
</center>
</BorderPane>
</children>
</AnchorPane>
You will get this output
I'm Creating a loading screen for my application which has complex Neumorphism Design using InnerShadow, although CPU and RAM usage is less, the application causes lagging effect for animation like Circular Progress Indicator of Gluon charm-glisten.jar as well as PathTransition animation. In this loading screen you can see a red rectangle moving around the design and the progress indicator in blue color in the innermost layer
Java Code:
package application;
import java.net.URL;
import java.util.*;
import com.gluonhq.charm.glisten.control.ProgressIndicator;
import javafx.animation.*;
import javafx.animation.PathTransition.*;
import javafx.application.*;
import javafx.concurrent.*;
import javafx.event.*;
import javafx.fxml.*;
import javafx.stage.*;
import javafx.util.*;
import javafx.scene.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
public class Main extends Application implements Initializable
{
private double xOffset = 0;
private double yOffset = 0;
#FXML
ProgressIndicator pg1;
static AnchorPane root;
private PathTransition pathTransitionCircle;
public void start(Stage primaryStage)
{
try
{
primaryStage.initStyle(StageStyle.TRANSPARENT);
root = FXMLLoader.load(getClass().getResource("Test1.fxml"));
Scene scene = new Scene(root,600,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.setFill(javafx.scene.paint.Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
root.setOnMousePressed(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
xOffset = e.getSceneX();
yOffset = e.getSceneY();
}
});
root.setOnMouseDragged(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
primaryStage.setX(e.getScreenX() - xOffset);
primaryStage.setY(e.getScreenY() - yOffset);
}
});
Rectangle rect2 = new Rectangle(0, 0, 20, 20);
rect2.setArcHeight(10);
rect2.setArcWidth(10);
rect2.setFill(Color.RED);
root.getChildren().add(rect2);
Path path2 = createEllipsePath(400, 200, 150, 150, 0);
path2.setLayoutX(57);
path2.setLayoutY(10);
root.getChildren().add(path2);
pathTransitionCircle = new PathTransition();
pathTransitionCircle.setDuration(Duration.seconds(6));
pathTransitionCircle.setPath(path2);
pathTransitionCircle.setNode(rect2);
pathTransitionCircle.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransitionCircle.setCycleCount(PathTransition.INDEFINITE);
pathTransitionCircle.setInterpolator(Interpolator.LINEAR);
pathTransitionCircle.setAutoReverse(false);
pathTransitionCircle.play();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
#Override
public void initialize(URL arg0, ResourceBundle arg1)
{
Task<Void> task = new Task<Void>()
{
protected Void call() throws Exception
{
for (int i = 0; i <= 100; i++)
{
updateProgress(i, 100);
Thread.sleep(40);
}
//Platform.runLater(() -> primaryStage.close());
return null;
}
};
pg1.progressProperty().unbind();
pg1.progressProperty().bind(task.progressProperty());
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>()
{
public void handle(WorkerStateEvent arg0)
{
try
{
/*primaryStage.close();
primaryStage = null;
System.gc();
FXML_Loader f1 = new FXML_Loader();
f1.intermediate();*/
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
private Path createEllipsePath(double centerX, double centerY, double radiusX, double radiusY, double rotate)
{
ArcTo arcTo = new ArcTo();
arcTo.setX(centerX - radiusX + 1); // to simulate a full 360 degree celcius circle.
arcTo.setY(centerY - radiusY);
arcTo.setSweepFlag(false);
arcTo.setLargeArcFlag(true);
arcTo.setRadiusX(radiusX);
arcTo.setRadiusY(radiusY);
arcTo.setXAxisRotation(rotate);
Path path = new Path();
path.getElements().addAll(
new MoveTo(centerX - radiusX, centerY - radiusY),
arcTo,
new ClosePath()); // close 1 px gap.
path.setStroke(Color.BLUE);
path.getStrokeDashArray().setAll(5d, 5d);
return path;
}
}
FXML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.gluonhq.charm.glisten.control.ProgressIndicator?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: transparent;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<children>
<VBox alignment="CENTER" layoutX="163.5" layoutY="63.5" prefHeight="290.0" prefWidth="290.0" style="-fx-background-color: #3D4956; -fx-background-radius: 150;">
<effect>
<InnerShadow color="#00000070" offsetX="-4.0" offsetY="-4.0">
<input>
<InnerShadow color="#a7a7a7bf" offsetX="4.0" offsetY="4.0" />
</input>
</InnerShadow>
</effect>
<children>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: #3D4956; -fx-background-radius: 150;" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</VBox.margin>
<effect>
<InnerShadow color="#0000008c" offsetX="4.0" offsetY="4.0">
<input>
<InnerShadow color="#a6a6a65c" offsetX="-4.0" offsetY="-4.0" />
</input>
</InnerShadow>
</effect>
<children>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: #3D4956; -fx-background-radius: 150;" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</VBox.margin>
<effect>
<InnerShadow color="#0000008c" offsetX="-4.0" offsetY="-4.0">
<input>
<InnerShadow color="#a6a6a65c" offsetX="4.0" offsetY="4.0" />
</input>
</InnerShadow>
</effect>
<children>
<VBox prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: #3D4956; -fx-background-radius: 150;" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</VBox.margin>
<effect>
<InnerShadow color="#0000008c" offsetX="4.0" offsetY="4.0">
<input>
<InnerShadow color="#a6a6a65c" />
</input>
</InnerShadow>
</effect>
<children>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: #3D4956; -fx-background-radius: 150;" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</VBox.margin>
<effect>
<InnerShadow color="#0000008c" offsetX="-4.0" offsetY="-4.0">
<input>
<InnerShadow color="#a6a6a65c" offsetX="4.0" offsetY="4.0" />
</input>
</InnerShadow>
</effect>
<children>
<AnchorPane prefHeight="58.0" prefWidth="141.0" style="-fx-background-color: #3D4956; -fx-background-radius: 150;" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</VBox.margin>
<effect>
<InnerShadow color="#0000008c" offsetX="4.0" offsetY="4.0">
<input>
<InnerShadow color="#a6a6a65c" offsetX="-4.0" offsetY="-4.0" />
</input>
</InnerShadow>
</effect>
<children>
<ImageView fitHeight="42.0" fitWidth="50.0" layoutX="49.0" layoutY="40.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#logo/database1.png" />
</image>
</ImageView>
<Label layoutX="57.0" layoutY="81.0" prefHeight="19.0" prefWidth="34.0" text="SQL" textFill="YELLOW">
<font>
<Font name="System Bold Italic" size="14.0" />
</font>
</Label>
<Label layoutX="38.0" layoutY="97.0" prefHeight="20.0" prefWidth="73.0" text="Developer" textFill="YELLOW">
<font>
<Font name="System Bold Italic" size="14.0" />
</font>
</Label>
<ProgressIndicator fx:id="pg1" layoutX="8.0" layoutY="7.0" prefHeight="108.0" prefWidth="122.0" progress="1.0" radius="60.0" />
</children>
</AnchorPane>
</children>
</VBox>
</children>
</VBox>
</children>
</VBox>
</children>
</VBox>
</children>
</VBox>
</children>
</AnchorPane>
Loading Screen Design
I am in the process of creating a javafx desktop application. The problem I'm encountering is if I add a controller to my fxml files the controls dissapear somehow. The program uses a fade transition to switch between the fxml files
AnchorPane reorderLevels,purchaseorder,onlineSales,generalLedger
,cashBook,payments,departmentalTransfers
,purchaseInvoice
,productMantainance,
branchTransfers,stockMovement,invoiceRegistration,stockTake;
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Override
public void initialize(URL url, ResourceBundle rb) {
//load all the fxml screens when the buttons are clicked
try {
//screens for the application
reorderLevels = FXMLLoader.load(getClass().getResource("ReorderLevels.fxml"));
purchaseorder = FXMLLoader.load(getClass().getResource("PurchaseOrder.fxml"));
onlineSales = FXMLLoader.load(getClass().getResource("OnlineSales.fxml"));
generalLedger = FXMLLoader.load(getClass().getResource("GeneralLedger.fxml"));
cashBook = FXMLLoader.load(getClass().getResource("CashBook.fxml"));
departmentalTransfers = FXMLLoader.load(getClass().getResource("DepartmentalTransfers.fxml"));
purchaseInvoice = FXMLLoader.load(getClass().getResource("PurchaseInvoice.fxml"));
productMantainance = FXMLLoader.load(getClass().getResource("ProductMantainance.fxml"));
branchTransfers = FXMLLoader.load(getClass().getResource("BranchTransfers.fxml"));
stockMovement = FXMLLoader.load(getClass().getResource("StockMovement.fxml"));
payments = FXMLLoader.load(getClass().getResource("Payments.fxml"));
invoiceRegistration = FXMLLoader.load(getClass().getResource("InvoiceRegistration.fxml"));
stockTake = FXMLLoader.load(getClass().getResource("StockTake.fxml"));
//set each and every individual node when the buttons are clicked
setNode(reorderLevels);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void setNode(Node node) {
holderPane.getChildren().clear();
holderPane.getChildren().add((Node) node);
//the transition between the screens
FadeTransition ft = new FadeTransition(Duration.millis(1));
ft.setNode(node);
ft.setFromValue(0.1);
ft.setToValue(1);
ft.setCycleCount(1);
ft.setAutoReverse(false);
ft.play();
}
/* screens */
#FXML
private void switchReorderLevels(ActionEvent event) {
setNode(reorderLevels);
}
#FXML
private void switchOnlineSales(ActionEvent event) {
setNode(onlineSales);
}
#FXML
private void switchGeneralLedger(ActionEvent event) {
setNode(generalLedger);
}
#FXML
private void switchPurchaseOrder(ActionEvent event) {
setNode(purchaseorder);
}
#FXML
private void switchCashBook(ActionEvent event) {
setNode(cashBook);
}
#FXML
private void switchAlert(ActionEvent event) {
setNode(purchaseorder);
}
#FXML
private void switchPayments(ActionEvent event) {
setNode(payments);
}
#FXML
private void switchDepartmentalTransfers(ActionEvent event) {
setNode(departmentalTransfers);
}
#FXML
private void switchPurchaseInvoice(ActionEvent event) {
setNode(purchaseInvoice);
}
#FXML
private void switchProductMantainance(ActionEvent event) {
setNode(productMantainance);
}
#FXML
private void switchBranchTransfers(ActionEvent event) {
setNode(branchTransfers);
}
//this is a screen that will displayone of the layouts that will show re order levels
<AnchorPane id="AnchorPane" prefHeight="713.0" prefWidth="1219.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="24.0" layoutY="17.0" prefHeight="30.0" prefWidth="285.0" style="-fx-font-size: 20; -fx-font-weight: bold; -fx-text-fill: #7387A8;" text="ReOrder Levels" />
<Separator layoutX="25.0" layoutY="54.0" prefHeight="5.0" prefWidth="1191.0" />
<Pane fx:id="tbl_reorderLevels" layoutX="25.0" layoutY="108.0" prefHeight="413.0" prefWidth="1174.0" style="-fx-background-color: #EFEFEF;">
<children>
<JFXComboBox fx:id="cmd_reOrder_productCode" layoutX="48.0" layoutY="33.0" prefHeight="25.0" prefWidth="267.0" promptText="Product Code" />
<JFXTextField fx:id="txt_reOrder_reorderLevel" labelFloat="true" layoutX="48.0" layoutY="91.0" prefHeight="25.0" prefWidth="267.0" promptText="Reorder Level" />
//this is the root layout that has buttons that will navigate between the screens using a fade transition
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<!-- root layout -->
<AnchorPane id="AnchorPane" nodeOrientation="LEFT_TO_RIGHT" prefHeight="623.0" prefWidth="1271.0" style="-fx-background-color: #fff;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller = "dashboard.FXMLDocumentController">
<children>
<Pane layoutX="-2.0" prefHeight="81.0" prefWidth="1272.0" style="-fx-background-color: #0b8dee;" styleClass="head-background" stylesheets="#style.css" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label fx:id="labelEstock" layoutX="204.0" layoutY="-3.0" prefHeight="88.0" prefWidth="536.0" style="-fx-shape: round;" stylesheets="#custom.css" text="Oasys Estock" textFill="WHITE">
<font>
<Font name="Brush Script MT Italic" size="36.0" />
</font>
</Label>
<!-- <ImageView fx:id="imageEstock" fitHeight="110.0" fitWidth="92.0" layoutX="102.0" layoutY="8.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image
url="#../../../../AndroidStudioProjects/OasysEstock/app/src/main/res/drawable/fortified_icon.png" />
</image>
</ImageView> -->
</children>
</Pane>
<VBox layoutY="85.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="518.0" prefWidth="122.0" style="-fx-background-color: #0b8dee; -fx-background-radius: 15;">
<children>
<JFXButton id="btnReorderLevels" fx:id="btnReorderLevels" buttonType="RAISED" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onAction="#switchReorderLevels" prefHeight="47.0" prefWidth="107.0" style="-fx-background-radius: 100;" text="ReOrder Levels" textFill="#f5f0f0">
<VBox.margin>
<Insets />
</VBox.margin>
<font>
<Font name="Calibri" size="12.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnInvoiceRegistration" onAction="#switchInvoiceRegistration" prefHeight="25.0" prefWidth="121.0" text="Invoice Registration" textFill="#f8f4f4" />
<JFXButton fx:id="btnPurchaseOrder" buttonType="RAISED" onAction="#switchPurchaseOrder" prefHeight="34.0" prefWidth="107.0" text="Purchase Order" textFill="#f5f0f0">
<font>
<Font name="Cambria" size="12.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnOnlineSales" onAction="#switchOnlineSales" prefHeight="39.0" prefWidth="107.0" text="Online Sales" textFill="#fff9f9">
<font>
<Font name="Calibri" size="12.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnGeneralLedger" onAction="#switchGeneralLedger" prefHeight="40.0" prefWidth="108.0" text="General Ledger" textFill="#f2eeee" />
<JFXButton fx:id="btnCashBook" onAction="#switchCashBook" prefHeight="35.0" prefWidth="104.0" text="Cash Book" textFill="#fffefe" />
<JFXButton fx:id="btnPayments" buttonType="RAISED" onAction="#switchPayments" prefHeight="25.0" prefWidth="114.0" text="Payments" textFill="#fcf6f6" />
<JFXButton fx:id="btnDepartmentalTransfers" buttonType="RAISED" layoutX="10.0" layoutY="205.0" onAction="#switchDepartmentalTransfers" prefHeight="25.0" prefWidth="114.0" text="Dept Transfers" textFill="#fcf6f6" />
<JFXButton fx:id="btnProductMantainance" buttonType="RAISED" layoutX="10.0" layoutY="230.0" onAction="#switchProductMantainance" prefHeight="56.0" prefWidth="121.0" text="Product Mantainance" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnPurchaseInvoice" buttonType="RAISED" layoutX="10.0" layoutY="255.0" onAction="#switchPurchaseInvoice" prefHeight="56.0" prefWidth="121.0" text="Purchase Invoice" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnBranchTransfers" buttonType="RAISED" layoutX="10.0" layoutY="311.0" onAction="#switchBranchTransfers" prefHeight="56.0" prefWidth="121.0" text="Branch Transfers" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnStockMovement" buttonType="RAISED" layoutX="10.0" layoutY="367.0" onAction="#switchStockMovement" prefHeight="56.0" prefWidth="121.0" text="Stock Movement" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnStockTake" buttonType="RAISED" layoutX="10.0" layoutY="448.0" onAction="#switchStockTake" prefHeight="56.0" prefWidth="121.0" text="Stock Take" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
</children>
<effect>
<InnerShadow blurType="TWO_PASS_BOX" choke="0.58" color="#212122" height="26.69" radius="10.620000000000001" width="17.79" />
</effect>
</VBox>
<AnchorPane fx:id="holderPane" layoutX="125.0" layoutY="96.0" prefHeight="507.0" prefWidth="1134.0"/>
</children>
</AnchorPane>
This question already exists:
JavaFX - get index row and index col by OnClick on GridPane [duplicate]
Closed 5 years ago.
I hope this is the last post I open about this.. I have tried everything but my coord on GridPane return always null and not Integer...
This is not a thread like this, it is a specific question, so please, don't close or do other actions. I have to solve this problem.
My actual condition:
Here is my code. My question is.. Why when i click on myGrid(GridPane) it returns always 'null' ? Thank you..
Here my controller in FXML:
Are you adding some type of Node to your GridPane? In this sample app StackPanes are added to each Grid. The same event handler is registered on all the StackPanes.
Main
package javafxapplication147;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication147 extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
/**
*
* #author blj0011
*/
public class FXMLDocumentController implements Initializable
{
#FXML
private Label lblMain;
#FXML
private void handleOnMouseClicked(MouseEvent event)
{
Node source = (Node)event.getSource();
Integer colIndex = (GridPane.getColumnIndex(source) == null) ? 0 : (GridPane.getColumnIndex(source));
Integer colRow = (GridPane.getRowIndex(source) == null) ? 0 : (GridPane.getRowIndex(source));
lblMain.setText(colIndex + " : " + colRow);
}
#Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication147.FXMLDocumentController">
<children>
<VBox layoutX="48.0" layoutY="14.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="0.0">
<children>
<GridPane gridLinesVisible="true" style="-fx-background-color: green;">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
</rowConstraints>
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
<children>
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="1" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="1" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="2" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="3" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="4" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="4" />
</children>
</GridPane>
<StackPane prefHeight="150.0" prefWidth="200.0">
<children>
<Label fx:id="lblMain" text="Label" />
</children>
</StackPane>
</children>
</VBox>
</children>
</AnchorPane>