Full screen under JavaFX with FXML - javafx

I have already asked the question but it was not answered.
How can I switch to full screen mode in JavaFX by pressing a button?
But the button is created with FXML (JavaFX Scene Builder). (cannot find symbol (stage) )
If the button is created manually, then it works.
public class Buch extends Application implements Initializable
{
# Override
public void start (Stage primaryStage) throws IOException
{
Stage stage = primaryStage;
Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);
Scene scene = new Scene (root);
stage.setTitle ("Buch");
stage.setScene (scene);
stage.getIcons () add (new Image ("Icon.png"));
/ / stage.setFullScreen (true) / / Works
stage.show ();
}
# FXML
public void fullscreen (ActionEvent event)
{
/ / stage.setFullScreen (true) / / Does not work
/ / cannot find symbol (stage)
}
Works:
public class Buch extends Application implements Initializable
{
# Override
public void start (Stage primaryStage) throws IOException
{
Stage stage = primaryStage;
Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);
Scene scene = new Scene (root);
stage.setTitle ("Buch");
stage.setScene (scene);
stage.getIcons () add (new Image ("Icon.png"));
stage.show ();
btn.setOnAction (new EventHandler <ActionEvent> ()
{
public void handle (ActionEvent evt)
{
stage.setFullScreen (true);
}
});
}
Does not work (of course ?):
public class Buch extends Application implements Initializable
{
# Override
public void start (Stage primaryStage) throws IOException
{
Stage stage = primaryStage;
Parent root = FXMLLoader.load (getClass () getResource ("Buch.fxml").);
Scene scene = new Scene (root);
stage.setTitle ("Buch");
stage.setScene (scene);
stage.getIcons () add (new Image ("Icon.png"));
stage.show ();
* /
# FXML
public void fullscreen (ActionEvent event)
{
stage.setFullScreen (true) / / Does not work
/ / cannot find symbol (stage)
}
/ * / / Will not work
}
You can also take a look at this link. The question is more detailed here:
stackoverflow.com/questions/22820049/full-screen-under-javafx-with-fxml-does-not-work
How can I use the stage variable anywhere? Or is there another solution ?
Please help me.
On the internet there is NO answer to my question!?!
I am a Java beginner. :-)
Thanks for your help

Why is your Application also your controller? That seems like it won't work, full screen functionality or no full screen functionality.
In the controller, just inject the button (or any node, but the button would be the obvious one), and call getScene() and getWindow() in the event handler:
public class MyController {
#FXML
private Button fullScreenButton ;
#FXML
private void fullScreen(ActionEvent event) {
Stage stage = (Stage) fullScreenButton.getScene().getWindow();
stage.setFullScreen(true);
}
}

I'm not a JavaFX Expert, but this won't work in pure Java too.
The DI Container injects the method fullscreen(event) which doesn't know stage, declared in start().
Did u try to move it up to a class member ?
public class Buch
{
private Stage stage;
...
}

Related

JavaFX: Opening a stage after delay on button click

