JavaFX Custom Control main method - java

I'm following this tutorial regarding custom control creation using JavaFX. I noticed that they had a main method, but they never showed it in the video.
I couldn't export my jar as a runnable jar because I needed a main class, so I tried to create one (code below). However, when I tried to import it into SceneBuilder, it didn't show.
Main class:
package controls;
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 s) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MessageTab.fxml"));
s.setScene(new Scene(root));
s.show();
}
public static void main(String[] args) {
launch(args);
}
}
When I ran the jar in eclipse, it displayed the fxml file.
I want to use this jar as a custom control, not an application, but I couldn't find anything about what I needed to do to export it properly. If you know what I need to do, please let me know.
Thanks!

Related

The type javafx.fxml.FXMLLoader is not accessible

I'm working on a project in Eclipse and I want to make use of JavaFX for my GUI. I installed JavaFX and I added it to my project library. When I try to import "import javafx.fxml.FXMLLoader;"
there is an error:"The type javafx.fxml.FXMLLoader is not accessible".
image: https://ibb.co/QmpFKDF
code:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Hello extends Application {
public void start(Stage stage) throws Exception {
Parent root = FXMLLOADER.load(getClass().getResource(
"Hello.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Hello");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Thanks.
First of all Java is case-sensitive as mentioned in the comments. Use FXMLLoader.load().
From your screenshot, the IDE recognizes the import line as an error line too. So your JavaFX Library has not been correctly imported.
You have to follow the correct steps to get it to work.
Here are some Tutorials that you can follow:
https://gist.github.com/stevenliebregt/bc62a382fc43064136b662ee62172ab3
How to add JavaFX runtime to Eclipse in Java 11?
https://openjfx.io/openjfx-docs/

Error: Could not find or load main class Sample.Main in JavaFx project

I am developing a JavaFx project in Eclipse IDE with one of my friend. The Java versions are same in both of our computers. but when I try to run the project in my own computer it gives the following error.
Error: Could not find or load main class Sample.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Although it gives above error, Main.java class is in the package called 'Sample'.
I have already went through some answers related to my problem but could not find a solution. I configured the build paths as follows too. but it doesn't run as expected.
Configure build path: Click here to see the image
Following is the Main.class
package Sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("top.fxml"));
primaryStage.setScene(new Scene(root, 1100, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I am much obliged if somebody can please help me to find the cause for this problem and how to rectify it.

IntelliJ setOnAction not recognized by ActionEvent library (JDK 8)

I seem to have a problem with IntelliJ concerning the setOnAction lambda function. After lots of research I can not find an answer to my problem.
I'm learning to program in Java (latest JDK 8 version) and recently moved from NetBeans to IntelliJ IDE. I wrote a very simple program and the code works perfectly in NetBeans, but I have problems using IntelliJ.
The setOnAction function is not recognized by IntelliJ. I have configured the IDE (Project Structure/Modules/ and chose 8 - Lambda's, type annotations,...) but with no success. I manually added: import javafx.event.ActionEvent;
I have also configured (Settings/General/Auto Import/Add unambiguous imports on the fly).
The program contains two classes, a Main class and a GUI class.
Main class:
package fxvb0203;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class fxvb0203 extends Application
{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage primaryStage)
{
FlowPane root = new FlowPane();
Scene scene = new Scene(root);
new GUI(root);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
GUI class:
package fxvb0203;
import javafx.scene.layout.FlowPane;
import javafx.event.ActionEvent;
import java.awt.*;
public class GUI
{
private final Button btnText = new Button("Text");
private final TextField txtField = new TextField();
public GUI(FlowPane pane)
{
btnText.setOnAction(event ->
{
txtField.setText("text");
});
pane.getChildren().add(btnText);
pane.getChildren().add(txtField);
}
}
setOnAction gives the following warning:
can not resolve method setOnAction, (lambda expression)
import javafx.event.ActionEvent; is greyed out as it is not used, which is strange. So it must be something with IntelliJ that is not correct.
Another small problem I have with IntelliJ only, but which does not relate to the main problem, is the following:
pane.getChildren().add(btnText);
pane.getChildren().add(txtField);
These two lines of code give the following error:
add (javafx.scene.Node) in list cannot be applied to (java.awt.TextField)
In NetBeans both work fine, but IntelliJ gives problems.
I hope to rely on the professional help of this community, because I am kinda stuck here and besides these small problems I really like the IntelliJ IDE.
Many thanks in advance.
Greets.
In your GUI class you have the following import statement: import java.awt.*;. The * means you're importing everything inside the java.awt package which includes (among others): java.awt.TextField and java.awt.Button.
What this means is that instead of btnText being a javafx.scene.control.Button it is actually a java.awt.Button. The AWT Button does not have a setOnAction method therefore you are getting a compilation error. This also affects your txtField which is now a java.awt.TextField instead of a javafx.scene.control.TextField.
Replace import java.awt.* with import javafx.scene.control.Button and import javafx.scene.control.TextField; or replace it with import javafx.scene.control.*.
In case importing java.awt.* was on purpose it is generally a bad idea to mix different GUI toolkits together.

Why Are These Imports Errored?

As part of my uni project, I have had to clone a group member's code and work on it myself. But on the imports included below, they are underlined and I can't seem to know why or how to address this. Netbeans says that these are 'Unused imports'. I've already tried to Google this but with no luck.
What does this mean and how do I fix it? Please bear with me as I am completely new to programming and it's concepts. Thank you.
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import static org.apache.http.HttpHeaders.USER_AGENT;
As the name implies, "Unused import" is a message from the IDE (not directly related to Java) and means that you're not using that class/interface/whatever it is. Also, note that this is a warning, not an error. This means, your code can compile and execute without issues.
To fix this, just remove the unused import statements.
Example of how to raise unused import:
package something;
//this interface is never used in this class
import java.util.List;
public class Example {
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Example of how to fix it:
package something;
//removed import java.util.List; since it's not used
public class Example {
public static void main(String args[]) {
System.out.println("Hello world");
}
}

Need to import custom package to my class

Hi I have a custom class which consists of a custom exception folder and 3 .java files.
My structure is com/raeside/family/
So I want to do the following
import com.raeside.family.*;
But it is just giving me a red underline and when I try to let netbeans repair it, it doesn't work. Where should I store my com/ folder so that I can import it into my java program? I tried storing it inside of projectname/src/ but it's to no avail.
package com.raeside.family;
public class W4Q3 {
public static void main(String[] args) {
System.out.println("hello");
}
}
Is my code, my W4Q3 file is in the family folder. Is there something I am missing?
If all of your classes are in the same package (e.g. com.raeside.family) then you don't need the import statement at all. Make sure that all of your java files are in the src/com/raeside/family directory and have
package com.raeside.family;
at the top.

Categories