I'm a newbie in JavaFX. I made a fx application it has a home and another jfxml file.
This is HomeContoler.java file for open another jfxmlfile
#FXML
public void actionIngredencesReg(ActionEvent event) {
try {
mainHome.setOpacity(0.2);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Ingrdences.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root1);
stage.setScene(scene);
stage.show();
// mainHome.setOpacity(1);
} catch (Exception e) {
e.printStackTrace();
}
}
when I click on the menu items it will works likes
this the close code of Ingredients.fxml file
#FXML
Label close_label;
this is a label, here in action
#Override
public void initialize(URL url, ResourceBundle rb) {
close_label.setOnMouseClicked(e -> {
//this is code for close only science
Stage stage = (Stage) close_label.getScene().getWindow();
stage.close();
});
}
but after close Ingredients.fxml file home.fxml file be like this
[this is not needed for me][4]
I want to convert home.fxml file like this
as like this after closing the ingredient.fxml
i want to setOpcaity of home.fxml file into 1 after close the ingredient.fxml file
anyone can help me to fix it...
Just revert the opacity when the stage is hidden:
stage.setOnHidden(e -> mainHome.setOpacity(1));
Related
There appears to be a delay in applying my dark UI CSS to a stage when it first appears. This causes an ugly looking white flash. How can I prevent this? Code and demo gif below.
if (presetDialog == null) {
// Create the preset dialog
presetDialog = new Stage();
presetDialog.initOwner(stage);
presetDialog.setTitle("Edit Preset");
// Load fxml
FXMLLoader loader = new FXMLLoader(getClass().getResource("EditPreset.fxml"));
try {
loader.setController(this);
BorderPane pane = loader.load();
Scene scene = new Scene(pane);
scene.getStylesheets().addAll(stage.getScene().getStylesheets());
presetDialog.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
presetDialog.showAndWait();
Here is the tableview controller :
Trying to send the data associated with the selected row into InstructorProfileContoller, when the button is pressed. The code works and prints you "You selected an Instructor" but then doesn't send any data over
Instructor instructor = (Instructor) InsTable.getSelectionModel().getSelectedItem();
if(instructor != null){
System.out.println("You select an instructor");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("InstructorProfile.fxml"));
Parent root = fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("update instructor");
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
InstructorProfileController instructorProfileController = fxmlLoader.getController();
instructorProfileController.countrychoicebox.setValue(instructor.getCountryID());
instructorProfileController.insstatuschoicebox.setValue(instructor.getIns_statusCode());
instructorProfileController.firstnametextfield.setText(instructor.getIns_firstName());
instructorProfileController.lastnametextfield.setText(instructor.getIns_lastName());
instructorProfileController.addresstextfield.setText(instructor.getIns_address());
instructorProfileController.citytextfield.setText(instructor.getIns_city());
// instructorProfileController.dateofBirthpicker.setValue(instructor.getIns_dateOfBirth());
instructorProfileController.gendertextfield.setText(instructor.getIns_sex());
instructorProfileController.zipcodetextfield.setText(String.valueOf(instructor.getIns_zipcode()));
}
initialize();
}
The method call stage.showAndWait() shows the stage, and then waits until it is closed. So you don't set the values until after the user has closed the window. Just move the calls that set the values to before the call to showAndWait().
Instructor instructor = (Instructor) InsTable.getSelectionModel().getSelectedItem();
if(instructor != null){
System.out.println("You select an instructor");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("InstructorProfile.fxml"));
Parent root = fxmlLoader.load();
InstructorProfileController instructorProfileController = fxmlLoader.getController();
instructorProfileController.countrychoicebox.setValue(instructor.getCountryID());
instructorProfileController.insstatuschoicebox.setValue(instructor.getIns_statusCode());
instructorProfileController.firstnametextfield.setText(instructor.getIns_firstName());
instructorProfileController.lastnametextfield.setText(instructor.getIns_lastName());
instructorProfileController.addresstextfield.setText(instructor.getIns_address());
instructorProfileController.citytextfield.setText(instructor.getIns_city());
// instructorProfileController.dateofBirthpicker.setValue(instructor.getIns_dateOfBirth());
instructorProfileController.gendertextfield.setText(instructor.getIns_sex());
instructorProfileController.zipcodetextfield.setText(String.valueOf(instructor.getIns_zipcode()));
Stage stage = new Stage();
stage.setTitle("update instructor");
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
}
initialize();
As an aside, it would be a much better style to just define a method setInstructor(...) in your InstructorProfileController:
public class InstructorProfileController {
// ...
public void setInstructor(Instructor instructor) {
countrychoicebox.setValue(instructor.getCountryID());
insstatuschoicebox.setValue(instructor.getIns_statusCode());
firstnametextfield.setText(instructor.getIns_firstName());
lastnametextfield.setText(instructor.getIns_lastName());
addresstextfield.setText(instructor.getIns_address());
citytextfield.setText(instructor.getIns_city());
// dateofBirthpicker.setValue(instructor.getIns_dateOfBirth());
gendertextfield.setText(instructor.getIns_sex());
zipcodetextfield.setText(String.valueOf(instructor.getIns_zipcode()));
}
// ...
}
and then, of course, just do
Instructor instructor = (Instructor) InsTable.getSelectionModel().getSelectedItem();
if(instructor != null){
System.out.println("You select an instructor");
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("InstructorProfile.fxml"));
Parent root = fxmlLoader.load();
InstructorProfileController instructorProfileController = fxmlLoader.getController();
instructorProfileController.setInstructor(instructor);
// ...
}
That way you don't expose the UI controls outside the relevant controller, which will make your code far more maintainable.
Im building an application that shows a window that ask the user if he want to suspend the computer with two button options, one of them its a YES and the PC suspends.
The other button named "Later" supposed to hide the window and after an hour it appears again and ask the same question.
Code for the "later buttton"
noButton.setOnAction(event -> {
Done=false; //boolean to close and open the frame
Gui gui = new Gui();
try {
gui.start(classStage);
} catch (Exception e) {
e.printStackTrace();
}
});
The boolean that you see in the code is bc it was the way i think i could control that, trust me i tried in different ways but no one just help me with the issue, here is the code of the GUI class
public class Gui extends Application {
public Stage classStage = new Stage();
public static boolean Done=true;
public static boolean flag=true;
public Gui() {
}
#Override
public void start(Stage primaryStage) throws Exception {
Done = Controller.isDone();
classStage = primaryStage;
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
primaryStage.setX(primaryScreenBounds.getMaxX() - primaryScreenBounds.getWidth());
primaryStage.setY(primaryScreenBounds.getMaxY() - primaryScreenBounds.getHeight());
primaryStage.setAlwaysOnTop(true);
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
primaryStage.setTitle("Alerta suspencion de equipo");
primaryStage.setScene(new Scene(root));
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UNDECORATED);
if (Controller.isDone() == true) {
primaryStage.show();
} else if(Controller.isDone() == false) {
primaryStage.hide();
Platform.exit(); // this is the only way that the windows close
}
}
i know that Platform.exit(); kills the program but when i only use .hide(); of the Stage nothing happens, the window never closed, the worst part is that when i use the Platform.exit() command i cant make the frame appear again...
Anyone knows a way maybe easier to hide and show a window after certain time? maybe im doing this wrong.
Regards.
It's not really clear what's going on in the code in your question. The bottom line is that you should never create an instance of Application yourself; the only Application instance should be the one created for you.
I don't actually see any need to have a separate class for the functionality you've shown (though you could, of course). All you need to do is hide classStage if the no button is pressed, and open it again in an hour:
noButton.setOnAction(event -> {
Done=false; //boolean to close and open the frame
classStage.hide();
PauseTransition oneHourPause = new PauseTransition(Duration.hours(1));
oneHourPause.setOnFinished(e -> showUI(classStage));
oneHourPause.play();
});
// ...
private void showUI(Stage stage) {
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stage.setX(primaryScreenBounds.getMaxX() - primaryScreenBounds.getWidth());
stage.setY(primaryScreenBounds.getMaxY() - primaryScreenBounds.getHeight());
stage.setAlwaysOnTop(true);
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
stage.setTitle("Alerta suspencion de equipo");
stage.setScene(new Scene(root));
stage.setResizable(false);
stage.initStyle(StageStyle.UNDECORATED);
stage.show();
}
Note that the FX Application will exit if the last window is closed, by default. So you should call Platform.setImplicitExit(false); in your init() or start() method.
I am assuming you are reloading the FXML file because the UI might have changed since it was previously loaded. Obviously if that's not the case, all you have to do is show the stage again as it is:
noButton.setOnAction(event -> {
Done=false; //boolean to close and open the frame
classStage.hide();
PauseTransition oneHourPause = new PauseTransition(Duration.hours(1));
oneHourPause.setOnFinished(e -> classStage.show());
oneHourPause.play();
});
I am creating a ListView with the ability to double click each item and have a window popup with inputs structured by an FXML file. The FXML file itemStep contains fx:controller="controller.ItemStep"
listViewVariable.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
if (mouseEvent.getClickCount() == 2) {
ItemStep item = listViewVariable.getSelectionModel()
.getSelectedItem();
if (item != null) {
try {
FXMLLoader isLoader = new FXMLLoader(Main.class.getResource("/view/itemStep.fxml"));
AnchorPane pane = isLoader.load();
Scene scene = new Scene(pane);
Stage stage = new Stage();
stage.setScene(scene);
item.setUrl(item.urlField.getText());
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
item.setUrl(item.urlField.getText());
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
I continue to get the following error using the above. I need to be able use the FXML file within this Stage.
Caused by: java.lang.InstantiationException: controller.ItemStep
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
... 44 more
Caused by: java.lang.NoSuchMethodException: controller.ItemStep.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 46 more
Your ItemStep class does not have a no argument constructor.
Does a new instance of ItemStep get recreated after each double[click]?
Yes, that is what you have wrote your code to do.
If you didn't wish to do that, you should invoke the load method on your FXMLLoader outside of your event handler, store the reference to the loaded pane in a final variable, and use that reference inside the event handler, rather than loading a new pane time every time the event handler is invoked.
when I invoke both the load FXMLLoader and final AnchorPane outside the event handler I get: AnchorPane#ed39805[styleClass=root]is already set as root of another scene
You can't add a node to more than one scene or to a single scene more than once, you either have to remove it from the original scene, or just re-use the original scene with a single instance of the node. I don't know what kind of behavior you desire, but probably, you want to just have a single window pop-up and make it modal, rather than creating a new window every time somebody clicks on something.
Basically, you do something like this (though this is just an outline that I have never compiled or executed as I don't completely know your complete requirements, nor what your ItemStep and urlField really are):
final FXMLLoader isLoader = new FXMLLoader(Main.class.getResource("/view/itemStep.fxml"));
final AnchorPane pane = isLoader.load();
final Scene scene = new Scene(pane);
final Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
listViewVariable.setOnMouseClicked(event -> {
if (mouseEvent.getButton().equals(MouseButton.PRIMARY) && (mouseEvent.getClickCount() == 2)) {
ItemStep item = listViewVariable.getSelectionModel().getSelectedItem();
if (item != null) {
stage.showAndWait();
item.setUrl(item.urlField.getText());
}
}
});
I am looking for a way to display an html file in a different stage once the help button is clicked.
public void handleButtonAction(ActionEvent event) throws IOException {
if (event.getSource() == help) {
stage = (Stage) help.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("help.fxml"));
WebView browser = new WebView();
Scene helpScene = new Scene(root);
Stage helpStage = new Stage();
helpStage.setTitle("Help Menu");
helpStage.setScene(helpScene);
URL url = getClass().getResource("readme.html");
browser.getEngine().load(url.toExternalForm());
helpStage.show();
}
}
Your code is fine except that you forgot to add the webview to the scene, do
((Pane) helpScene.getRoot()).getChildren().add(browser);