sorta new to Javafx. I'm running into a problem when in my controller class for my main stage, Im trying to: after clicking on a button, start a delay that will then open a new stage.
The problem comes to when loading the FXML file within the button action event and in the run() part of the Scheduler that I'm using for the delay.
I can't throw the exception in the run() because it clashes with itself?
And I can't catch it either in the Parent Root by doing :
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("Popup.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
because it just flat out won't run. I think Im approaching this poorly or just the wrong way entirely.
I can't seem to get this to work. The stage runs fine just on button click but I need the delay.
Code for Controller Class:
public class Controller implements Initializable {
public static int seconds;
public static boolean yes = false;
public String strtime = String.format("Current Date/Time : %tc", new Date());
#FXML
Button remind;
#FXML
Label secondsuntil,date;
#FXML
TextField Day, Minute, remindname;
#FXML
private Button Butt;
static String Nameoftask;
int days, minutes;
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
date.setText(strtime);
}
#FXML
private void handleButtonAction(ActionEvent actionEvent) throws IOException {
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
ScheduledFuture<?> countdown = scheduler.schedule(new Runnable() {
#Override
public void run(){
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Popup.fxml"));
primaryStage.setTitle(Nameoftask);
primaryStage.initModality(Modality.NONE);
primaryStage.setScene(new Scene(root, 200,50));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
primaryStage.setX(width);enter code here
primaryStage.setY(height*-1);
primaryStage.show();
try {
FXMLLoader loader = new
FXMLLoader(getClass().getResource("Popup.fxml"));
loader.setController(new Controller());
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}}, 1, TimeUnit.SECONDS);
}
}

JavaFX change ComboBox items (outside of initialize() method)

I am playing around with SceneBuilder and come across a few questions about the intialize() method and how to change ComboBox items after it's already been initialized in said method. So basically, after I set the items in initialize, I am not able to change them anymore from another method in the controller.
Here is my code:
public class AppController implements Initializable {
private ObservableList<String> list = FXCollections.observableArrayList();
private MainModel model;
#FXML
private ComboBox<String> cobUsers = new ComboBox<String>();
#Override
public void initialize(URL url, ResourceBundle rb) {
list.add("name1");
list.add("name2");
cobUsers.setItems(list); // this works!
}
public void initModel(MainModel model) {
this.model = model;
}
public void addItems(){
list.add("name3");
list.add("name4");
cobUsers.setItems(list); // this does not work. ComboBox items remain "name1" and "name2"
}
}
public class App extends Application {
private Stage primaryStage;
private AnchorPane rootLayout;
private AppController appController = new AppController();
MainModel model = new MainModel();
#Override
public void start(Stage primaryStage) {
appController.initModel(model);
this.primaryStage = primaryStage;
this.primaryStage.setTitle("App");
initRootLayout();
appController.addItems();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLDocument.fxml"));
rootLayout = (AnchorPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
So guess my question is, how can I access/change my ComboBox later on, after it's been initialized in intialize()?
Thanks! :)
UPDATE 1:
I have changed the initRootLayout() in the App class (see below) and it WORKS now. list now contains 4 items and all of them show up in the ComboBox after calling addItems(). Thanks everyone!
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("FXMLDocument.fxml"));
rootLayout = (AnchorPane) loader.load();
AppController controller = loader.<AppController>getController();
controller.addItems();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}

Switching scenes in JavaFX(FXML) error

So, I'm trying to switch scenes in JavaFX but I can't seem to get it to work when I hard coded it I was able to get it working by using lambda expressions.
public class Main extends Application {
Stage window;
Scene scene1;
Scene scene2;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
Label label = new Label("Welcome to the first scene");
Button bttn1 = new Button("Go to second scene");
bttn1.setOnAction(e -> window.setScene(scene2));
//Scene 1
VBox layout1 = new VBox(20);
layout1.getChildren().addAll(label, bttn1);
scene1 = new Scene(layout1, 400, 400);
//Scene 2
Button bttn2 = new Button("Go to first scene");
bttn2.setOnAction(e -> window.setScene(scene1));
StackPane layout2 = new StackPane();
layout2.getChildren().add(bttn2);
scene2 = new Scene(layout2, 400, 500);
window.setScene(scene1);
window.setTitle("Test");
window.show();
}
However the project involves a few different GUIs and I would prefer to design the GUI's in FXML Scene Builder than to hardcode them the FX way. However when I have tried to do the FXML way it hasn't worked an error is always appearing when I press the button.
Error message
This is the document controller code.
public class FXMLDocumentController implements Initializable {
#FXML
private Button button1;
#FXML
private Button button2;
#FXML
private void handleButtonAction(ActionEvent event) throws IOException {
Stage stage;
Parent root;
if(event.getSource() == button1){
stage=(Stage)button1.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
}
else{
stage=(Stage)button2.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
The error you posted says the code is trying to load a button as an anchor pane. Check too see if you have an anchorpane with the fx:I'd of button1.

Add KeyEvent for calculator numpad in JavaFX using MVC

I created in Eclipse a simple calculator using JavaFx and MVC pattern. I would like to add keylisteners in order to press the buttons of my calculator by simply pressing the buttons in my keyboard. I tried to add #onKeyPress in SceneBuilder and then a method onKeypress (with some coding inside) in my Controller class but nothing happens.Could you please give some general instructions how to implement something like this? Thanks!
Thanks for your comments. I added the following code snippet in App.java:
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
controller.numFromKeyboard(event.getCode().toString());
}
});
And also, I had to add:
Parent root = loader.load();
Controller controller = loader.getController();
// The above line MUST be
// inserted after root is loaded in order the controller of my
// app to be instantiated,
// otherwise we will get a null exception when handler will be
// invoked
App.java
public class App extends Application {
//controller = new Controller();
#Override
public void start(Stage primaryStage) {
try {
// Read file fxml and draw interface.
FXMLLoader loader = new FXMLLoader(getClass()
.getResource("/application/View.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("/application/application.css").toExternalForm());
Image icon = new Image(getClass().getResourceAsStream("/application/Assets/App.png"));
primaryStage.getIcons().add(icon);
primaryStage.setTitle("JavaFX Calculator by Dimitris Baltas");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
controller.numFromKeyboard(event.getCode().toString());
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX load FXML inside parent controller

I have a borderpane where I'm loading an fxml (alias FirstPanel) with relative controller inside of it and positioned in the center. The fxml contains a TableView and a button which should load another fxml (alias SecondPanel) and relative controller instead the first panel. Basically, the SecondPanel needs to show some details about the data selected in the table.
Is it possible to do it? How can I get the parent of my FirstPanel and use it for the SecondPanel instead of the first?
UPDATE
I've tried many solutions but without reach my goal. The application load the UsersMainPageController which contains only an AnchorPane like parent control, so this is the relative code:
[UsersMainPageController]
public class UsersMainPageController implements Initializable {
private PostOffice application;
#FXML
private AnchorPane ParentControl;
public void setApp(PostOffice application){
this.application = application;
}
public void loadPage(String pageName) {
try {
URL url = getClass().getResource(pageName);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(url);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream());
ParentControl.getChildren().clear();///name of pane where you want to put the fxml.
ParentControl.getChildren().add(page);
}
catch (IOException e) {
e.printStackTrace();
}
}
public void loadManageUsers () {
loadPage("UsersManage.fxml");
}
public void loadListUsers () {
loadPage("UsersList.fxml");
}
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
loadListUsers();
}
}
loadListUsers calls UsersList.fxml with the relative controller UsersListController that contains a TableView with some records and some buttons. When I click a specific button, it should call loadManageUsers with relative controller UsersManageController which contains some fields for editing data and inserting new users. When users are edited or inserted, it should be able to return to the previous page with the TableView and clear the current page (in this case UsersManageController).
[UsersListController]
public class UsersListController implements Initializable {
private UsersMainPageController mainController;
#FXML
private void handleButtonEditAction(ActionEvent event) throws IOException {
mainController.loadManageUsers();
}
}
[UsersManageController]
public class UsersManageController implements Initializable {
private UsersMainPageController mainController;
#FXML
private void handleButtonBackAction(ActionEvent event) throws IOException {
mainController.loadListUsers();
}
}
When I click from the UsersListController the ButtonEdit to load the UsersManageController, I get this error:
Caused by: java.lang.NullPointerException
at postoffice.multiuser.UsersListController.handleButtonAggiornaAction(UsersListController.java:210)
... 50 more
you can add your child fxml into parent controller..
1.first just take a anchor pane and set its bounds where you want to put your FXML code.
now try this...
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
openNewWindow("Example.fxml");
}
});
you can pass fxml name into function...
public void openNewWindow(String FXMLFile)
{
//ChildNode child;
try {
URL url = getClass().getResource(FXMLFile);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(url);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream());
anchor.getChildren().clear();///name of pane where you want to put the fxml.
anchor.getChildren().add(page);
}
catch (IOException e) {
e.printStackTrace();
}
}

Resources