I am working on a employee management system using java fx and mysql. Basically, when the user click on a row of the tableview, it stock the row data in a employee object, than to String variable. At the same times, an edit scene open. I want the TextField in the new scene to be set up with the previous variable that the row had.
The method displaySelected() is a mouseEvent method, so its in this method that I retrieve row data. Right after retrieving the data, I call switchEdit() method, its only purpose is to open the Edit scene.
*Note that retrieving data from row is working just fine, I print it to be sure.
Right after opening the new scene, I use setText method to set the data in the TextField, thats where the error occurs. I get :
Cannot invoke "javafx.scene.control.TextField.setText(String)" because "this.idEdit" is null
I doubled check, I got all the necessary #FXML import and I checked previous similar question but it didn't help or maybe I didn't know how to implement it in my own code...
I even tried to setText directly in switchEdit method, but I get the same mistakes, and I think the problem may occur because im using one controller on two scene?
I honestly did my best before asking my question, thanks for your help and Ill take any advice on the way I typed my answer so next time Ill be better at helping you to help me.
Board.java class control the dashboard scene and the edit scene :
public class Board {
#FXML
private TableView<employeeList> employee;
#FXML
private TableColumn<employeeList, String> firstColumn;
#FXML
private TableColumn<employeeList, String> genderColumn;
#FXML
private TableColumn<employeeList, String> idColumn;
#FXML
private TableColumn<employeeList, String> lastColumn;
#FXML
private TableColumn<employeeList, String> yoeColumn;
#FXML
private Button refresh;
public ObservableList<employeeList> data = FXCollections.observableArrayList();
public void refreshTable() {
//CLEAN TABLEVIEW BEFORE REFRESH
employee.getItems().clear();
try {
String query = "select * from employee";
DataBase connectLive = new DataBase();
Connection connectDb = connectLive.getConnection();
Statement st;
ResultSet rs;
st = connectDb.createStatement();
rs = st.executeQuery(query);
employeeList emp;
while (rs.next()) {
emp = new employeeList(rs.getInt("id"), rs.getString("firstname"), rs.getString("lastname"), rs.getString("gender"), rs.getString("yoe"));
data.add(emp);
}
connectDb.close();
} catch (Exception e) {
e.printStackTrace();
}
idColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("id"));
firstColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("firstname"));
lastColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("lastname"));
genderColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("gender"));
yoeColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("yoe"));
employee.setItems(data);
}
public void displaySelected(MouseEvent event) throws IOException {
employeeList emp = employee.getSelectionModel().getSelectedItem();
if (emp ==null) {
System.out.println("Ya R");
}
else {
String f = emp.getFirstname();
String l = emp.getLastname();
String i = String.valueOf(emp.getId());
String g = emp.getGender();
String y = emp.getYoe();
switchEdit();
idEdit.setText(i);
firstnameEdit.setText(f);
lastnameEdit.setText(l);
genderEdit.setText(g);
yoeEdit.setText(y);
// settEdit(f,l,i,g,y);
}
}
//EDIT
#FXML
private Button exitEdit;
#FXML
private Button buttonfinish;
#FXML
private TextField firstnameEdit;
#FXML
private TextField genderEdit;
#FXML
private TextField idEdit;
#FXML
private TextField lastnameEdit;
#FXML
private TextField yoeEdit;
#FXML
private AnchorPane paneEdit;
public void exitEdit(ActionEvent e) {
Stage stage;
stage = (Stage) paneEdit.getScene().getWindow();
stage.close();
}
public void exit() {
Stage stage;
stage = (Stage) paneEdit.getScene().getWindow();
stage.close();
}
public void switchEdit() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/application/Edit.fxml"));
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Stage primaryStage = new Stage();
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
}
public void switchE(ActionEvent e) throws IOException {
switchEdit();
}
}
fxml code for the board:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Board">
<children>
<AnchorPane fx:id="pane2" prefHeight="860.0" prefWidth="920.0" style="-fx-background-color: #1B2430;">
<children>
<VBox layoutX="60.0" layoutY="25.0" prefHeight="797.0" prefWidth="777.0" style="-fx-background-color: #EEEEEE; -fx-background-radius: 30;">
<children>
<AnchorPane prefHeight="28.0" prefWidth="777.0">
<children>
<Button fx:id="exitButton" layoutX="734.0" layoutY="20.0" mnemonicParsing="false" onAction="#exit" prefHeight="20.0" prefWidth="20.0" style="-fx-background-radius: 100; -fx-background-color: #7b3733;" text="X" textFill="#eeeeee">
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color>
</DropShadow>
</effect>
<font>
<Font name="Consolas" size="12.0" />
</font>
</Button>
</children>
</AnchorPane>
<HBox alignment="CENTER" prefHeight="119.0" prefWidth="737.0" style="-fx-background-color: #336e7b; -fx-background-radius: 30;">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" text="Dash Board" textFill="WHITE">
<font>
<Font name="Calibri Light" size="24.0" />
</font>
<HBox.margin>
<Insets right="30.0" />
</HBox.margin>
</Label>
</children>
<VBox.margin>
<Insets left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</HBox>
<HBox prefHeight="42.0" prefWidth="581.0">
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" />
<BorderPane prefHeight="38.0" prefWidth="1026.0">
<right>
<TextField prefHeight="25.0" prefWidth="219.0" promptText="🔎 Search keywords" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets right="20.0" />
</BorderPane.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
</right>
<left>
<Button fx:id="refresh" mnemonicParsing="false" onAction="#refreshTable" prefHeight="25.0" prefWidth="82.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;" text="🗘" textFill="WHITE" textOverrun="CLIP" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="2.0" left="20.0" />
</BorderPane.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</Button>
</left>
</BorderPane>
</children>
</HBox>
<BorderPane prefHeight="364.0" prefWidth="777.0">
<center>
<TableView fx:id="employee" onMouseClicked="#displaySelected" prefHeight="345.0" prefWidth="737.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="idColumn" prefWidth="84.0" text="ID" />
<TableColumn fx:id="firstColumn" prefWidth="163.0" text="First Name" />
<TableColumn fx:id="lastColumn" prefWidth="163.0" text="Last Name" />
<TableColumn fx:id="genderColumn" prefWidth="163.0" text="Gender" />
<TableColumn fx:id="yoeColumn" prefWidth="163.0" text="Year Of Experience" />
</columns>
<BorderPane.margin>
<Insets left="20.0" right="20.0" />
</BorderPane.margin>
</TableView>
</center>
</BorderPane>
<BorderPane prefHeight="60.0" prefWidth="777.0">
<left>
<Button fx:id="addButton" mnemonicParsing="false" onAction="#addButton" prefHeight="25.0" prefWidth="200.0" style="-fx-background-color: #336e7b; -fx-background-radius: 30;" text="Add" textFill="WHITE" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="20.0" right="20.0" />
</BorderPane.margin>
<font>
<Font name="Calibri" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color></DropShadow>
</effect>
</Button>
</left>
<right>
<Button fx:id="deleteButton" mnemonicParsing="false" onAction="#switchD" prefWidth="200.0" style="-fx-background-color: #7b3733; -fx-background-radius: 30;" text="Delete" textFill="WHITE" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="20.0" right="20.0" />
</BorderPane.margin>
<font>
<Font name="Calibri" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color></DropShadow>
</effect>
</Button>
</right>
<center>
<Button fx:id="EditButton" mnemonicParsing="false" onAction="#switchE" prefHeight="25.0" prefWidth="200.0" style="-fx-background-color: #808080; -fx-background-radius: 30;" text="Edit" textFill="WHITE" BorderPane.alignment="CENTER">
<font>
<Font name="Calibri" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color></DropShadow>
</effect>
</Button>
</center>
</BorderPane>
<BorderPane prefHeight="57.0" prefWidth="777.0">
<VBox.margin>
<Insets top="90.0" />
</VBox.margin>
<center>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Copyright © 2022 - Amine Lakhal - Raphael Ducros" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</children>
</VBox>
</children>
</AnchorPane>
</children>
</AnchorPane>
fxml code for the edit page
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:id="paneEdit" prefHeight="775.0" prefWidth="493.0" style="-fx-background-color: #1B2430;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Board">
<children>
<VBox layoutX="65.0" layoutY="30.0" prefHeight="715.0" prefWidth="364.0" style="-fx-background-color: #EEEEEE; -fx-background-radius: 30;">
<children>
<Button fx:id="exitEdit" mnemonicParsing="false" onAction="#exitEdit" prefHeight="20.0" prefWidth="20.0" style="-fx-background-radius: 100; -fx-background-color: #7b3733;" text="X" textFill="#eeeeee">
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color>
</DropShadow>
</effect>
<font>
<Font name="Consolas" size="12.0" />
</font>
<VBox.margin>
<Insets left="328.0" top="13.0" />
</VBox.margin>
</Button>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="359.0" style="-fx-background-color: #336e7b; -fx-background-radius: 30;">
<children>
<ImageView>
<image>
<Image url="#edit.png" />
</image>
</ImageView>
<Label alignment="CENTER" contentDisplay="CENTER" text=" Edit Employee" textFill="WHITE">
<font>
<Font name="Calibri Light" size="24.0" />
</font>
<HBox.margin>
<Insets right="30.0" />
</HBox.margin>
</Label>
</children>
<VBox.margin>
<Insets left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</HBox>
<Label prefHeight="35.0" prefWidth="314.0" text="ID Employe *" textFill="#8a959a">
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
</Label>
<TextField fx:id="idEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<font>
<Font name="Consolas" size="12.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="314.0" text="Last Name" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="10.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="lastnameEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="473.0" text="First Name" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="15.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="firstnameEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="311.0" text="Gender " textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="15.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="genderEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="318.0" text="Year Of Experience" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="15.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="yoeEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="15.0" prefWidth="310.0" text=" Information can be modified later" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
<font>
<Font name="Calibri" size="11.0" />
</font>
</Label>
<HBox prefHeight="35.0" prefWidth="200.0">
<children>
<CheckBox mnemonicParsing="false" text="All data have been confirmed by manager" textFill="#8a959a">
<HBox.margin>
<Insets left="20.0" top="5.0" />
</HBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</CheckBox>
</children>
</HBox>
<Button fx:id="buttonfinish" mnemonicParsing="false" prefHeight="47.0" prefWidth="359.0" style="-fx-background-radius: 30; -fx-background-color: #336e7b;" stylesheets="#application.css" text="Finish" textFill="WHITE">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<font>
<Font name="Calibri Light" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color>
</DropShadow>
</effect>
</Button>
<Text fill="#8a959a" strokeType="OUTSIDE" strokeWidth="0.0" text="All sensitive data are subject to confidentiality rules in force in the province of Quebec. Any illegal use will be punished by law. 23:788:22|667" wrappingWidth="297.13671875">
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="10.0" />
</VBox.margin>
<font>
<Font name="Calibri" size="11.0" />
</font>
</Text>
</children>
</VBox>
</children>
</AnchorPane>
Don't use the same controller class (application.Board) for different FXML files.
Each time you load the FXML, the loader will create a new instance of the controller class, only initializing the FXML fields associated with the new FXML fields in the new controller instance.
In your case, the idEdit field is initialized in the controller instance created by the FXML Loader invoked in your switchEdit() method. But that instance differs from the instance that called the switchEdit() method.
Instead, create a new class to form the controller for the employee data editor.
Use MVC to share a model or pass parameters to the new controller. In your case, the shared model or parameters to pass would be the id or data of the selected employee.
The Makery JavaFX tutorial provides an illustrative example.
I'm designing a dashboard in JavaFX FXML. Here is a screenshot of the dashboard:
Here is FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.farhad.controllers.DashboardMenuController"
spacing="50.0"
id="dashboard-menu"
stylesheets="#../style/dashboard.css">
<HBox alignment="CENTER" spacing="5.0">
<ImageView>
<Image url="#../icons/user.png" />
</ImageView>
<VBox spacing="5.0" alignment="CENTER_LEFT">
<Label id="user-full-name" text="User full name"/>
<Label id="user-login-name" text="User login name"/>
</VBox>
</HBox>
<Separator />
<!-- About User -->
<VBox spacing="20.0">
<HBox>
<ImageView>
<Image url="#../icons/overview.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Overview"/>
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/accounts.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Accounts" />
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/transaction.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Transactions"/>
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/products.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Your Products"/>
<Separator orientation="VERTICAL"/>
</HBox>
</VBox>
<Separator />
<!-- Settings -->
<VBox spacing="20.0">
<HBox>
<ImageView>
<Image url="#../icons/add-account.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Add New Account" />
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/merchant.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Be Merchant"/>
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/settings.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="User Settings" />
<Separator orientation="VERTICAL" />
</HBox>
</VBox>
<!-- Logout -->
<VBox alignment="BOTTOM_CENTER">
<HBox >
<ImageView>
<Image url="#../icons/logout.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Logout" />
<Separator orientation="VERTICAL" />
</HBox>
</VBox>
</VBox>
My problem is in the Logout label and a little white vertical separator in each menu item. I want to place the Logout label at the bottom of Parent VBOX and a little white vertical separator to the left of HBOX as other apps.
How can I modify the code so my dashboard looks like this:
Suppose that this dashboard has a Logout label at the bottom of the VBox
i use scene builder to build a gui for my app and i have this problem:
When stage screen is set to Maximized , AnchorPane is set top left corner and all it's contents inside(VBox etc). i want to cover the full screen or at least be at the center. Im not too familiar with anchorPane alignment and positioning but wit borderpane i didn't have that problem. So here is how my screen looks :
So the anchorpane just is being placed always top left like this.
Here is my fxml code:
<AnchorPane prefHeight="394.0" prefWidth="586.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gr.hua.dit.oop2.gui.Controller2">
<children>
<VBox layoutY="29.0" prefHeight="116.0" prefWidth="344.0" spacing="30.0">
<children>
<TextField fx:id="name" alignment="TOP_CENTER" promptText="Enter your full name" />
<TextField fx:id="age" alignment="BASELINE_CENTER" promptText="Enter your age" />
</children>
</VBox>
<VBox layoutX="425.0" layoutY="54.0" prefHeight="300.0" prefWidth="161.0" spacing="10.0">
<children>
<Label alignment="CENTER_RIGHT" text="Tell us your interests" />
<CheckBox fx:id="museum" layoutX="10.0" layoutY="36.0" mnemonicParsing="false" text="Museums" />
<CheckBox fx:id="sights" layoutX="10.0" layoutY="62.0" mnemonicParsing="false" text="Historical Sights" />
<CheckBox fx:id="night" layoutX="10.0" layoutY="88.0" mnemonicParsing="false" text="Night Life" />
<CheckBox fx:id="forest" layoutX="10.0" layoutY="114.0" mnemonicParsing="false" text="Forests" />
<CheckBox fx:id="lake" mnemonicParsing="false" text="Lakes" />
<CheckBox fx:id="zoo" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" text="Zoo" />
<CheckBox fx:id="sea" layoutX="10.0" layoutY="218.0" mnemonicParsing="false" text="Sea - Beach" />
<CheckBox fx:id="mountain" layoutX="10.0" layoutY="254.0" mnemonicParsing="false" text="Mountains" />
</children>
</VBox>
<Button fx:id="submit" alignment="BOTTOM_CENTER" layoutX="269.0" layoutY="360.0" mnemonicParsing="false" text="Proposal">
<font>
<Font size="15.0" />
</font>
</Button>
<VBox layoutX="10.0" layoutY="174.0" prefHeight="161.0" prefWidth="375.0">
<children>
<TextField fx:id="cities" alignment="CENTER" prefHeight="36.0" prefWidth="334.0" promptText="Enter up to 5 Desired Cities you want to Visit">
<font>
<Font size="10.0" />
</font>
</TextField>
<Label alignment="CENTER" contentDisplay="TOP" prefHeight="18.0" prefWidth="377.0" text="e.g. "Athens gr,Berlin de,New York us" />
</children>
</VBox>
</children>
</AnchorPane>
So I have a stylesheet with just one style within at the moment for testing purposes.
CSS code:
.minimizeBtn{
-fx-background-color: rgba(0,0,0,1);
}
Setting the ID of the button. This is in the controller for the stage.
public void initialize(URL url, ResourceBundle rb) {
handler = new DbHandlers();
minimizeBtn1.setId("minimizeBtn");
My buttons in fxml:
<Button fx:id="minimizeBtn1" layoutX="520.0" mnemonicParsing="false"
prefHeight="25.0" prefWidth="12.0" styleClass="minimizeBtn" text="Button" />
<Button layoutX="548.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="maximizeBtn" text="Button" />
<Button layoutX="574.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="closeBtn" text="Button" />
The rest of my fxml file:
<AnchorPane prefHeight="450.0" prefWidth="600.0" style="-fx-background-color: #2A7FFF;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bownhrmain1.pages.LoginScreenController">
<children>
<AnchorPane prefHeight="450.0" prefWidth="600.0" style="-fx-background-color: #2A7FFF;">
<children>
<Label layoutX="335.0" layoutY="144.0" text="Inventory Systems" textFill="WHITE">
<font>
<Font size="8.0" />
</font>
</Label>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="200.0" layoutY="65.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../images/benx_logo.png" />
</image>
</ImageView>
<Hyperlink layoutX="250.0" layoutY="368.0" text="Forgot Password?" textFill="WHITE" underline="true" />
<JFXButton fx:id="loginBtn" buttonType="RAISED" layoutX="190.0" layoutY="302.0" prefHeight="51.0" prefWidth="221.0" ripplerFill="#2980b9" style="-fx-background-color: #1E5D87;" text="Login" textFill="WHITE" />
<JFXPasswordField fx:id="passwordField" focusColor="#1e5d87" labelFloat="true" layoutX="190.0" layoutY="237.0" prefHeight="34.0" prefWidth="221.0" promptText="Password..." unFocusColor="WHITE" />
<JFXTextField id="text-field" fx:id="usernameField" focusColor="#1e5d87" labelFloat="true" layoutX="190.0" layoutY="176.0" prefHeight="32.0" prefWidth="221.0" promptText="Username/Email..." unFocusColor="WHITE" />
<ImageView fitHeight="20.0" fitWidth="19.0" layoutX="522.0" layoutY="5.0" pickOnBounds="true">
<image>
<Image url="#../images/minimize.png" />
</image>
</ImageView>
<ImageView fitHeight="11.0" fitWidth="12.0" layoutX="554.0" layoutY="9.0" pickOnBounds="true">
<image>
<Image url="#../images/expand-button.png" />
</image>
</ImageView>
<ImageView fitHeight="11.0" fitWidth="11.0" layoutX="581.0" layoutY="9.0" pickOnBounds="true">
<image>
<Image url="#../images/closeWindow.png" />
</image>
</ImageView>
<Button fx:id="minimizeBtn1" layoutX="520.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="minimizeBtn" text="Button" />
<Button layoutX="548.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="maximizeBtn" text="Button" />
<Button layoutX="574.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="closeBtn" text="Button" />
</children>
</AnchorPane>
</children>
</AnchorPane>
The reason i have the styleclass on the buttons is because I have tried both setting the css through class name and ID.
Here's an image of the css working in JavaFx scene builder:
CSS Applying in scene builder.
Here's an imagee of it failing to apply when you run the program:
Not applying
So the answer to this question is to add the following to your start function:
scene.getStylesheets().add(getClass().getResource("pathtostylesheet").toExternalForm());
I am trying to center a label spanning two columns of a GridPane. However, it does not seem to work.
Application class:
public class GUIMainFXML extends Application {
#Override
public void start(Stage primaryStage) {
try {
GridPane p = (GridPane) FXMLLoader.load(getClass().getResource(
"FirstGUIexample.fxml"));
Scene scene = new Scene(p);
scene.getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Welcome (FXML CODE)");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}}
fxml file:
<GridPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
hgap="5" vgap="5">
<padding>
<Insets top="10" right="10" bottom="10" left="10" />
</padding>
<children>
<Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER"
GridPane.rowIndex="0" />
<Button text="Press me" GridPane.halignment="CENTER"
GridPane.rowIndex="1" GridPane.columnSpan="2" />
<CheckBox text="Don't touch me" GridPane.rowIndex="2"
GridPane.halignment="CENTER" GridPane.columnSpan="2" />
<Label text="Type me: " GridPane.halignment="CENTER"
GridPane.columnIndex="1" GridPane.rowIndex="3" />
<TextField GridPane.rowIndex="3" GridPane.columnIndex="2"
GridPane.halignment="CENTER" />
</children>
</GridPane>
application.css
Text {
-fx-font-size: 15pt;
-fx-font-family: Tahoma;
-fx-font-weight: bold;
}
I would like to set it either in the css or in the fxml, I am just testing.
You haven't provided the columnIndex for Text and Button, which you want to align in the center.
<Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER"
GridPane.columnIndex="1" GridPane.rowIndex="0" id="argh" />
<Button text="Press me" GridPane.halignment="CENTER" GridPane.columnIndex="1"
GridPane.rowIndex="1" GridPane.columnSpan="2" />