I am using a pane which has all the content what I want to do is set new content ones the next button is clicked. I could use set invisible and visible for the next content but this is not what I want I want to have it in the stackpane and so I have a image stored in their and the button.
Asked
Active
Viewed 928 times
0
-
If you want to remove the current content and replace it with new content you can do this: stackPane.getChildren().remove(yourObject) and then add your new content with stackPane.getChildren().add(newObject) – Frank Aug 29 '15 at 19:41
-
do you have email or something i really need help and i am a newbie so what i have is a image and 3-4 buttons so the name of the object is on the button so when the right button is clicked then it will give points to the score else they will have to try it again so the right button give the points and move to the next question – kio lo Aug 29 '15 at 20:02
-
Where is the question? Is it the image? Can you please explain what the other buttons do? Do you press a button and if the answer is correct you want to show a new image? – Frank Aug 29 '15 at 20:41
-
correct this what i want to do the buttons should also change – kio lo Aug 29 '15 at 21:20
-
1Seems you might actually be asking about how to [Create wizard in JavaFX](http://stackoverflow.com/questions/19198951/create-wizard-in-javafx). – jewelsea Aug 29 '15 at 23:43
-
yeah sort out but the button needs to change is like a guessing game where the user needs to guess the image and press the right option with the buttons then the next pane appears with a new question with a fresh image and the buttons – kio lo Aug 30 '15 at 07:37
1 Answers
0
You can add an imageview object to your stackpane and change the image within when the button is clicked.
UPDATED ANSWER
@Override
public void start(Stage primaryStage) {
Button btn = new Button("One");
Button btn2 = new Button("Two");
btn2.setTranslateX(btn.getTranslateX() + 60);
Image img1 = new Image ("https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Thumbs_up_font_awesome.svg/512px-Thumbs_up_font_awesome.svg.png");
Image img2 = new Image ("https://cdn2.iconfinder.com/data/icons/windows-8-metro-style/512/thumbs_down.png");
Image loadImg = new Image("http://www.ledr.com/colours/white.jpg");
ImageView imgHolder = new ImageView(loadImg);
imgHolder.setVisible(false);
imgHolder.setFitHeight(100);
imgHolder.setFitWidth(100);
//Offset stackapane
imgHolder.setTranslateY(-80);
Random rand = new Random();
btn.setOnAction(e->{
int randomNum = rand.nextInt(10) + 1;
imgHolder.setVisible(true);
if(randomNum % 2 == 0){
imgHolder.setImage(img1);
}
else{
imgHolder.setImage(img2);
}
});
//Repeat action for btn2
StackPane root = new StackPane();
root.getChildren().addAll(btn,btn2,imgHolder);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}

kye
- 2,166
- 3
- 27
- 41
-
i tried using the stack pane but is not working so i just used set visible true/false any suggestion how to improve that – kio lo Aug 31 '15 at 17:27
-
What exactly is not working within the stackpane? (keep in mind all element within a stackpane are centered, you have to set TranslateX and Y to change positioning of elements.) Also, ImageView cant be set to null, changing visibility accordingly is the best solution. I've updated my answer with a working example, hope that helps – kye Aug 31 '15 at 18:00
-
it easier to show what i mean if you have a email or team-viewer i can show which would be easier basically what i have done is user a canvas which is on pane and the images/buttons are on the canvas so i set to true/false how do i change the pane to a stack pane ? shall i change the pane to stackpane ? – kio lo Aug 31 '15 at 18:20
-
Could you post a sample of your code? That would give a better me understanding of what you're trying to do, the question is quite vague at the moment. – kye Aug 31 '15 at 18:24
-
do you have an email i can send it to you as zipped files is easier please or i can give u mine what ever suits you – kio lo Aug 31 '15 at 18:26
-
-
-
yes but still dont quite understand what youre trying do to or the source of your problem – kye Aug 31 '15 at 19:16
-