JavaFX application won't compile in NetBeans IDE 8.1 - javafx

I am trying to run this application in which I used event-handling and it won't compile and won't show anything. I am currently using NetBeans 8.1. What is that I did wrong. Also it is not showing any errors.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class event extends Application {
public void start(Stage primaryStage)
{
HBox pane = new HBox();
pane.setAlignment(Pos.CENTER);
Button btOk = new Button("OK");
Button btCancel = new Button("Cancel");
OkHandlerClass handler1 = new OkHandlerClass();
btOk.setOnAction(handler1);
CancelHandlerClass handler2 = new CancelHandlerClass();
pane.getChildren().addAll(btOk, btCancel);
Scene scene = new Scene(pane);
primaryStage.setTitle("Handle the fucking event");
primaryStage.setScene(scene);
primaryStage.show();
}
class OkHandlerClass implements EventHandler<ActionEvent>{
public void handle(ActionEvent e) {
System.out.println("OK button clicked");
}
}
class CancelHandlerClass implements EventHandler<ActionEvent>{
public void handle(ActionEvent event) {
System.out.println("Cancel button clicked");
}
}
public static void main(String[] args) {
launch(args);
}
}

Related

Editable ComboBox cleared when i pressed space or arrow buttons - JavaFX

I am making a Editable ComboBox which show any values contain the input and it works fine.
But the problem is in the input field, whenever i typed Space or arrow buttons the input field keep reseting.
I tried setonKeyPressed and setonKeyTyped too but it isn't solve the problem.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
ObservableList<String> fruit = FXCollections.observableArrayList();
fruit.add("apple");
fruit.add("orange");
fruit.add("banana");
ComboBox myComboBox = new ComboBox();
myComboBox.setEditable(true);
myComboBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
myComboBox.getItems().clear();
myComboBox.show();
String input = myComboBox.getEditor().getText();
for ( int i = 0 ; i < fruit.size(); i++) {
if(fruit.get(i).contains(input)) { //Check if the list contains the input
myComboBox.getItems().add(fruit.get(i));
}
}
}
});
HBox hbox = new HBox(myComboBox);
primaryStage.setScene(new Scene(hbox, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Error with handle(Event) in EventHandler

I'm new new to javafx and I would like just to print the the coordinates of the mouse click.
I've seen examples but I've got an error in the EventHandler line that I don't understand, it says:
anonymous javafxapplication2.JavaFXApplication2$1 is not abstract and does not override abstract method handle(Event) in EventHandler
What's the problem? Thanks!
package javafxapplication2;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class JavaFXApplication2 extends Application {
#Override
public void start(Stage primaryStage) {
Line line = new Line();
Group root = new Group(line);
Scene scene = new Scene(root, 800, 800);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {
#Override
public void handle(MouseEvent event) {
System.out.println(event.getX());
System.out.println(event.getY());
}
}
primaryStage.setTitle("Disegna linee");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
From looking at the documentation here on javafx handlers I believe your problem is that you need to provide the type to the new eventHandler like
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() { // Was missing the <MouseEvent>
#Override
public void handle(MouseEvent event) { ... };
});

Stage will not close with BorderPane after phone is rotated 90 degrees

I believe I've found a bug with BorderPane. The stage will close as expected if you do not rotate the phone. However, if you rotate the phone and then click on Close the screen does nothing until you rotate the phone back to the original position and then is displays the primaryStage. The code to recreate it is very simple.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
final Stage stage = new Stage();
Button closeBtn = new Button("Close");
closeBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
stage.close();
}
});
FlowPane pane = new FlowPane();
pane.getChildren().addAll(new Label("Hello World!"), closeBtn);
Scene scene = new Scene(pane);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(primaryStage);
stage.setScene(scene);
stage.show();
}
});
BorderPane borderPane = new BorderPane();
borderPane.setPrefHeight(Screen.getPrimary().getVisualBounds().getMaxY());
borderPane.setPrefWidth(Screen.getPrimary().getVisualBounds().getMaxX());
borderPane.setCenter(btn);
primaryStage.setScene(new Scene(borderPane));
primaryStage.show();
}
Does anyone know how to work around this issue?
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
}
}
apply plugin: 'org.javafxports.jfxmobile'
mainClassName = 'helloworld.HelloWorld'
version = '8u40'
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
jfxmobile {
ios {
forceLinkClasses = ['ensemble.**.*']
}
android {
applicationPackage = 'org.javafxports.ensemble'
}
}
Thank you for the hints. I removed the extra stage and scene. My workaround is this.
package helloworld;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
final Group group = new Group();
final BorderPane borderPane = new BorderPane();
borderPane.setPrefHeight(Screen.getPrimary().getVisualBounds().getMaxY());
borderPane.setPrefWidth(Screen.getPrimary().getVisualBounds().getMaxX());
final Button btn = new Button("Say 'Hello World'");
borderPane.setCenter(btn);
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Button closeBtn = new Button("Close");
closeBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
group.getChildren().setAll(borderPane);
}
});
FlowPane pane = new FlowPane();
pane.getChildren().addAll(new Label("Hello World!"), closeBtn);
group.getChildren().setAll(pane);
}
});
group.getChildren().add(borderPane);
primaryStage.setScene(new Scene(group));
primaryStage.show();
}
}
While I can reproduce your issue, and you can report it here, on mobile things are a little bit different from desktop, and you shouldn't create a different stage or a different scene: just switch the nodes on the scene instead.
If you create the project with Gluon's plugin, you'll notice it uses View nodes, and you can easily switch between them, but always related to the same root (GlassPane) on the same scene.
If you need dialogs, you can use JavaFX built-in Dialogs, but Gluon already has its own dialogs, more adapted to a mobile environment.

