JavaFX: FileChooser is blinking - java

It first appears in the left upper corner of the screen and then shows in the middle of the screen.
It is code:
private static File fileChooserDialog( final String initialDirectory, final String initialFileName, final boolean open,
final String filterString, final String... extensions) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(filterString, extensions);
fileChooser.getExtensionFilters().add(extFilter);
Stage stage = new Stage();
File resultFile;
if(open) {
resultFile = fileChooser.showOpenDialog(stage);
} else {
resultFile = fileChooser.showSaveDialog(stage);
}
if(resultFile != null) {
lastSelectedFilePath = resultFile.getParent();
}
return resultFile;
}

You should not create a new Stage every time you want to show a FileChooser. Remove this line:
Stage stage = new Stage();
And use your application's Window as an owner for the FileChooser. For example, if you are trying to show this dialog when the user clicks a button, you can get the Window like this:
Button button = new Button("Browse");
button.setOnAction(event -> {
Window window = button.getScene().getWindow();
fileChooser.showOpenDialog(window);
event.consume();
});

Related

Getting images to show on grid-pane dynamically

My goal is to display images that are selected from a filechooser activated by button, and add those images to my gridpane. I am able to get the right URL file path name from the file chooser in order to make a correct imageview however when doing so my gridpane does not show any images being added..
public void makeBrowseButton(Stage primaryStage) {
//attach handler
browseButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser(); // create object
fileChooser.getExtensionFilters()
.addAll(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif")); //filter for music files
//FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
if (!parentPath.equalsIgnoreCase(
"")) { //go to previous directory if exists
File parentPathFile = new File(parentPath);
fileChooser.setInitialDirectory(parentPathFile);
}
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) { // display the dialog box
String wholePath = selectedFile.getPath();
String name = selectedFile.getName();
String megaPath = selectedFile.getAbsolutePath();
String megaUrl;
try {
megaUrl = Paths.get(megaPath).toUri().toURL().toString();
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
parentPath = selectedFile.getParent();
System.out.println("wholePath: " + wholePath);
System.out.println("parent: " + parentPath);
System.out.println("File Name: " + name);
System.out.println("megaPath: " + megaUrl);
//System.out.println("Canonical: " + Canonical);
Image newAwesomeImage = new Image(megaUrl);
paneofgridmonkeys.add(new ImageView(newAwesomeImage), 0, 0);
//ImageView view = new ImageView();
//view.setImage(newAwesomeImage);
//paneofgridmonkeys.add(view, 1, 1);
//paneofgridmonkeys.setConstraints(view, 0, 4);
//paneofgridmonkeys.add(new Label("Changed the image!"), 0, 1);
createDisplay(primaryStage);
}
}
});
}
I've tried multiple ways of insterting an images and these are the filepaths im getting:
wholePath: \\jupiter\yr1005\Desktop\20190111_1340501.jpg
parent: \\jupiter\yr1005\Desktop
File Name: 20190111_1340501.jpg
megaPath: file://jupiter/yr1005/Desktop/20190111_1340501.jpg
(im using the megapath)
basically when i choose an image from filechooser I get no error but no image is shown after selection. I just get all the print statements in return.. an idea why?
This is my create Display method:
public void createDisplay(Stage primaryStage) {
primaryStage.setTitle(this.MONKEY_TITLE);
GridPane paneofgridmonkeys = new GridPane();
paneofgridmonkeys.setAlignment(Pos.CENTER);
paneofgridmonkeys.setVgap(10);
paneofgridmonkeys.add(browseButton, 10, 10);
ScrollPane allTehFaces = new ScrollPane(paneofgridmonkeys);
allTehFaces.setFitToWidth(true);
primaryStage.setScene(new Scene(allTehFaces, 500, 500));primaryStage.show();
}
}
You're problem is in the createDisplay method; specifically this line:
GridPane paneofgridmonkeys = new GridPane();
Here you're creating a locally-scoped GridPane called paneofgridmonkeys, which must be the name of another class-level variable called paneofgridmonkeys, since it's available to makeBrowseButton. When you do this in local scope, the new instance you've created becomes the one that's used inside of that method, rather than the class-level instance; thus the class-level one isn't the one being added to your scene, and you're not seeing the changes.

JavaFX - Tab will not show nor file contents when file is opened

