Okay so this is the code I used. Most of it comes from the JavaFX tutorial here.
I wanted my program to load a picture when I clicked the button. But it will do the prints but it will not load the picture.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.image.*;;
public class Browser extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Test");
Image ph1 = new Image("ph1.jpg");
ImageView disp1 = new ImageView();
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Test1");
disp1.setImage(ph1);
System.out.println("Test2");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You haven't added the disp1 ImageView to the root StackPane.
Try this:
StackPane root = new StackPane();
root.getChildren().add(btn);
root.getChildren().add(disp1);
Related
The buttons dont give a console output. Two buttons are in a Vbox at the bottom of a BorderPane and should print "new" or "continue" when pressed.
I followed a tutorial and tried to extend it to one more button.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application implements EventHandler<ActionEvent> {
Button btn1, btn2;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("title");
Button btn1 = new Button("new");
Button btn2 = new Button ("continue");
btn1.setOnAction(this);
btn2.setOnAction(this);
VBox vb = new VBox (btn1, btn2);
vb.setSpacing(10);
vb.setPadding(new Insets(20));
BorderPane root = new BorderPane();
root.setBottom(vb);
Scene scene = new Scene (root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
#Override
public void handle(ActionEvent event) {
if (event.getSource()==btn1) {
System.out.println("new!");
} else if (event.getSource()==btn2) {
System.out.println("continue!");
}
}
}
nothing happens, but there should be a output of "new or continue"
Remove class from buttons.
This way the code will use buttons from fields, instead of creating new ones.
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("title");
btn1 = new Button("new");
btn2 = new Button ("continue");
// the rest of the code will be the same
}
I made a javafx program using a horizontal SplitPane with a Canvas on one side in which something is drawn. When the SplitPane is resized the Canvas resizes with it.
Everything worked fine until I switched to Java 10. Suddenly the Canvas-side can only be expanded, not reduced.
Anyone has an idea why that is?
(The working Java version was 1.8.0_181)
package test;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
private static Display display;
#Override
public void start(Stage primaryStage) {
display = new Display();
StackPane stackPaneDisplay = new StackPane();
stackPaneDisplay.getChildren().add(display);
stackPaneDisplay.setStyle("-fx-background-color: white");
AnchorPane anchorPaneDisplay = new AnchorPane();
anchorPaneDisplay.getChildren().add(stackPaneDisplay);
AnchorPane.setBottomAnchor(stackPaneDisplay, Double.MIN_VALUE);
AnchorPane.setTopAnchor(stackPaneDisplay, Double.MIN_VALUE);
AnchorPane.setLeftAnchor(stackPaneDisplay, Double.MIN_VALUE);
AnchorPane.setRightAnchor(stackPaneDisplay, Double.MIN_VALUE);
display.widthProperty().bind(stackPaneDisplay.widthProperty());
display.heightProperty().bind(stackPaneDisplay.heightProperty());
StackPane stackPaneLeft = new StackPane();
SplitPane splitPane = new SplitPane();
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.getItems().addAll(stackPaneLeft, anchorPaneDisplay);
BorderPane root = new BorderPane();
root.setCenter(splitPane);
Scene scene = new Scene(root);
primaryStage.setMaximized(true);
primaryStage.setScene(scene);
primaryStage.show();
splitPane.setDividerPositions(0.2);
}
public static void main(String[] args) {
launch(args);
}
}
and the Canvas-Class
package test;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class Display extends Canvas {
GraphicsContext gc = this.getGraphicsContext2D();
public Display() {
widthProperty().addListener(e -> {
draw();
});
}
public void draw() {
gc.strokeOval(500, 500, 100, 100);
}
}
Thanks in advance
I have the image files in my bin folder but when I run my program all I have is my button options that are not being replaced with my images. I think it has something to do with the file: in front of the images but I'm not sure. The images are not actually a moving gif (dunno why its called gif).
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
HBox root = new HBox();
Button btnKings;
Button btnDucks;
Button btnSharks;
Button btnBlues;
btnKings = new Button("Kings");
btnDucks = new Button("Ducks");
btnSharks = new Button("Sharks");
btnBlues = new Button("Blues");
Image imgKings = new Image("file:kings.jpg");
btnKings.setGraphic(new ImageView(imgKings));
Image imgDucks = new Image("file:ducks.gif");
btnDucks.setGraphic(new ImageView(imgDucks));
Image imgSharks = new Image("file:sharks.gif");
btnSharks.setGraphic(new ImageView(imgSharks));
Image imgBlues = new Image("file:blues.gif");
btnBlues.setGraphic(new ImageView(imgBlues));
HandleButtonClick clickEvent = new HandleButtonClick();
btnKings.setOnAction(clickEvent);
btnDucks.setOnAction(new HandleButtonClick("You clicked the ducKS!"));
btnKings.setOnAction(new HandleButtonClick("You clicked the Kings"));
btnSharks.setOnAction(new HandleButtonClick("You clicked the sharks"));
btnBlues.setOnAction(new HandleButtonClick("You clicked the blues"));
root.getChildren().add(btnKings);
root.getChildren().add(btnDucks);
root.getChildren().add(btnSharks);
root.getChildren().add(btnBlues);
Scene scene = new Scene(root, 960, 130);
primaryStage.setTitle("HockeyButtons");
primaryStage.setScene(scene);
primaryStage.show();
}
}
I cant find out how to replace the words buttons with the pictures
Nghia Duong
If you want only images to be shown on the button but text then you should create Button without text.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
HBox root = new HBox();
Button btnKings;
Button btnDucks;
Button btnSharks;
Button btnBlues;
btnKings = new Button();
btnDucks = new Button();
btnSharks = new Button();
btnBlues = new Button();
Image imgKings = new Image("kings.jpg");
btnKings.setGraphic(new ImageView(imgKings));
Image imgDucks = new Image("ducks.gif");
btnDucks.setGraphic(new ImageView(imgDucks));
Image imgSharks = new Image("sharks.gif");
btnSharks.setGraphic(new ImageView(imgSharks));
Image imgBlues = new Image("blues.gif");
btnBlues.setGraphic(new ImageView(imgBlues));
root.getChildren().add(btnKings);
root.getChildren().add(btnDucks);
root.getChildren().add(btnSharks);
root.getChildren().add(btnBlues);
Scene scene = new Scene(root, 960, 130);
primaryStage.setTitle("HockeyButtons");
primaryStage.setScene(scene);
primaryStage.show();
}
}
And here you are showing the path of the image incorrectly.
Image imgKings = new Image("file:kings.jpg");
You'd better show absolute path or if the images are in classpath folder then by:
String absolutePathToIcon =
getClass().getResource("kings.jpg").toExternalForm();
Image imgKings = new Image(absolutePathToIcon);
I have a stage that acts as popup,I want to close the stage if the user clicks outside that stage,how to achieve that,My code for the stage acting as popup is as follows:
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("colorchange.fxml"));
Scene sce = new Scene(root,400,400);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(color.getScene().getWindow());
stage.setScene(sce);
stage.showAndWait();
Register a listener with stage.focusedProperty() and call stage.hide() if the focused property changes to false.
SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class CloseWindowOnClickOutside extends Application {
#Override
public void start(Stage primaryStage) {
Button showPopup = new Button("Show popup");
showPopup.setOnAction(e -> {
Stage popup = new Stage();
Scene scene = new Scene(new Label("Popup"), 120, 40);
popup.setScene(scene);
popup.initModality(Modality.APPLICATION_MODAL);
popup.initOwner(primaryStage);
popup.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
if (! isNowFocused) {
popup.hide();
}
});
popup.show();
});
StackPane root = new StackPane(showPopup);
Scene scene = new Scene(root, 350, 120);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I'm extremely new to JavaFX, and I'm attempting to get a button(specifically scrapeBtn) into the bottom right corner of an application. Here is what I have so far:
package main;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Driver extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Button scrapeBtn = new Button();
scrapeBtn.setText("Scrape!");
scrapeBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("Scrape button pressed.");
}
});
TextField console = new TextField();
GridPane root = new GridPane();
GridPane.setConstraints(scrapeBtn, 2, 2, 1, 1);
root.getChildren().add(scrapeBtn);
root.getChildren().add(console);
Scene scene = new Scene(root, 600, 400);
primaryStage.setTitle("Wiki Scraper");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Any ideas as to how I could accomplish this? Some tips in general to aligning and formatting things with JavaFX would also be really appreciated.
Thanks.
I often use a BorderPane for similar purposes (e.g. a Dialog with some text and controls etc. at the center and one or more buttons at the bottom). Therefore, I use the BorderPane as root and a HBox as "button container" at the bottom. Finally, I set the botton alignment to "RIGHT".
Here an example based on your code:
#Override
public void start(Stage primaryStage) {
// center
VBox vbCenter = new VBox(); // use any container as center pane e.g. VBox
TextField console = new TextField();
vbCenter.getChildren().add(console);
// bottom respectively "button area"
HBox hbButtons = new HBox();
Button scrapeBtn = new Button();
scrapeBtn.setText("Scrape!");
scrapeBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("Scrape button pressed.");
}
});
hbButtons.getChildren().add(scrapeBtn);
hbButtons.setAlignment(Pos.CENTER_RIGHT);
// root
BorderPane root = new BorderPane();
root.setPadding(new Insets(20)); // space between elements and window border
root.setCenter(vbCenter);
root.setBottom(hbButtons);
Scene scene = new Scene(root, 600, 400);
primaryStage.setTitle("Wiki Scraper");
primaryStage.setScene(scene);
primaryStage.show();
}
This code leads to this (after resizing the window a little bit):
You can use two BorderPanes to place a control bottom right
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
BorderPane bottom = new BorderPane();
bottom.setRight(new Button("I am placed bottom right"));
root.setBottom(bottom);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setWidth(400);
primaryStage.setHeight(400);
primaryStage.show();
}