JavaFX Stage not fully loaded

I have this whole mess of Controls and Transitions in the Scene, and whenever an error or message is made, I call FormBuilder.say(String x).
The sample included doesn't error as much, because it's empty. But when all the stuff are active, the pop up from FormBuilder.say(String x) comes out as a mess.
If I wait long enough, attempt to interact, or if I minimize then restore the Stage, it loads properly. But many times, it's just blank.
My laptop runs Ubuntu 15.10, with approx 2GB RAM. No Video Card.
Included is a screen cap of the messy picture.
Included is the code that's affected during the bug. I've striped out some of the less suspected parts. (EDITED)
package solo;
import javafx.animation.Animation;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.util.Duration;
public class FormBuilder extends Application{
Stage ps;
public FormBuilder(){
}
public void begin(String[] args){
launch(args);
}
public void start(Stage primaryStage) {
ps = primaryStage;
ps.setTitle("E-VIL v0.0");
ps.setScene(new Scene(scene(), 800, 600));
ps.show();
}
public void say(String things){
Stage stage = new Stage(StageStyle.UTILITY);
stage.initOwner(ps);
Text text = new Text(things);
Button btn = new Button("Okay");
btn.setOnAction(e -> {stage.close();});
VBox pane = new VBox(text,btn);
pane.setAlignment(Pos.TOP_CENTER);
text.setLayoutY(12);
pane.autosize();
stage.setScene(new Scene(pane));
stage.sizeToScene();
stage.setOnShown(e -> {stage.requestFocus();});
stage.showAndWait();
}
public Pane scene(){
Pane pn = new Pane();
Button btn = new Button("Say Something");
btn.setOnAction(e->{FormBuilder.this.say("Something");});
btn.setLayoutX(0);
btn.setLayoutY(10);
pn.getChildren().add(btn);
pn.sceneProperty().addListener(new ChangeListener<Scene>(){
#Override
public void changed(ObservableValue<? extends Scene> arg0, Scene arg1, Scene arg2) {
System.out.println("new Scene");
if(arg2 != null)
arg2.windowProperty().addListener(new ChangeListener<Window>(){
#Override
public void changed(ObservableValue<? extends Window> observable, Window oldValue,
Window newValue) {
System.out.println("new window");
if(newValue != null){
Rectangle rect = new Rectangle(0,30,300,300);
rect.setFill(Color.RED);
pn.getChildren().add(rect);
Rectangle box = new Rectangle(30,60,50,50);
TranslateTransition tt = new TranslateTransition(Duration.seconds(1), box);
tt.setByX(160);
tt.setByY(160);
tt.setAutoReverse(true);
tt.setCycleCount(Animation.INDEFINITE);
pn.getChildren().add(box);
tt.play();
}
}
});
}
});
return pn;
}
}
package solo;
public class Main {
public static void main(String args[]){
FormBuilder fb = new FormBuilder();
fb.begin(args);
}
}

JavaFX ImageView Transition

I'm trying to create image gallery and use some image animations. Problem is with ImageView. I would like to play() RotateTransition from some method and call this method any time but it's not working at all. There should be some issue with threads but even if it is called from new thread nothing is happening. Is there any solution how to work with ImageView and Transitions generally?
public class ImageGallery extends ImageView{
RotateTransition rt;
public ImageGallery() {
setImage(new Image("/img/01.jpg"));
setPreserveRatio(true);
rt = new RotateTransition(Duration.millis(800), this);
rt.setByAngle(90);
//this works but not what I need
//fitWidthProperty().addListener(e -> rt.play());
}
public void rotateRight(){
rt.play(); //nothing
//run later is not working too
//Platform.runLater(new ViewTransition(this));
}
}
Thanks
As per user comments in the question, adding a MCVE
Main.java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
ImageGallery gallery = new ImageGallery();
VBox box= new VBox(gallery);
box.setAlignment(Pos.CENTER);
Scene scene = new Scene(box, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
gallery.rotateRight();
}
public static void main(String[] args){
launch(args);
}
}
ImageGallery.java
import javafx.animation.RotateTransition;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
public class ImageGallery extends ImageView{
RotateTransition rt;
public ImageGallery() {
setImage(new Image("http://jaxenter.com/wp-content/uploads/2013/03/javafx.1.png"));
setPreserveRatio(true);
rt = new RotateTransition(Duration.millis(800), this);
rt.setByAngle(90);
}
public void rotateRight(){
rt.play();
}
}

Resources