Open Image From FileChooser On a Button - javafx

enter image description hereI need help I want to open a Image with a button of browse using filechooser on javafx ? how can I do it?
FileChooser f;
File file;
Image img;
ImageView mv;
#Override
public void start(Stage primaryStage)
{
f = new FileChooser();
Button browse = new Button("Browse");
browse.setOnAction((event) ->
{
file = f.showOpenDialog(primaryStage);
img = new Image(file.toURI().toString());
mv = new ImageView(img);
});
mv.setImage(img);
BorderPane root = new BorderPane();
root.setTop(browse);
root.setCenter(mv);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

The start method sets up the scene and shows the window. It completes before the button handler is run and the information you get during it's execution is not available at this time. You need to do modify the scene with the image from the event handler for this reason:
#Override
public void start(Stage primaryStage) {
final FileChooser f = new FileChooser();
final ImageView mv = new ImageView(); // create empty ImageView
Button browse = new Button("Browse");
browse.setOnAction((event) -> {
File file = f.showOpenDialog(primaryStage);
if (file != null) { // only proceed, if file was chosen
Image img = new Image(file.toURI().toString());
mv.setImage(img);
}
});
BorderPane root = new BorderPane();
root.setTop(browse);
root.setCenter(mv);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
Alternatively you could create the ImageView in the event handler and place it in the scene:
#Override
public void start(Stage primaryStage) {
final FileChooser f = new FileChooser();
Button browse = new Button("Browse");
final BorderPane root = new BorderPane();
browse.setOnAction((event) -> {
File file = f.showOpenDialog(primaryStage);
if (file != null) { // only proceed, if file was chosen
Image img = new Image(file.toURI().toString());
ImageView mv = new ImageView(img);
root.setCenter(mv); // add ImageView to scene
}
});
root.setTop(browse);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

Related

JavaFX - creating a new window on button clicked(using Scene Builder) [duplicate]

Looking at this code they show a way to display a new window after a login. When username and password are correct it opens new dialog. I want a button click to open new dialog, without checking for username and password.
If you just want a button to open up a new window, then something like this works:
btnOpenNewWindow.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Parent root;
try {
root = FXMLLoader.load(getClass().getClassLoader().getResource("path/to/other/view.fxml"), resources);
Stage stage = new Stage();
stage.setTitle("My New Stage Title");
stage.setScene(new Scene(root, 450, 450));
stage.show();
// Hide this current window (if this is what you want)
((Node)(event.getSource())).getScene().getWindow().hide();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
I use the following method in my JavaFX applications.
newWindowButton.setOnMouseClicked((event) -> {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("NewWindow.fxml"));
/*
* if "fx:controller" is not set in fxml
* fxmlLoader.setController(NewWindowController);
*/
Scene scene = new Scene(fxmlLoader.load(), 600, 400);
Stage stage = new Stage();
stage.setTitle("New Window");
stage.setScene(scene);
stage.show();
} catch (IOException e) {
Logger logger = Logger.getLogger(getClass().getName());
logger.log(Level.SEVERE, "Failed to create new Window.", e);
}
});
The code below worked for me I used part of the code above inside the button class.
public Button signupB;
public void handleButtonClick (){
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("sceneNotAvailable.fxml"));
/*
* if "fx:controller" is not set in fxml
* fxmlLoader.setController(NewWindowController);
*/
Scene scene = new Scene(fxmlLoader.load(), 630, 400);
Stage stage = new Stage();
stage.setTitle("New Window");
stage.setScene(scene);
stage.show();
} catch (IOException e) {
Logger logger = Logger.getLogger(getClass().getName());
logger.log(Level.SEVERE, "Failed to create new Window.", e);
}
}
}

javafx TabPane show search field in the default dropddown showing hidden tabs