I'm trying to create a text editor using JavaFX. I want to be able to open a file from a window into the main window as a tab. So far, I've created the necessary menu items and an option window that contains the function to open the file explorer to select a file.
When the user presses the "Choose File" button, the file explorer is opened. When a user selects a file, the "Open File" window closes. The main window (3rd image) is then left but does not contain a tab with the file contents.
Upon execution of the "openFile()" function, no errors are returned, but no tab is opened. I believe it may be an issue with trying to open the tab within the "chooseFileButton.SetOnAction()" function but cannot confirm.
Any suggestions/explanations would be much appreciated.
Open File
Open File (FileChooser)
Output:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primary) throws Exception {
primary.setTitle("Chris' Text Editor");
MenuBar menuBar = new MenuBar();
VBox vbox = new VBox(menuBar);
/* FILE MENU */
MenuItem openFile = new MenuItem("Open...");
fileMenu.getItems().add(openFile);
Pane rootPane = new Pane();
TextArea editorTextArea = new TextArea();
editorTextArea.setMinHeight(1000);
editorTextArea.setMinWidth(1000);
editorTextArea.setVisible(false);
rootPane.getChildren().add(editorTextArea);
TabPane tabPane = new TabPane();
tabPane.setSide(Side.TOP);
openFile.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Label fileLabel = new Label();
fileLabel.setText("No File selected...");
GridPane grid = new GridPane();
Scene contextScene = new Scene(grid, 450, 300);
/* NEW WINDOW */
Stage openFileWindow = new Stage();
openFileWindow.setTitle("Open File");
openFileWindow.setScene(contextScene);
/* SET WINDOW MODAL */
openFileWindow.initModality(Modality.WINDOW_MODAL);
/* SET PARENT WINDOW */
openFileWindow.initOwner(primary);
/* CHOOSE FILE DIRECTORY BUTTON */
openFileWindow.setX(primary.getX() + (primary.getX() / 2));
openFileWindow.setY(primary.getX() + (primary.getX() / 2));
openFileWindow.show();
/* CHOOSE FILE BUTTON */
Button chooseFileButton = new Button();
chooseFileButton.setText("Choose File");
chooseFileButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
FileChooser chooseFile = new FileChooser();
File selectedFile = chooseFile.showOpenDialog(openFileWindow);
if(selectedFile != null) {
String filePath = selectedFile.getPath();
fileLabel.setText(filePath);
String fileContent = openFile2(filePath);
/* CREATE NEW TAB */
Tab newTab = new Tab();
newTab.setContent(editorTextArea);
newTab.setText(filePath);
tabPane.getTabs().add(newTab);
editorTextArea.setVisible(true);
/* POPULATE TEXT AREA WITH FILE CONTENTS */
editorTextArea.appendText(fileContent);
/* FOCUS ON TAB */
SingleSelectionModel<Tab> selection = tabPane.getSelectionModel();
selection.select(newTab);
openFileWindow.close();
}
}
});
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.add(chooseFileButton, 0, 0);
grid.add(fileLabel, 0, 1);
}
});
menuBar.getMenus().add(fileMenu);
Scene scene = new Scene(vbox, 1000, 750);
primary.setScene(scene);
primary.show();
}
public String openFile2(String filePath) {
StringBuilder content = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)){
stream.forEach(s -> content.append(s).append("\n"));
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
You never added your TabPane to the scene:
vbox.getChildren().add(tabPane);

How do I display an image using javaFX by selecting a file from file explorer

1.I have a code that opens a pane and I can open file explorer, but I do not know how to open said image when I select it from file explorer. Also this code is meant to be used in different #Overrride statements but I have only managed to get this far using one statement. Is there a way for me to call some of these events from another Override statement?
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
ImageView myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
EventHandler<ActionEvent> loadEventListener
= t -> {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
ImageView myImageView = new ImageView();
myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
}
};
Since you already have an ImageView and have shown it in the scene, simply set its image to the image you load.
For this you need to make the ImageView an instance variable:
private ImageView myImageView ;
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
// presumably you intended this somewhere?
pane.setCenter(rootBox);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
Note there is no need to load a buffered image and then convert it. So all you need in the event handler is
loadEventListener = t -> {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
if (file != null) {
Image image = new Image(file.toURI().toString());
myImageView.setImage(image);
}
};

Communicating with an FXML Controller that's already open

