I want to switch between 2 different scenes:
Scene 1: ScrollPane where I load different images.
Scene 2: the stage is transparent and there's only 1 button to return to Scene 1.
I´ve been able to do it just having a different fxml for each scene and creating a new scene every time I switch them.
The problem is when I switch from scene 2 to scene 1, all the images loaded in scene 1 aren't there (It´s obvious as I´m creating a new scene rather than "loading" Scene1.
Is there any way to keep the images already loaded when I swith from Scene 2 to Scene 1?
Scene1
public class ControllerImpl implements Initializable, ControlledScreen {
ScreensController myController;
public void initialize(URL url, ResourceBundle rb) {
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
#FXML
private void goToScreen2(ActionEvent event) throws Exception{
try {
Stage primaryStage = (Stage) hideStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/arrow.fxml"));
Parent root1 = fxmlLoader.load();
primaryStage.setScene(new Scene(root));
}
catch (Exception e) {
e.printStackTrace();
}
}
#FXML
public javafx.scene.control.Button hideStage;
}
Scene2:
public class ControllerArrow implements Initializable{
ScreensController myController;
#Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
#FXML
public javafx.scene.control.Button showStage;
#FXML
private void goToScreen1(ActionEvent event) throws Exception{
try{
Stage stage = (Stage) showStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
You can switch the scene of your stage like:
Stage stage = (Stage) scene.getWindow();
stage.setScene(yourScene);
So when you have an instance of your scene, you can apply it to your stage
Related
Basically i am using JavaFX scene buidler 2.0 i want to change the scene from one to other without using any button for them.
Main File
public class OurFirstProject extends Application {
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.setFullScreenExitHint("");
//stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
public static void main(String[] args) {
Application.launch(args);
}
}
public class FXMLDocumentController implements Initializable {
#FXML
private void change(ActionEvent event) throws IOException {
Parent sceneChange = FXMLLoader.load(getClass().getResource("Change.fxml"));
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene changeScene = new Scene(sceneChange, screenBounds.getWidth(), screenBounds.getHeight());
Stage Window = (Stage) ((Node) event.getSource()).getScene().getWindow();
Window.setScene(changeScene);
Window.setFullScreen(true);
Window.show();
}
int a = 0;
#FXML
public Button helloButton;
#FXML
private Label ourLabel;
#FXML
private void printHello(ActionEvent e) {
a++;
if (a % 2 == 0) {
ourLabel.setText("Hello World! Kyun" + a);
} else {
ourLabel.setText("Hello Dunia" + a);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
enter code here
}
The Scene Mentioned in the FXML File Name"change", i want to Run this Scen without Using Button I wanna run this on the delay of five second of FIrst scene.
You can use a Timer() like,
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Platform.runLater(new Runnable() {
#Override
public void run() {// After this you can add your change.fxml load code
Parent root = null;
try {
root = fxmlLoader.load();
}catch(Exception e)
{
//Exception catch code here
}
primaryStage.show();//Here you can write your show code like window.show()
}
});
}
},5000);// 5000- time delay in milliseconds
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();
}
}
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.
I have a simple button that i want to click and open a new scene but i made the new scene in another class but it doesn't work and it does not show my errors either.
Here is my Controller class
public class Controller
{
public TextField txtUsername;
public TextField txtEmail;
public TextField txtPass;
public TextField txtPhone;
public Button btnLogin;
}
public void buttonClicked()
{
Chattingform chattingform = new Chattingform ();
}
Here is my Chattingform class
public class Chattingform extends Application
{
#Override
public void start (Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Chattingform.fxml"));
try {
Parent root = fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setOpacity(1);
stage.setTitle("Exchat");
stage.setScene(new Scene (root, 450, 450));
stage.showAndWait();
}catch ( IOException io )
{
JOptionPane.showMessageDialog (null, "Could not open ");
}
}
From my experince, all JavaFX Controllers have:
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
Hi I am creating a login page and a dashboard page I want to close or hide my login page and Show Dashboard When I Click on Login Button. but its not working
public class LoginController implements Initializable {
#FXML
private TextField txtUser;
#FXML
private PasswordField txtPassword;
#FXML
private Label message;
#FXML
private Label lblUser;
#FXML
private Label lblPassword;
#FXML
private void OpenDashBoard(ActionEvent event) {
try {
FxmlMethods object = new FxmlMethods();
// showFxml method usd for open a new window named DashBoard.fxml
object.showFxml("/DashBoard/DashBoard.fxml", "/ DashBoard/DashboardStyle.css", "Dash Board");
} catch (Exception exception) {
exception.printStackTrace();
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
And My Main Class is :
public class DATACOLLECTION extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
stage.getIcons().add(new Image("/images/icon.png"));
Scene scene = new Scene(root);
stage.setTitle("Login");
stage.setScene(scene);
scene.getStylesheets().addAll(this.getClass().getResource("Login.css").toExternalForm());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
please help me
I am using this and its working Thanks to all for your Response.
#FXML
private void CloseAction(ActionEvent event) {
Stage stage = (Stage) Close.getScene().getWindow();
stage.close();
}