I am just starting to learn javafx.I am trying to make a simple application where you enter into a textfield and simultaniously give it out via the label.
Heres the main class
package testing;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class testing extends Application {
private Stage primaryStage;
private BorderPane RootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("First Application");
initRootLayout();
initProjectTest();
}
public static void main(String[] args) {
launch(args);
}
public Stage getPrimaryStage() {
return primaryStage;
}
public void initRootLayout() {
try{ FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/RootLayout.fxml"));
RootLayout=(BorderPane)loader.load();
primaryStage.setScene(new Scene(RootLayout));
primaryStage.show();
}
catch(IOException e) {
e.printStackTrace();
}
}
public void initProjectTest() {
try {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/ProjectTest.fxml"));
AnchorPane ProjectTest=(AnchorPane)loader.load();
RootLayout.setCenter(ProjectTest);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Here is the controller class
package testing.view;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class ProjectTestController {
#FXML
private Label label1=new Label();
#FXML
private TextField textfield1=new TextField();
label1.textProperty().bind(textfield1.textProperty());
}
If I try to implement it via a button where the text is updated everytime I click the button(via a onAction function) the code works fine. However I get syntax error when I try to bind. I tried to search for answers but none of were having syntax errors.
Related
I have 2 scenes. One of the scenes is Login and the other is Register. When I click the register button on the Log in screen it switches me to the register scene. The actions I have made in the controller for the button is not working. Do I need to do something in the main or controller to get the Register.fxml to work?
ModelController:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.sql.*;
public class ModelController extends Main{
#FXML
private Label LblStatus;
#FXML
private TextField UserEmail;
#FXML
private TextField UserPassword;
#FXML
private TextField RegisterEmail;
#FXML
private TextField RegisterPassword;
#FXML
private TextField RegisterFirst;
#FXML
private TextField RegisterLast;
#FXML
private TextField RegisterSSN;
#FXML
private TextField RegisterAddress;
#FXML
private TextField RegisterCity;
#FXML
private TextField RegisterState;
#FXML
private TextField RegisterZip;
#FXML
private Button LoginRegister;
public void Login(ActionEvent event) throws Exception {
if(UserEmail.getText().equals("User") && UserPassword.getText().equals("pass")) {
LblStatus.setText("Login Success");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
else {
LblStatus.setText("Login Failed");
}
}
public void handleButtonAction(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Register.FXML"));
Parent root1 = (Parent) fxmlLoader.load();
Scene Register = new Scene(root1);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(Register);
window.show();
}
catch(Exception e) {
}
}
public void CompleteRegistration(ActionEvent event) {
}
Main:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
The 2nd scene was not pulling in the controller.
Hello I am currently working on a PIN Generator for my work and as I am completely new to Java I am having a few difficulties, especially when using JavaFX.
I want to make the programm show another .fxml file when clicking on one of each buttons.
Here is my code:
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 {
public static void main(String[] args) {
Application.launch(main.class, args);
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
I have created a controller Class for each button on the scene.
Controller Class Code:
package view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class MainController {
#FXML
private Label dpmadirektpropingenerator;
#FXML
private Button unlockPinButton;
#FXML
private Button confirmationPinButton;
}
This code should works.
Modify your main class like this:
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 {
public static void main(String[] args) {
Application.launch(main.class, args);
}
static Stage stg;
#Override
public void start(Stage stage) throws Exception {
this.stg = stage;
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
and here is your pressButton function:
public void pressButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sedondFXML.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
main.stg.close();
} catch(Exception e) {
e.printStackTrace();
}
}
Newbie question. I'm trying to make a javafx to do list class but i need to add a button and textfield to insert data to the listview. I want to add these but I can't add them directly to the listview. I dunno how to do it.
package learning;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class ToDoList extends Application{
private ObservableList<String> doList = FXCollections.observableArrayList();
private String text="";
public void start(Stage primaryStage){
primaryStage.setTitle("2DoList");
TextField userTextField = new TextField();
Button enter=new Button("Add");
enter.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
text= userTextField.getText();
doList.add(text);
userTextField.setText("");
text="";
}
});
ListView<String> root=new ListView<String>(doList);
Scene scene=new Scene(root,250,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the java
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class mains extends Stage {
public static void main(String[] args) {
new JFXPanel();
Platform.runLater(new Runnable(){
#Override
public void run() {
new mains();
}
});
}
void go(){
new JFXPanel();
new mains().show();
}
public mains() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"LOL.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (Exception e) {
e.printStackTrace();
}
}
#FXML
private Button button;
#FXML
private Label label;
#FXML
void push(ActionEvent event) {
}
}
here is the fxml http://pastebin.com/uzBrMRDV
I get a load exception it says Root is already specified.
If i remove the setRoot(this); it doesnt load at all
I am getting really frustrated with JFX...
Is there anyway to load FXML files like a Stage from the controller itself
Remove the line
fxmlLoader.setRoot(this);
Your FXML defines the root to be an AnchorPane (and you can't set the root twice, which is why you are getting the error).
Since the current class is a Stage, and the FXMLLoader loads an AnchorPane, you need to put the loaded AnchorPane in a Scene and set the scene in the stage. Replace
fxmlLoader.load();
with
AnchorPane root = fxmlLoader.load();
Scene scene = new Scene(root); // optionally specify dimensions too
this.setScene(scene);
Im Getting These Errors when trying to parse this FXML file into my Java Program.The code for when i load in the FXML file and where i get the errors are bellow
package mediarealm;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MediaRealm extends Application {
#Override
public void start(Stage primaryStage) {
Parent root = null;
primaryStage.initStyle(StageStyle.UNDECORATED);
try {
root = FXMLLoader.load(getClass().getResource("/rsrc /UIManagmentDefaultState.fxml"));
} catch (IOException ex) {
Logger.getLogger(MediaRealm.class.getName()).log(Level.SEVERE, null, ex);
}
root.setStyle("-fx-background-color: #000000;");
Scene scene = new Scene(root, 1280, 720);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It could possibly because of the code Bellow but I don't think it is i'm trying to load in some of the elements from the fxml file into the rest of my code so that i can have full access to said elements with java.
package mediarealm;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Controller
{
#FXML private Button ExitButton;
#FXML private static Button myVideos;
public static void doshit()
{
myVideos.setText("Addison is gay!");
}
Probably a long shot, but the line
< fx:id="myMusic" /Button>
is no correct XML, it rather should be
</Button>