Weird errors, JAVAFX FXML - javafx

Here is my code, I will put //and the error before each bit that causes an error.
#Override
public void//Syntax error on token "void", # expected start(Stage//Syntax error on token(s), misplaced construct(s) stage) throws//Syntax error on token "throws", interface expected Exception {
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
Scene scene = new Scene(root, 300, 275);
stage.setTitle("FXML Welcome");//Syntax error on token ";", # expected
stage.setScene(scene);//Syntax error on token ";", # expected
stage.show()//Syntax error, insert ")" to complete MethodDeclaration;
}

You seem to be missing the opening part of the `start method.
#Override
public void start(Stage stage) {
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
Scene scene = new Scene(root, 300, 275);
stage.setTitle("FXML Welcome");//Syntax error on token ";", # expected
stage.setScene(scene);//Syntax error on token ";", # expected
stage.show();//You are missing semi-colon here.
}

Related

Opening fxml file for certain time and go back to the main screen?

How do I display an fxml file for some seconds and close that fxml file and return to the mainpage.fxml?
I have tried to add hideThispage method to the initialize method in the controller class of the fxml but I guess the initialize methods just runs before the class is actually loaded so I cannot run this code.
#FXML
public void hideThisPage() throws InterruptedException, IOException {
Window window = new Stage();
PauseTransition pause = new PauseTransition(Duration.seconds(5));
pause.setOnFinished(e -> window.hide());
pause.play();
FxmlDisplay fxmlDisplay = new FxmlDisplay();
fxmlDisplay.stageSelection("View/Main.fxml");
}
To return to the main.fxml or any fxml I have a method that I call:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(path));
Parent root = fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
In my case, I have a print button in my printreceipt.fxml file that prints the contents. Then it switches to the printpage.fxml ( i am able to switch to the page) But my problem is the printpage.fxml is supposed to be displayed only for 5 seconds and close. then it should go back to main.fxml. how and where do I call the hidethisPage() method?
#FXML
void printRandomTicket(ActionEvent event) throws SQLException, ClassNotFoundException, JRException, IOException, InterruptedException {
FxmlDisplay fxmlDisplay = new FxmlDisplay();
((Node) event.getSource()).getScene().getWindow().hide();
fxmlDisplay.stageSelection("/View/Dialogs/PrintingTicket.fxml");
GenerateTicket generateTicket = new GenerateTicket();
generateTicket.generateTicket();
PrintTicket printTicket = new PrintTicket();
printTicket.printTicket();
}
I hope this clarifies it.

TextField ESCAPE Keycode not caught

I'm trying to catch an ESCAPE key press in a textfield.
I was expecting to put a simple event listener (or even an event filter) to the textfield. But nothing works.
It seems all key events are caught, except the ESCAPE key I 'm waiting for.
Can you provide any help please,
Thanks in advance
PS: the following bunch of code explains how I proceed.
public class KeyEventSample extends Application {
/**
* Constructor
*/
public KeyEventSample() {super();}
#Override
public void start(final Stage primaryStage) throws Exception {
final TextField textField = new TextField();
final TextArea textArea = new TextArea();
textField.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.ESCAPE)
textArea.appendText("ESCAPE");
else if (e.getCode() == KeyCode.ENTER)
textArea.appendText("ENTER");
else
textArea.appendText("->"+e.getCode().toString());
});
final BorderPane borderPane = new BorderPane();
borderPane.setCenter(textField);
borderPane.setBottom(textArea);
final Scene scene = new Scene(borderPane, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}

TextField in PopOver with strange behavior

I have a PopOver with a TextField with a strange behavior, this PopOver it's owned by other TextField because when I type the word 'Fernández' all keys are processed by the internal TextField except when I type a stressed vowel like 'á' that it's collected by the external TextField.
PopOver owned by TextField
But when i show the same PopOver owned by a button works fine and the internal TextField receives the letter 'á'
PopOver owned by Button
I would appreciate any help to solve it.
EDIT: Here you can see an example code to show this.
public class PopOverTest extends Application {
#Override
public void start(Stage primaryStage) {
CustomTextField externo = new CustomTextField();
ImageView imgView = new ImageView(new Image("test/image.png"));
externo.setLeft(imgView);
CustomTextField interno = new CustomTextField();
PopOver popOver = new PopOver();
popOver.setContentNode(interno);
popOver.stArrowLocation(PopOver.ArrowLocation.TOP_LEFT);
imgView.setOnMouseClicked(e -> {
popOver.show(imgView);
});
StackPane root = new StackPane();
root.getChildren().add(externo);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I found a solution for this.
Changing the external Textfield EventDispatcher and the problem is resolved
EventDispatcher dispatcher = externalTextField.getEventDispatcher();
then on focus of the internal TextField
externalTextField.setEventDispatcher(interntalTextField.getEventDispatcher());
and when lost focus restore the EventDispatcher
externalTextField.setEventDispatcher(dispatcher);
That's all folks!

JavaFX translateZ for a non-root node causes it to disappear

I'm trying to use the translateZ property on a VBox to move the panel "into the screen".
If I use setTranslateZ() on the root node this works fine. However if I change root.setTranslateZ(200); to panel.setTranslateZ(200); the window is blank.
public class Demo01HelloWorld3D extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Button button = new Button("Press me");
VBox panel = new VBox(button);
panel.setAlignment(Pos.CENTER);
panel.setDepthTest(DepthTest.ENABLE);
VBox root = new VBox(panel);
root.setAlignment(Pos.CENTER);
root.setDepthTest(DepthTest.ENABLE);
root.setTranslateZ(200);
// panel.setTranslateZ(200); <== I want this to work
Scene scene = new Scene(root, 320, 240, true);
primaryStage.setScene(scene);
scene.setCamera(new PerspectiveCamera(false));
primaryStage.show();
}
}
Setting translateZ on the root node
Setting translateZ on the panel node
Things I've tried
Setting depthTest attribute to enable - although I don't think this is necessary as it defaults to DepthTest.INHERIT
Lots of searching for similar questions!
Checking SCENE3D is enabled - yes it is
Checking the javadoc for translateZProperty
checked Z value is less than camera clippingFar property
Looked at Oracle JavaFX 3D tutorial - this does not specifically address 3D with standard controls and containers etc.
With panel.setTranslateZ(200); you're pushing the panel behind the root, so the root obscures it.
Add root.setStyle("-fx-background-color: transparent;"); and it works:
#Override
public void start(Stage primaryStage) throws Exception {
Button button = new Button("Press me");
VBox panel = new VBox(button);
panel.setAlignment(Pos.CENTER);
panel.setDepthTest(DepthTest.ENABLE);
VBox root = new VBox(panel);
root.setAlignment(Pos.CENTER);
root.setDepthTest(DepthTest.ENABLE);
root.setStyle("-fx-background-color: transparent;");
panel.setTranslateZ(200);
Scene scene = new Scene(root, 320, 240, true);
primaryStage.setScene(scene);
scene.setCamera(new PerspectiveCamera(false));
primaryStage.show();
}

Opening a new scene in java FX

I have two FXML files and one Controller.
I have posted the code in which i have tried to create a second stage (and failed).
Error message is:
javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader
How do I fix it?
#FXML private Scene trial_scene;
#FXML public Stage m;
#FXML public void click(Stage stage) throws IOException {
m = new Stage();
openWindow();
}
#FXML private void openWindow() throws IOException {
FXMLLoader root =FXMLLoader.load(
SampleController.class.getResource(
"settings.fxml"
)
);
m.initModality(Modality.WINDOW_MODAL);
m.setTitle("My modal window");
m.setScene(trial_scene);
m.show();
}
The static method FXMLLoader.load returns the root node (and so the objet hierarchy) defined in the .fxml file. In your case it seems to be an AnchorPane.
Your fist line should be
AnchorPane root = FXMLLoader.load(SampleController.class.getResource("settings.fxml"));
Use this object to construct your new scene, for example
Scene trial_scene = new Scene(root, 300, 300, Color.BLACK);
Then pass this scene to your new stage
m.setScene(trial_scene);

Resources