How to set search field for the default drop down on the tab pane in JavaFX ,which shows hidden tab list. and here is my code for tabs on the Tabpane
public void start(Stage primaryStage) {
primaryStage.setTitle("Tabs");
Group root = new Group();
Scene scene = new Scene(root, 400, 250, Color.WHITE);
TabPane tabPane = new TabPane();
BorderPane borderPane = new BorderPane();
for (int i = 0; i < 10; i++) {
Tab tab = new Tab();
tab.setText("Tab" + i);
HBox hbox = new HBox();
hbox.getChildren().add(new Label("Tab" + i));
hbox.setAlignment(Pos.CENTER);
tab.setContent(hbox);
tabPane.getTabs().add(tab);
}
// bind to take available space
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
borderPane.setCenter(tabPane);
root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}

How can the text of the label be shown?

As you can see in the picture, the label in the third box, next to the ComboBox, has the text '...' instead of 'fase'.
I have previously attempted to increase the size of the HBox in which it resides, increasing spacing, but to no avail. Any suggestions?
public void start(Stage primaryStage) {
RadioButtonCustom inhoud = new RadioButtonCustom();
Fieldset fs = new Fieldset("eerste", inhoud);
RadioButtonCustom inhoud2 = new RadioButtonCustom();
Fieldset fs2 = new Fieldset("tweede", inhoud2);
//error
HBox hb = new HBox();
hb.minWidth(200);
Label fase = new Label("fase");
fase.setText("Fase");
fase.minWidth(500);
fase.getStyleClass().add("bordered-titled-title");
StackPane.setAlignment(fase, Pos.CENTER);
ComboBox<Integer> cb = new ComboBox<>();
hb.setSpacing(40);
for (Integer i : array) {
cb.getItems().add(i);
}
hb.getChildren().addAll(fase,cb);
Fieldset fs3 = new Fieldset("fase",hb);
// einde error
VBox vb = new VBox();
vb.getChildren().addAll(fs, fs2,fs3);
vb.setSpacing(20);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
BorderPane root = new BorderPane();
root.getChildren().add(vb);
Scene scene = new Scene(root, 500, 500);
scene.getStylesheets().add("/lissa/stijl.css");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

Add navigation input filed into JavaFX web browser

I created this very simple example of JavaFX web browser.
StackPane secondaryLayout = new StackPane();
Scene secondScene = new Scene(secondaryLayout, 200, 100);
Stage secondStage = new Stage();
secondStage.setTitle("Second Stage");
secondStage.setScene(secondScene);
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
String url = "http://zoranpavlovic.blogspot.com/";
engine.load(url);
StackPane sp = new StackPane();
sp.getChildren().add(browser);
Scene root = new Scene(sp, 600, 600);
secondStage.setScene(root);
secondStage.show();
I would like to add input filed into the to size of the window and button "Go". Can you help me to implement this?
You can have a HBox with a textfield and a button and on button's action you can load the webengine.
I wrote the code using notepad, so errors may crept in
StackPane secondaryLayout = new StackPane();
Scene secondScene = new Scene(secondaryLayout, 200, 100);
Stage secondStage = new Stage();
secondStage.setTitle("Second Stage");
secondStage.setScene(secondScene);
HBox box = new HBox();
TextField textField = new TextField();
Button go = new Button();
box.getChildren.addAll(textField, go);
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
go.setOnAction(new Eventhandler<ActionEvent>(){
#Override public void handle(ActionEvent e) {
String url = textField.getText();
engine.load(url);
}
});
BorderPane sp = new BorderPane();
sp.setTop(box);
sp.setCenter(browser);
Scene root = new Scene(sp, 600, 600);
secondStage.setScene(root);
secondStage.show();

JavaFX how use Image and ImageView to open and image from my system

I am trying to display an image using Image and ImageView classes of JavaFX and it does not work.
try {
String workingDir = System.getProperty("user.dir");
final File f = new File(workingDir, "src/chk.hrn.nye.movieplayer/img.png");
// System.out.println(workingDir);
// File file = new File(loadImage.class.getResource("img1.jpg").toString());
Image img = new Image(f.toURI().toString());
ImageView imv = new ImageView(img);
Group root = new Group();
root.getChildren().add(imv);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
System.out.println(e);
}

Resources