I am having problems with javafx (surprise lol). For some reason my code seems to be running, but the actual gui never shows up on my screen, and it is stuck in the bottom right. When I take out the "implements initializable" in the Sample Controller class, then the gui shows up. I would greatly appreciate any help anyone could give me !!
Thanks
Main Class
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Sample.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);
}
}
Sample Controller Class
package application;
import java.awt.Label;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.ListView;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Popup;
import javafx.stage.Stage;
public class SampleController{
#FXML
private ListView<String>mainListView;
/**
* Initializing the class
*/
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String>thisMainListView = FXCollections.observableArrayList("SPY","QQQ","Rus2000");
mainListView.setItems(thisMainListView);
}
}
Related
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 2 years ago.
I'm new to JavaFX, especially the built in API, which is the Timeline
But I wanna add a timer for an event using Timeline
Timeline timeline;
timeline = new Timeline(new KeyFrame(Duration.millis(2500),
(evt) -> {
System.out.println("hello");
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
error: cannot find symbol
Duration.millis(2500),
symbol: method millis(int)
location: class Duration
1 error
This is the full code of it, just wanna test out the timeline thing to create a timer for an event.
In this case, I try to add a simple println "Hello" to see if it works, but it doesn't.
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.scene.image.Image;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.ResourceBundle;
import java.util.TimerTask;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javax.swing.*;
public class Scene2Controller implements Initializable {
#FXML
private JFXButton btn_submit;
#FXML
private JFXTextField txt_email;
#FXML
private JFXPasswordField txt_pass;
#FXML
private ImageView img1;
#FXML
private ImageView img2;
#FXML
private Pane pane_loader;
#FXML
private void handleButtonAction(ActionEvent event) throws IOException, Exception {
if (event.getSource() == btn_submit) {
if (txt_email.getText().equals("") && txt_pass.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please fill in the field", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
pane_loader.setVisible(true);
pane_loader.toFront();
if (txt_email.getText().equals("root") && txt_pass.getText().equals("test1234"))
{
Timeline timeline;
timeline = new Timeline(new KeyFrame(Duration.millis(2500l),
(evt) -> {
System.out.println("hello");
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
else
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
Image image = new Image ("/image/business_care_clinic_1282308.jpg");
Image image2 = new Image ("/image/815.gif");
img1.setImage(image);
img2.setImage(image2);
}
}
Huge thanks to #kleopatra and #Pagbo , I just realised that I've imported the wrong class.
my bad.. :)
import java.time.Duration;
to
import javafx.util.Duration;
Would it be possible to wrap an entire JavaFX application into a while loop to trigger automatic events? For example in a auction house simulator:
package main;
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 {
// Standard JavaFX boilerplate
primaryStage.show();
while(true){
// Get price of this item
// Update table of listings
}
}
public static void main(String[] args) {
launch(args);
}
I know that the loop would block the main thread for the GUI so I was thinking of using the system time + a few seconds in the while loop instead:
double systemTime = systemTime;
double executeTime = systemTime + 5;
while(systemTime != executeTime){
//Do things
executeTime = systemTime + 5;
}
At any rate I know what I need, I just don't know what it's called or implemented.
Well you were right this would most likely block the JavaFX thread, but so would your second statement. As its still looping blocking the thread. What you could do is use
the ScheduledExecutorService to run a Runnable or thread to periodically refresh the gui and update it with what ever information. But you should make sure to wrap the parts which change the GUI within the JavaFX Thread which you can simply do so by using Platform.runLater method. Or for more heavy duty background tasks, use the Task class for JavaFX.
I will use the runLater method for simplicity sakes.
Example:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.lang.management.PlatformManagedObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class Example extends Application {
private Scene myscene;
private TextArea exampleText;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
//JavaFX boilerplate
VBox rootVBox = new VBox();
exampleText = new TextArea();
VBox.setVgrow(exampleText, Priority.ALWAYS);
myscene = new Scene(rootVBox);
rootVBox.getChildren().add(exampleText);
//End of JavaFX boilerplate
// Scheduler to update gui periodically
ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor();
Random r = new Random();
Runnable addNewNumber = () -> {
Platform.runLater(()->{
System.out.println("I Just updated!!!");
String newNumber = Integer.toString(r.nextInt(100));
System.out.println("adding "+ newNumber +" to textfield ");
exampleText.appendText(newNumber+"\n");
});
};
executor.scheduleAtFixedRate(addNewNumber, 0, 500, TimeUnit.MILLISECONDS);
primaryStage.setScene(myscene);
primaryStage.show();
}
}
I wanted to give SceneBuilder a try because its pain to center objects manually (in code). Unfortunately root is taken from me when I use FXML, so I can't set group as root. What I want to do is to operate on canvas (that is added to root group) while still having SceneBuilder FXML file working.
For example when Ive set root sceneBuilder FXML file then i couldnt add sprites to it in code. Vice versa when I have set root as group then the application wasnt making use of FXML file.
How to connect these? Below is code with use of group as root. I have added sprite, which is not visible in SceneBuilder. Also the button made in SceneBuilder is not visible after compiling the application.
Code:
package zegelardo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.animation.AnimationTimer;
import javafx.event.EventHandler;
import javafx.scene.input.KeyEvent;
import javafx.scene.control.Button;
import java.util.ArrayList;
import java.util.Iterator;
public class Zegelardo extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Group rootx = new Group();
Scene scene = new Scene(rootx);
scene.setFill(Color.BLACK);
stage.setScene(scene);
stage.setTitle("Zegelardo");
Sprite tlo = new Sprite();
tlo.setImage("test55.gif");
tlo.setPosition(0, 0);
Canvas canvas = new Canvas( 800, 400 );
rootx.getChildren().add(canvas);
GraphicsContext gc = canvas.getGraphicsContext2D();
// gc.clearRect(0, 0, 1000,800);
// briefcase.render( gc );
tlo.render(gc);
// Group leaf = new Group();
// root.getChildren().add(canvas);
// Canvas canvas = new Canvas( 800, 400 );
//leaf.getChildren().add( canvas );
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
So I created a FXML interface using JavaFX's Scene Builder tool, and I'm trying to load it via my code. However, I ran into this error when trying to load my FXML document: FXMLLoader.load can't be resolved to a type.
Here is my code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Driver extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Parent root = new FXMLLoader.load(getClass().getResource("interface.fxml"));
Scene scene = new Scene(root, 600, 400);
primaryStage.setTitle("Wiki Scraper");
primaryStage.setScene(scene);
primaryStage.show();
}
}
My FXML document is contained in the most top level project folder.
What am I doing incorrectly?
Try to split it up this way:
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("DefaultFrame.fxml"));
Parent root = (Parent) loader.load();
dfController = loader.getController();
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>