I'm a total beginner in programming. I've been trying to create a simple program that gets two values from the user (two grades from 0 to 10) and returns the average between them. I've done it with Swing and JOptionPane but using javafx I got nothing. Here goes the code.
package fxcalcmedia;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXCalcMedia 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();
stage.setTitle("CALCULADORA DE MÉDIA");
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
package fxcalcmedia;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.text.Text;
public class FXMLDocumentController implements Initializable {
#FXML
private Label lblMedia;
private Button btnCalc;
private Text txtMedia;
private Text txtNota1;
private Text txtNota2;
#FXML
private void clicouBotao(ActionEvent event) {
double nota1 = Double.parseDouble(txtNota1.getText());
double nota2 = Double.parseDouble(txtNota2.getText());
double media = (nota1+nota2)/2;
txtMedia.setText(Double.toString(media));
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="155.0" prefWidth="303.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="fxcalcmedia.FXMLDocumentController">
<children>
<Label fx:id="lblMedia" layoutX="30.0" layoutY="119.0"
text="Média" />
<Label layoutX="28.0" layoutY="33.0" text="Nota 1" />
<Label layoutX="28.0" layoutY="76.0" text="Nota 2" />
<Button fx:id="btnCalc" layoutX="212.0" layoutY="71.0"
mnemonicParsing="false" onAction="#clicouBotao" text="Calcular"
/>
<TextField fx:id="txtNota1" layoutX="90.0" layoutY="28.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtNota2" layoutX="90.0" layoutY="71.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtMedia" layoutX="90.0" layoutY="114.0"
onAction="#clicouBotao" prefHeight="25.0" prefWidth="78.0" />
</children>
</AnchorPane>
So... the window appears, I insert the grades but when I click on the button to get the average grade, nothing happens. I've tried a lot of things, I've searched a lot... I hope somebody can help. Thanks again.
You need to place #FXML annotation on each Node that have fx:id="" in fxml file. It is necessary for FXMLLoader to create correct instances and inject them to controller.
Another problem is components type in fxml file. In fxml you use TextField
<TextField fx:id="txtNota1" layoutX="90.0" layoutY="28.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtNota2" layoutX="90.0" layoutY="71.0"
prefHeight="25.0" prefWidth="78.0" />
<TextField fx:id="txtMedia" layoutX="90.0" layoutY="114.0"
onAction="#clicouBotao" prefHeight="25.0" prefWidth="78.0" />
but in cotntroller you use Text:
private Text txtMedia;
private Text txtNota1;
private Text txtNota2;
Solution:
#FXML
private Label lblMedia;
#FXML
private Button btnCalc;
#FXML
private TextField txtMedia;
#FXML
private TextField txtNota1;
#FXML
private TextField txtNota2;
As an extension to #MBec's answer. A very nice tool for creating GUIs for javaFX is JavaFX Scene Builder. It provides you with drag & drop functionality and also gives templates for each fxml file's Controller class.
Related
This is how I unbindBiDirectional from a class FamilyMember that contains a string variable called 'txtName'. I unbind the old value and clear it, and then bind the new value.
Unbinding:
((TreeItem<FamilyMember>)oldValue).getValue().nameProperty().unbindBidirectional(txtName.textProperty());
txtName.clear();
Binding:
txtName.setText(((TreeItem<FamilyMember>)newValue).getValue().nameProperty().getValue());
((TreeItem<FamilyMember>)newValue).getValue().nameProperty().bindBidirectional(txtName.textProperty());
But im confused as to how to do this for a ComboBox. My ComboBox is for selecting a gender with 3 options as strings, (ComboBox), Male, Female and Other. How can I achieve the above using a ComboBox with String Property instead?
Controller Class:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML
private ComboBox<String> genderComboBox;
#FXML
private Label selectedGenderLabel;
private Random random = new Random();
#Override
public void initialize(URL location, ResourceBundle resources) {
// Add items:
genderComboBox.getItems().addAll("Male", "Female", "Other");
// Bind selection of combo box to string property:
selectedGenderLabel.textProperty().bind(genderComboBox.valueProperty());
}
#FXML
public void handleUnbindBtnClick() {
// Un-bind and clear:
selectedGenderLabel.textProperty().unbind();
selectedGenderLabel.setText("");
}
#FXML
public void handleBindBtnClick() {
// Make a random selection:
genderComboBox.getSelectionModel().select(random.nextInt(3));
// Re-bind:
selectedGenderLabel.textProperty().bind(genderComboBox.valueProperty());
}
}
FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<ComboBox fx:id="genderComboBox" />
<Label text="Selected Gender:" />
<Label fx:id="selectedGenderLabel" />
<Button mnemonicParsing="false" onAction="#handleUnbindBtnClick" text="Unbind" />
<Button mnemonicParsing="false" onAction="#handleBindBtnClick" text="Bind" />
</children>
</VBox>
Using the FXML project template in netbeans, I've been experiencing an issue with defining the FX controller in my FXML file.
The main class, fxml document and fxml controller are all in the same package: "login".
I am importing the controller into the fxml document using it's fully qualified name.
Error occurs at the import line: "Class login.MyController does not exist."
FXML Document
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import login.MyController?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.MyController">
<children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #091D34;" VBox.vgrow="ALWAYS">
<children>
<Polygon fill="#000000b1" layoutX="363.0" layoutY="103.0" points="-50.0, 40.0, 50.0, 40.0, 50.0, -73.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: #133863;" />
<ButtonBar layoutX="233.0" layoutY="256.0" prefHeight="40.0" prefWidth="161.0">
<buttons>
<Button layoutX="135.0" layoutY="18.0" minWidth="49.0" mnemonicParsing="false" prefHeight="25.0" text="Login" onAction="#doLogin" />
<Button layoutX="165.0" layoutY="18.0" mnemonicParsing="false" text="About" />
</buttons>
</ButtonBar>
<PasswordField layoutX="186.0" layoutY="216.0" prefHeight="25.0" prefWidth="273.0" promptText="Password" onKeyPressed="#onEnter" />
<TextField layoutX="186.0" layoutY="175.0" prefHeight="25.0" prefWidth="273.0" promptText="Username" />
<Text layoutX="253.0" layoutY="76.0" strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-fill: #236AB9;" text="Anatomy Law" textAlignment="CENTER" wrappingWidth="120.58984375">
<font>
<Font name="Aparajita" size="22.0" />
</font>
</Text>
</children>
</AnchorPane>
Controller
package login;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class MyController implements Initializable {
#FXML
public void doLogin(Event e){
System.out.println("do login called");
}
#FXML
public void onEnter(KeyEvent ke){
if(ke.getCode() == KeyCode.ENTER){
System.out.println("on enter called");
login();
}
}
private void login(){
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
Main
package login;
import java.net.URL;
import javafx.application.Application;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
MyController fxmlController = new MyController();
fxmlLoader.setController(fxmlController);
fxmlLoader.setLocation(new URL("C:/.../login/Login.fxml"));
VBox vbox = fxmlLoader.<VBox>load();
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Note
I recently tried the following line in my FXML document:
<?import login.*?>
Which resulted in "package login does not exist."
And
<?import MyController?>
Which resulted in class does not exist.
So i'm assuming it's some kind of build path error but they're all in the same package. I even added the package that they're in to additional source packages to build from just to make sure but that didn't solve anything. My project layout is the following:
Project Name
Source Packages
login
Login.fxml
MyController.java
Main.java
I reviewed the other question postings for this problem but no one seemed to have this specific problem. If I did however miss someone's post with an identical answer, I will gladly take that reference.
You don't have to import it. You have to specify the full name of your controller class though.
In your case it is login.MyController so fx:controller="login.MyController".
Though you might have some other problems because I'm not having any issues when i put that import statement in the fxml. It just isn't necessary.
Make sure that the document you are editing is definitely the document in your package.
Somehow, in my case I was actually editing a document outside of the package with the same exact name. This is why it couldn't find the controller class when I tried to import it.
I am Trying to come up with simple tool where I want to Call another fxml page on a button click from present fxml page.
My Main Java class is designed as follows:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
public class Main extends Application
{
#Override
public void start(Stage stage) {
try {
TitledPane page = (TitledPane)FXMLLoader.load(getClass().getResource("NewTest.fxml"));
Scene scene = new Scene(page);
stage.setScene(scene);
stage.setTitle("Welome Page");
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Application.launch(Main.class, (java.lang.String[])null);
}}
and Controller Class is designed as :
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.stage.Stage;
public class SimpleController implements Initializable
{
#FXML // fx:id="loginbtn"
private Button loginbtn; // Value injected by FXMLLoader
#Override // This method is called by the FXMLLoader when
initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
assert loginbtn != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
// initialize your logic here: all #FXML variables will have been injected
loginbtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Stage stage;
Parent root;
if(event.getSource()==loginbtn){
//get reference to the button's stage
stage=(Stage) loginbtn.getScene().getWindow();
//load up OTHER FXML document
try{
TitledPane page =(TitledPane) FXMLLoader.load(getClass().getResource("secondPage.fxml"));
}catch(Exception e)
{e.printStackTrace();}
}
}
});
}
}
And the Fxml Page is like:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<TitledPane text="Welcome to the Vacation Planner" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<content>
<AnchorPane prefHeight="330.0" prefWidth="497.0">
<children>
<Label layoutX="14.0" layoutY="52.0" prefHeight="25.0" prefWidth="189.0" text="User Name">
<font>
<Font size="24.0" />
</font>
</Label>
<TextArea layoutX="53.0" layoutY="106.0" prefHeight="41.0" prefWidth="391.0" />
<Label layoutX="14.0" layoutY="165.0" text="Password">
<font>
<Font size="24.0" />
</font>
</Label>
<Button fx:id="loginbtn" layoutX="114.0" layoutY="262.0" mnemonicParsing="false" text="Login">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="exitbtn" layoutX="271.0" layoutY="262.0" mnemonicParsing="false" text="Exit">
<font>
<Font size="18.0" />
</font>
</Button>
<PasswordField layoutX="53.0" layoutY="207.0" prefHeight="39.0" prefWidth="391.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
When I am clicking on the button Login it is not opening the Second xml page that I am trying to open on button click.
Thanks in advance.
You load the Nodes and then do nothing with it.
You need to add them to the existing scene or replace that scene:
try{
TitledPane page =(TitledPane) FXMLLoader.load(getClass().getResource("secondPage.fxml"));
Scene newScene = new Scene(page);
stage.setScene(newScene);
} catch(Exception e) {
e.printStackTrace();
}
Furthermore you haven't associated the controller with the fxml. Use e.g. the fx:controller attribute with the fully qualified class name of the controller class at the root of the fxml to do this:
<TitledPane text="Welcome to the Vacation Planner" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="my.package.SimpleController">
Where my.package would be the package containing the controller.
I need to insert values in list view in one by one like chatting.
now my code is
#FXML
private ListView<String> messageList;
private ObservableList<String> messages = FXCollections.observableArrayList();
messageList.setItems(messages);
This may be similar to what you are asking.
Main:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ChatApp 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.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
public class FXMLDocumentController implements Initializable {
#FXML private ListView lvChatWindow;
#FXML private TextField tfUser1, tfUser2;
ObservableList<String> chatMessages = FXCollections.observableArrayList();//create observablelist for listview
//Method use to handle button press that submits the 1st user's text to the listview.
#FXML
private void handleUser1SubmitMessage(ActionEvent event) {
chatMessages.add("User 1: " + tfUser1.getText());//get 1st user's text from his/her textfield and add message to observablelist
tfUser1.setText("");//clear 1st user's textfield
}
//Method use to handle button press that submits the 2nd user's text to the listview.
#FXML
private void handleUser2SubmitMessage(ActionEvent event) {
chatMessages.add("User 2: " + tfUser2.getText());//get 2nd user's text from his/her textfield and add message to observablelist
tfUser2.setText("");//clear 2nd user's textfield
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
lvChatWindow.setItems(chatMessages);//attach the observablelist to the listview
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="349.0" prefWidth="549.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="chatapp.FXMLDocumentController">
<children>
<Button fx:id="bntUser1Send" layoutX="99.0" layoutY="299.0" onAction="#handleUser1SubmitMessage" text="send message user1" />
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Button fx:id="btnUser2Send" layoutX="351.0" layoutY="299.0" mnemonicParsing="false" onAction="#handleUser2SubmitMessage" text="send message user2" />
<ListView fx:id="lvChatWindow" layoutX="75.0" layoutY="29.0" prefHeight="200.0" prefWidth="419.0" />
<TextField fx:id="tfUser1" layoutX="36.0" layoutY="258.0" prefHeight="25.0" prefWidth="239.0" />
<TextField fx:id="tfUser2" layoutX="293.0" layoutY="258.0" prefHeight="25.0" prefWidth="239.0" />
</children>
</AnchorPane>
This app simulates two different users sending messages to one listview. Similar to a chat. More comments in Controller
Can Anybody tell me how to construct HiddenSidesPane in FXML not in the controller?
I am having the basic controller code for this but I am not able to understand how to create fxml structure from that.
Can I have something like this? Below code;
<HiddenSidesPane prefWidth="800.0" pinnedSide="TOP">
<content>
<HBox fillHeight="false" nodeOrientation="RIGHT_TO_LEFT"
prefHeight="27.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="3.0" StackPane.alignment="TOP_RIGHT">
<children>
<Label prefHeight="14.0" prefWidth="94.0" text="Value Date From">
<HBox.margin>
<Insets right="2.0" top="5.0" />
</HBox.margin>
</Label>
</children>
<StackPane.margin>
<Insets top="2.0" />
</StackPane.margin>
</HBox>
</content>
</HiddenSidesPane>
This how I made a fast example with the offical FXSampler of ControlsFX in mind:
Assumptions
You already set up your FXML Project and added the ControlsFX.jar as dependency on your build path.
FXMLDocument.fxml
Watch for the import statements.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import org.controlsfx.control.*?>
<StackPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="200" prefWidth="320" fx:controller="javafxapplication17.FXMLDocumentController">
<children>
<HiddenSidesPane fx:id="pane">
<content>
<Label alignment="CENTER" style="-fx-background-color: white; -fx-border-color: black;" maxHeight="1000.0" maxWidth="1000.0" text="Content Node" />
</content>
<top>
<Label fx:id="pinLabel" style="-fx-background-color: rgba(0,255,0,.25);" text="(Click to pin / unpin)" alignment="CENTER" prefHeight="50.0" prefWidth="50.0" onMouseClicked="#handleMouseClicked" />
</top>
</HiddenSidesPane>
</children>
</StackPane>
FXMLController.java
Inject your variables pane and pinLabel to set them.
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Side;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import org.controlsfx.control.HiddenSidesPane;
public class FXMLDocumentController implements Initializable {
#FXML
private HiddenSidesPane pane;
#FXML
private Label pinLabel;
#FXML
private void handleMouseClicked(MouseEvent event) {
if (pane.getPinnedSide() != null) {
pinLabel.setText("(unpinned)");
pane.setPinnedSide(null);
} else {
pinLabel.setText("(pinned)");
pane.setPinnedSide(Side.TOP);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
JavaFXApplication17.java
Sorry for that name :-)
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication17 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);
}
}
As far as you already know here is the JavaDoc of HiddenSidesPane: http://controlsfx.bitbucket.org/org/controlsfx/control/HiddenSidesPane.html
And if you need an example download the Zip http://fxexperience.com/downloads/controlsfx-8.40.9.zip and unzip it, there is a file controlsfx-samples-8.40.9.jar inside. Double click it and show the sources.