I have searched again and again on this to no avail. I have a JavaFX FXML window which is connected to a Controller; this window is open. Clicking a button on the window triggers the opening of another FXML file, linked to its respective controller.
The second window (optionsUI.fxml and optionsController) has a few radio buttons. When one is clicked, I want the location of an image/button to change in the mainUI window. How do I go about doing that?
mainController:
public void assetPressed(MouseEvent event) {
//Get the source of Handler
HUDButton button = (HUDButton) event.getSource();
//Check if an asset is already selected
//----do a thing
//Open stage
openStage(currentAsset);
} else {
//if the current asset selected and the new asset clicked are the same
//----do something
closeStage();
}
//if the current asset selected and the new asset clicked are different
else {
//----do something else
assetIsSelected = true;
openStage(currentAsset);
}
}
}
//opening optionsUI.fxml
public void openStage(Asset asset) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("optionsUI.fxml"));
Parent root = null;
try {
root = fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
optionsController controller = fxmlLoader.getController();
Scene scene = new Scene(root, 300, 450);
stage.setScene(scene);
if (alreadyExecuted == false) {
stage.initStyle(StageStyle.UNDECORATED);
stage.initOwner(stageControls); //Making the mainUI the owner of the optionsUI
stage.setTitle("HUDEdit Version 3.0.0");
alreadyExecuted = true;
}
The main issue I am having is adding an event handler on the radio buttons which will change a property of the Button that was pressed (currentButton). I searched on this issue, but what I got was what I have already done: to open a new stage with the new values present in the other FXML file.
You can do something like this in your OptionsController (I am going to rename things to conform to standard naming conventions, btw.)
The basic idea here is just to expose a property representing what the user has selected via the radio buttons.
public class OptionsController {
#FXML
private RadioButton radioButton1 ;
#FXML
private RadioButton radioButton2 ;
private SomeType someValue1 = new SomeType();
private SomeType someValue2 = new SomeType();
private final ReadOnlyObjectWrapper<SomeType> selectedThing = new ReadOnlyObjectWrapper<>();
public ReadOnlyObjectProperty<SomeType> selectedThingProperty() {
return selectedThing.getReadOnlyProperty() ;
}
public final SomeType getSelectedThing() {
return selectedThingProperty().get();
}
public void initialize() {
radioButton1.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
selectedThing.set(someValue1);
}
});
radioButton2.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
selectedThing.set(someValue2);
}
});
}
// ...
}
And now when you load Options.fxml you can just observe that property and do whatever you need when it's value changes:
public void openStage(Asset asset) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("optionsUI.fxml"));
Parent root = null;
try {
root = fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
OptionsController controller = fxmlLoader.getController();
controller.selectedThingProperty().addListener((obs, oldSelection, newSelection) -> {
// do whatever you need with newSelection....
});
// etc...
}

Using a variable outside of an lambda expression block in JavaFX

So, I have a program that loads an image with a FileChooser. I use a lambda expression to define this loading action so the function LoadImage() returns the chosen image inside the event block but I can't use it outside this block.
Can someone help me?
Here is my code so far:
public class Main extends Application{
Stage window;
BorderPane layout;
ImageView imageView;
Image currentImage;
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
window.setTitle("Image Viewer v.1.0");
//MENU
Menu fileMenu = new Menu("_File");
MenuItem openFile = new MenuItem("Open file...");
openFile.setOnAction(e -> currentImage = LoadImage());
fileMenu.getItems().add(openFile);
//Main menu bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
imageView = new ImageView();
imageView.setImage(currentImage); //here the currentImage is null
layout = new BorderPane();
layout.setTop(menuBar);
layout.setCenter(imageView);
Scene scene = new Scene(layout, 900, 600);
window.setScene(scene);
window.show();
}
public Image LoadImage()
{
final Image image;
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
FileChooser.ExtensionFilter extFilterjpg = new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
FileChooser.ExtensionFilter extFilterpng = new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
return new Image(file.toURI().toString());
}
} // end of Main class
So forth, nothing is displayed in the ImageView.
How can I use the value returned by the LoadImage() method?
The value of currentImage is indeed changed, but when you set the value of imageView's image property, it is not yet set. You have to change the property of your ImageView, not the variable (which you use only before setting the image).
Try changing your lambda to:
openFile.setOnAction(e -> imageView.setImage(LoadImage()) )

Categories

Resources