I am new to use the RichTextFX for my JAVAFX project. But I find that I cannot add the text to the InlineCssTextArea. In the project I used the MVC design and generate the UI code from the Scene Builder. The InlineCssTextArea is succesfully created but I cannot figure out why I cannot add text content.The program successfully compiled without error.But the InlineCssTextArea is empty as default.
I have read the official demos of the RichTextFX on Github, but they build up the UI purely from code but not from Scene Builder.
The release "fat" version of the library required :https://github.com/FXMisc/RichTextFX
Thank you
Tree structure
> ├───library
> │ richtextfx-fat-0.10.2.jar
> │
> │
> └───src
> Main.fxml
> Main.java
> specialController.java
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import org.fxmisc.richtext.InlineCssTextArea?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="specialController">
<children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<InlineCssTextArea fx:id="specialArea" layoutX="14.0" layoutY="14.0" prefHeight="346.0" prefWidth="611.0" />
</children>
</AnchorPane>
</children>
</VBox>
Main.java
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 {
private static final String indexFXMLFileName="Main.fxml";
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent index= FXMLLoader.load(getClass().getResource(indexFXMLFileName));
primaryStage.setTitle("Writing Assitant Index");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(index));
primaryStage.show();
}
}
specialController.java
import javafx.fxml.FXML;
import org.fxmisc.richtext.InlineCssTextArea;
public class specialController{
#FXML
public InlineCssTextArea specialArea;
public specialController() {
String alphabet = "Remember when you were a careless eight year old kid riding a bike with your friends, racing each other around the neighborhood? ";
specialArea = new InlineCssTextArea(alphabet);
}
}
I finally found the solution. Change the specialController.java to this. Thanks to #kleopatra. Except the problem for re-instantiation mentioned by kleopatra, the major problem was that I didn't realize the difference between initialize and constructor, and therefore I tried to manipulate before the initialization of the controls, which caused the nullPointerException. There are already some answers related the difference between these two on StackOverflow.
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import org.fxmisc.richtext.InlineCssTextArea;
import java.net.URL;
import java.util.ResourceBundle;
public class specialController implements Initializable {
public InlineCssTextArea specialArea;
public specialController() {
}
#Override
public void initialize(URL location, ResourceBundle resources) {
String alphabet = "Remember when you were a careless eight year old kid riding a bike with your friends, racing each other around the neighborhood? ";
specialArea.deleteText(0,specialArea.getLength());
specialArea.appendText(alphabet);
specialArea.setPrefWidth(400);
specialArea.setPrefHeight(600);
}
}
Related
I'm trying to run a simple JavaFX app in eclipse using java 1.8 (I don't have the option to use newer java version), my simple controller:
import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class MyController {
#FXML private Canvas can;
#FXML
void buttonPressed(ActionEvent event) {
}
}
and main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MyApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MyFxml.fxml"));
Scene scene = new Scene(root);
stage.setTitle("MyApp");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
my fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.BorderPane?>
<BoarderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller=MyController">
<top>
<ToolBar prefHeight="40.0" prefwidth="200.0" BorderPane.alignment="CENTER">
<items>
<Button mnemonicParsing="false" onAction="#buttonPressed" text="Press" />
</item>
</ToolBar>
</top>
<center>
<Canvas fx:id="can" height="300.0" width="300.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
every time I try to run this I get
Error: JavaFX runtime components are missing, and are required to run this application
I have already added both jfxrt.jar and jfxswt.jar to a library and add it to the project and I've added permissions to javafx in the JRE but I keep getting this exception
i'm fairly new to javafx and fxml. I'm trying to teach myself! However, when I was creating a program for a simple login GUI I came into an issue when I finally tried to run my program. It told me there was no main method in my class and I'm unsure of how to fix it. Any ideas?
My program creates a login screen and when you enter "test" for the username and password it'll take you to another scene.
Here is my Login.java
package com;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
/**
*
* #author Tyler
*/
public class Login extends Application{
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Login");
stage.show();
}
}
Here is my LoginController.java
package com;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* #author Tyler
*/
public class LoginController implements Initializable {
#FXML
private Label lblMessage;
#FXML
private TextField txtUsername;
#FXML
private PasswordField txtPassword;
#FXML
private void btnLoginAction(ActionEvent event) throws Exception{
if(txtUsername.getText().equals("test") && txtPassword.getText().equals("test")){
((Node) (event.getSource())).getScene().getWindow().hide();
Parent parent = FXMLLoader.load(getClass().getResource("DateSelection.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.setTitle("Date Selection");
stage.show();
}else{
lblMessage.setText("Username or Password is invalid!");
}
}
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Here is my Login.fxml
<?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" fx:id="lblMessage" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.LoginController">
<children>
<PasswordField fx:id="txtPassword" layoutX="200.0" layoutY="200.0" prefHeight="30.0" prefWidth="200.0" promptText="Password" />
<TextField fx:id="txtUsername" layoutX="200.0" layoutY="140.0" prefHeight="30.0" prefWidth="200.0" promptText="Username" />
<Button fx:id="btnLogin" layoutX="269.0" layoutY="251.0" mnemonicParsing="false" onAction="#btnLoginAction" prefHeight="30.0" text="Login">
<font>
<Font size="14.0" />
</font></Button>
<Label fx:id="lblMessage" layoutX="283.0" layoutY="71.0" text="Label" />
</children>
</AnchorPane>
Here is my DateSelectionController.java
package com;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
/**
* FXML Controller class
*
* #author Tyler
*/
public class DateSelectionController implements Initializable {
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Here is my DateSelection.fxml
<?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="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.DateSelectionController">
<children>
<Label layoutX="191.0" layoutY="164.0" text="Welcome">
<font>
<Font name="System Bold" size="50.0" />
</font>
</Label>
</children>
</AnchorPane>
From Oracle:
The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.
So one solution is to make sure it's being built in a way that fully supports the JavaFX tools. The other solution is to add a main method to starts the application. That would avoid any potential problems like this, and doesn't cause any problems in the case where it's not needed.
Your main method should look like this:
public static void main(String[] args){
Application.launch(Login.class, args);
}
That will simply pass control on to JavaFX to handle like it would normally.
This question already has an answer here:
javafx 8 compatibility issues - FXML static fields
(1 answer)
Closed 8 years ago.
I am using JavaFX 8 and FXML in this project and trying to update my text area with the results from other classes within the program.
The Text Area is defined in the FXML document as such:
<TextArea fx:id="feedBack" editable="false" focusTraversable="false"
layoutX="203.0" layoutY="32.0" prefHeight="205.0" prefWidth="308.0"
wrapText="true"/>
It is called in the controller here, note that originally it was a "private" item but to make it work with the classes I made it public:
#FXML
public static TextArea feedBack;
EDIT: It is worth noting that when the TextArea is identified as "private" I have no issue getting the set/append text method to work but this does not allow me to use the text area in other classes, which is what I need to do.
However now when I try to do either appendText() or setText(), such as follows:
feedBack.setText("Connection Made");
I am given a null point exception. Why is this? I have made this work in JavaFX 7 with FXML by doing the same thing and it works fine. What am I missing to make this work?
EDIT, test/proof of brokenness with complete tiny program.
I did not make another class for this one as the textarea won't work in the control anyway.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textareatester.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TextArea fx:id="feedback" layoutX="61.0" prefHeight="200.0" prefWidth="200.0" wrapText="true" />
<Button fx:id="dostuff" layoutX="261.0" layoutY="62.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
Main Method:
package textareatester;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TextAreaTester 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 Class:
package textareatester;
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.control.TextArea;
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
public static TextArea feedback;
#FXML
private Button dostuff;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
feedback.setText("Hello World!"); ///<-- this is what gives me the NPE
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// not touching anything
}
}
Solution for those who are curious:
I ended up simply returning my results strings from my methods and then appending them to the TextArea.
The problem is the static at the definition of the TextArea. Since static attributes are bound to the class and not the object, variable feedback is never initiated and thus is null when accessed in the EventHandler.
I have this controller:
package cz.vutbr.feec.bmds.cv2;
import java.awt.Button;
import java.awt.TextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Slider;
public class TestGuiController {
private int buttonPressed = 0;
#FXML
private Button tlacitko;
#FXML
private TextField textovePole;
#FXML
private Slider slider;
public void buttonPressed(ActionEvent e) {
buttonPressed++;
textovePole.setText(Integer.toString(buttonPressed));
}
}
this fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<AnchorPane prefHeight="200.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml" fx:controller="cz.vutbr.feec.bmds.cv2.TestGuiController">
<children>
<Button fx:id="tlacitko" layoutX="30.0" layoutY="40.0" mnemonicParsing="false" onTouchPressed="#buttonPressed" text="Button" />
<Slider fx:id="slider" layoutX="157.0" layoutY="17.0" orientation="VERTICAL" />
<TextField fx:id="textovePole" layoutX="14.0" layoutY="89.0" prefWidth="134.0" />
</children>
</AnchorPane>
and this is my main class:
package cz.vutbr.feec.bmds.cv2;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("TestGui.fxml"));
primaryStage.setTitle("Titulek");
primaryStage.setScene(new Scene(root,300,275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
When I run this through ant I get message box with error (exception during running application). I tried simple fxml without controller and it works so I am guesing I do something wrong with controller. What I must change to have it working?
I must answer my own question. Problem was in TestGuiController where I used java.awt.Button and java.awt.TextField instead of javafx.scene.control.Button and javafx.scene.control.TextField.
I'm not 100% sure but try:
in FXML: Button: onAction instead of onTouchPressed
Please provide the exact Exception message.
Today i am Going to create Hello World Application with JavaFX Fxml project.Here is the code.
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="temp.Temp">
<children>
<Button id="button" layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" prefHeight="16" prefWidth="69" fx:id="label" />
</children>
</AnchorPane>
Sample.java
package temp;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class Sample implements Initializable {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Temp.java
package temp;
import javafx.application.Appl
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Temp extends Application {
public static void main(String[] args) {
Application.launch(Temp.class, args);
}
#Override
public void start(Stage stage) throws Exception {
try{
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
stage.setScene(new Scene(root));
stage.show();
}
catch(Exception e){
System.out.print(e);
}
}
}
But It is Giving Error
/Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1757: The following error occurred while executing this line:
/Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1392: jarsigner returned: 1
BUILD FAILED (total time: ६ seconds)
But When i Use Exception Handling it Gives Location Not Found ?
Please help
You are trying to load Controller from fx:controller="temp.Temp". This is not Your controller class, you should use fx:controller="temp.Sample" instead.
You also need a leading forward slash to load package resources. Assuming that you have Sample.fxml in the temp package, it would look like this:
Parent root = FXMLLoader.load(getClass().getResource("/temp/Sample.fxml"));