IntelliJ setOnAction not recognized by ActionEvent library (JDK 8) - java

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.

Related

Working with JavaFX and I got this error: Required type: java.awt.event.ActionEvent - Provided: javafx.event.ActionEvent [duplicate]

This question already has answers here:
JavaFx, Problems with #FXML
(2 answers)
Closed 8 months ago.
So, I'm trying to iron out a problem, but the suggestions from intellij are less than helpful.
As the title suggests: I am working with JavaFX and see this error message.
Intellij recommended migrate actionEvent type to 'ActionEvent'. After selecting to migrate the type nothing changed and the error persisted. Then, I tried the next recommendation from Intellij: change parameter 'actionEvent' type to 'ActionEvent' which brings up a window that indicates a "Super Method Found" and my options are to change the base method (which is the jdbc jar file) or current method.
As an alternative I tried changing the import statements (that are denoted with "this import statement") and eventually made my way back to the original error after intellij recommended I change override function to a lambda function.
package sample;
//import java.awt.event.ActionEvent; //this import statement
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent; //this import statement
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class LoggedInController implements Initializable{
#FXML
private Button button_logout;
#FXML
private Label label_welcome;
#FXML
private Label label_super_pw;
#Override
public void initialize(URL location, ResourceBundle resources){
button_logout.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
//line below is where the error occurs
DBUtils.changeScene(actionEvent, "sample.fxml", "Log In", null, null);
}
});
}
/**
* This function displays the user's information once they log in
* #param username the user's display name
* #param super_pw the "super password" radio button that is selected when an account in created
*/
public void setUserInformation(String username, String super_pw){
label_welcome.setText("Welcome " + username + "!");
label_super_pw.setText("Your super secret password is: " + super_pw + ".");
}
}
As you noted, the error happens here:
DBUtils.changeScene(actionEvent, "sample.fxml", "Log In", null, null);
And the error says the problem is with the actionEvent argument. More specifically, the type of actionEvent is javafx.event.ActionEvent (as it should be) but the changeScene method is defined to accept a java.awt.event.ActionEvent as the first argument. The former type is part of the JavaFX framework, whereas the latter type is from the AWT framework.
I assume DBUtils is your own class. And since you're writing a JavaFX-based application, not an AWT-based one, the solution is to go into your DBUtils source file and replace:
import java.awt.ActionEvent;
With:
import javafx.event.ActionEvent;
In general, unless you're mixing UI frameworks (which you should not be unless you really need to and know what you're doing), you should avoid any imports from AWT and Swing when writing a JavaFX application.

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.

JavaFX Custom Control main method

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!

How can I import jDatePicker into a BlueJ Java program?

I'm trying to use the jDatePicker tool from here: sourceforge.net/projects/jdatepicker/files/latest/download and I've placed the .jar file in C:\Program Files (x86)\BlueJ\lib\userlib and it's been recognised in the BlueJ preferences but I have no idea how to actually use it in my project. I've tried all sorts of import commands but it's not picking it up. Any ideas?
Update: OK, i've now got it to compile, but the applet doesn't run, BlueJ just says "Applet not initialised":
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import net.sourceforge.jdatepicker.*;
import net.sourceforge.jdatepicker.impl.*;
import net.sourceforge.jdatepicker.util.*;
public class Task1 extends java.applet.Applet implements ActionListener
{
public void init()
{
setLayout(null);
setSize(200,240);
JButton btnConfirm=new JButton("Confirm"); //initalises the button
btnConfirm.setBounds(15,2,100,20);
add(btnConfirm); //paints it on the screen
btnConfirm.addActionListener(this);
TextField text = new TextField(20);
text.setBounds(5,24,185,20);
add(text);
UtilDateModel model = new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
datePicker.setBounds(50,80,185,20);
add(datePicker);
}
/* Use the method actionPerformed to trap the event button clicked */
public void actionPerformed(ActionEvent evt)
{
JOptionPane.showMessageDialog(null,"ALERT MESSAGE","TITLE",JOptionPane.WARNING_MESSAGE);
}
}
To import other classes in your Java files, you need to add an import statement with the full qualified name of the class (The file name with the package).
To import the JDatePicker class, you need to add:
import org.jdatepicker.JDatePicker;
If you want to import all the classes in a package, you can use *:
import org.jdatepicker.*;
The classes must be present in the Classpath. As you are adding the JAR file in the BlueJ\lib\userlib folder, this should be enough to import the classes correctly.
You may need to import other classes in other packages like:
import org.jdatepicker.impl.*;
import org.jdatepicker.util.*;
Import your required classes or packages as needed.
You need to tell java where to find the library. You do this by adding the location, where you put the library to your classpath.
Usually your IDE supports you in this process by providing a GUI to do this.
For BlueJ, take a look here: http://www.bluej.org/faq.html#faq_How_do_I_use_custom_class_libraries__JARs__

Categories