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.
Related
This question already has answers here:
How to load Image file to ImageView?
(3 answers)
Closed 3 years ago.
I am trying to add an image to a gridview using a button that opens up a filechooser that only accepts images. I am getting an exception error when im using the file path from the filechooser to create a setImage to my grid view. I think this is because the path im getting is just not right.
Here is the code where it fails:
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();
parentPath = selectedFile.getParent();
System.out.println("wholePath: " + wholePath);
System.out.println("File Name: " + name);
System.out.println("megaPath: " + megaPath);
Image newAwesomeImage = new Image(megaPath);
ImageView view = new ImageView();
view.setImage(newAwesomeImage);
paneofgridmonkeys.add(view, 0, 0);
//paneofgridmonkeys.add(new Label("Changed the image!"), 0, 1);
createDisplay(primaryStage);
}}});
}
The error message is the title it is saying that the exact problem is line:
view.setImage(newAwesomeImage);
as for my system.out results this is what im getting:
wholePath: M:\Home\BenStillerDuckFace.jpg
File Name: BenStillerDuckFace.jpg
megaPath: M:\Home\BenStillerDuckFace.jpg
ive tried all of these and non work. Any ideas?
The Image(String url) constructor requires a URL string, not a file name. A file name is not a URL.
To convert a file name string to a URL string, do one of these:
// Java 7+
String megaUrl = Paths.get(megaPath).toUri().toURL().toString();
// Java 1.4+
String megaUrl = new File(megaPath).toURI().toURL().toString();
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);
A Set of imagefiles are added to an arraylist(filelist2) of type File.Then an imageview and a button are addded to a vbox,such vboxes are added to a grids of a gripane using a for loop.( number of iterations is equal to size of the filelist2)Once a button is pressed I need to get the corresponding filename of the image within that vbox.
Say I pressed the button contained at (1,1) {i.e row no01 ,col no1} I need to get filename of image at (1,1)
here's a screenshot:
here's my code: FXMLController
File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
for (File file1 : filelist1) {
filelist2.add(file1);
}
btnar = new ArrayList<>();
for (int i = 0; i < filelist2.size(); i++) {
downloadbtn = new Button("Download");
btnar.add(downloadbtn);
final int index=i;
downloadbtn.setId(String.valueOf(index));
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
try {
System.out.println("sssss");
downloadbtn.getId();
//System.out.println(filelist2.get(Integer.valueOf(downloadbtn.getId())).getName());
} catch (Exception ex) {
Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
System.out.println(filelist2.size());
gridpane.setAlignment(Pos.CENTER);
gridpane.setPadding(new Insets(20, 20, 20, 20));
gridpane.setHgap(20);
gridpane.setVgap(20);
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setFillWidth(true);
columnConstraints.setHgrow(Priority.ALWAYS);
gridpane.getColumnConstraints().add(columnConstraints);
int imageCol = 0;
int imageRow = 0;
for (int i = 0; i < filelist2.size(); i++) {
System.out.println(filelist2.get(i).getName());
image = new Image(filelist2.get(i).toURI().toString());
pic = new ImageView();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(image);
vb = new VBox();
vb.getChildren().addAll(pic, (Button) btnar.get(i));
gridpane.add(vb, imageCol, imageRow);
GridPane.setMargin(pic, new Insets(2, 2, 2, 2));
imageCol++;
// To check if all the 3 images of a row are completed
if (imageCol > 2) {
// Reset Column
imageCol = 0;
// Next Row
imageRow++;
}
}
Why not simply
System.out.println(filelist2.get(index).getName());
?
(Actually, it's not really clear to me why you create filelist2 at all. Why not do
btnar = new ArrayList<>();
for (int i=0; i < filelist1.length; i++) {
downloadbtn = new Button("Download");
btnar.add(downloadbtn);
final int index=i;
downloadbtn.setId(String.valueOf(index));
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
try {
System.out.println("sssss");
System.out.println(filelist1[index].getName());
} catch (Exception ex) {
Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
Use setUserData and getUserData to store and retrieve custom values in Nodes ! Set the fileName as the userdata and on click, retrieve it.
downloadbtn.setUserData(filelist2.get(index).getName());
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
System.out.println(downloadbtn.getUserData());
}
Consider using a java.util.HashMap<Button, File> and calling hashMap.get(actionEvent.getSource()).getName() to get the file name.
I've created a DataButton which can hold some typed data (unlike userData which has the type Object).
You can specify a Renderer to render the data on the button or to render an alternative text, eg. in your case: "Download".
Eg. you could use something like this:
List<Path> pathlist2 = new ArrayList<>();
...
// provide language specific text for "Download"
ResourceBundle myResourceBundle = ...;
...
DownloadRenderer downloadRenderer = new DownloadRenderer(myResourceBundle);
...
// the dafault renderer would set the text property to path.toString()
DataButton<Path> downloadbtn = new DataButton<>(downloadRenderer);
downloadbtn.setData(pathlist2.get(index));
downloadbtn.setOnAction((actionEvent) -> {
Path path = downloadbtn.getData();
...
});
...
private static class DownloadRenderer extends AbstractDataRenderer<Object> {
private final ResourceBundle myResourceBundle;
public DownloadRenderer(final ResourceBundle myResourceBundle) {
this.myResourceBundle = myResourceBundle;
}
#Override
public String getText(Object item) {
return myResourceBundle.getString("downloadbtn.text");
}
}
As you can see, you can work directly with Path objects (which should be preferred to the legacy File objects). You don't have to cast or convert the data.
Note: you could also omit the DownloadRenderer and set the text property directly:
downloadbtn.setData(pathlist2.get(index));
downloadbtn.setText(myResourceBundle.getString("downloadbtn.text"));
But then you have to make sure to call setText always after setData.
The library is Open Source and is available from Maven Central:
<dependency>
<groupId>org.drombler.commons</groupId>
<artifactId>drombler-commons-fx-core</artifactId>
<version>0.4</version>
</dependency>
I have a TilePane with a imageView items, each imageview displaying an image got from a directory in my pc. When any of the images in the tilePane is clicked a message is printed onto console with the direcory path address to the folder where the image resides, for example:
You clicked: ImageResources/wp.png
I would like to extend this further so that when a particular image is clicked the folder in which the image resides opens.
My implementation below only prints to console the message below, but no directory/ folder gets opened. The message is:
File Not Found
How can I get it to work so that the folder|directory opens? Thank you all in advance.
Also, of the directory could open with the clicked image selected, then that would be an extra, but is not a priority right now.
My implementation so far goes as follows:
public class TilePaneExample extends Application {
#Override
public void start(Stage primaryStage) {
VBox root = new VBox(30);
String[] imageResources = new String[]{
//loading images
"ImageResources/facebook.png",
"ImageResources/faviicon.png",
"ImageResources/jquery-logo.png",
"ImageResources/linkedin_32.png",
"ImageResources/loading1.png",
"ImageResources/twitter.png",
"ImageResources/twitter_32.png",
"ImageResources/wp.png",};
// Pane
TilePane tilePane = new TilePane();
tilePane.setHgap(5);
tilePane.setVgap(5);
for (final String imageResource : imageResources) {
Image image = new Image(getClass().getResourceAsStream(imageResource));
ImageView imageView = new ImageView(image);
imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
File f = new File(imageResource);
String absolutePath = f.getAbsolutePath();
String folderPath = absolutePath.
substring(0, absolutePath.lastIndexOf(File.separator));
try {
Desktop.getDesktop().open(new File(folderPath));
} catch (IllegalArgumentException iae) {
System.out.println("File Not Found");
} catch (IOException ex) {
Logger.getLogger(TilePaneExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
tilePane.getChildren().add(imageView);
}
root.getChildren().addAll(tilePane);
primaryStage.setTitle("TilePane Example");
Scene scene = new Scene(root, 300, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
For example, clicking the second image, "ImageResources/faviicon.png", prints its folder path as D:\standAloneDev\java\workingDir\Jive\TilePaneExample\ImageResources
I've got an SWT application with a bunch of graphical elements. I'd like for the user to be able to drag an element to their Desktop / Windows Explorer / OS X Finder. When they drop the element, I need the path that they dropped it to, so that I can create a file in that location which represents the element.
I don't think I can use a FileTransfer, because there is no source file. There is a source object which can create a file, but only once it knows where to put it.
Inlined below is a simple example of what I'm trying to achieve, there is a text box with a label to drag from. If the user drags to some folder or file, I'd like to get the path that they dragged to. If they dragged to a file, I'd like to replace the contents of that file with whatever is in the text box. If they dragged to a folder, I'd like to create a file called "TestFile" with the contents of whatever is in the text box.
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class DesktopDragExample {
public static void main(String[] args) {
// put together the SWT main loop
final Display display = Display.getDefault();
display.syncExec(new Runnable() {
#Override
public void run() {
Shell shell = new Shell(display, SWT.SHELL_TRIM);
initializeGui(shell);
//open the shell
shell.open();
//run the event loop
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
});
}
// create the gui
private static void initializeGui(Composite parent) {
GridLayout layout = new GridLayout(2, false);
parent.setLayout(layout);
// make the instructions label
Label infoLbl = new Label(parent, SWT.WRAP);
GridData gd = new GridData();
gd.grabExcessHorizontalSpace = true;
gd.horizontalAlignment = SWT.FILL;
gd.horizontalSpan = 2;
infoLbl.setLayoutData(gd);
infoLbl.setText(
"You should be able to drag to the desktop, Windows Explorer, or OS X Finder.\n" +
"If you drag to a file, it will replace the contents of that file with the contents of the text box.\n" +
"If you drag to a folder, it will create a file named 'TestFile' whose contents are whatever is in the text box.");
// make the text element
final Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
gd = new GridData();
gd.grabExcessHorizontalSpace = true;
gd.horizontalAlignment = SWT.FILL;
text.setLayoutData(gd);
// make the label element
Label label = new Label(parent, SWT.NONE);
label.setText("Drag me");
// listener for drags
DragSourceListener dragListener = new DragSourceListener() {
#Override
public void dragStart(DragSourceEvent e) {
e.detail = DND.DROP_COPY;
}
#Override
public void dragFinished(DragSourceEvent e) {
System.out.println("--dragFinished--");
System.out.println("e.data=" + e.data);
}
#Override
public void dragSetData(DragSourceEvent e) {
System.out.println("--dragSetData--");
System.out.println("e.data=" + e.data);
}
};
// the DragSource
DragSource dragSource = new DragSource(label, DND.DROP_COPY);
dragSource.setTransfer(new Transfer[]{FileTransfer.getInstance()});
dragSource.addDragListener(dragListener);
}
private static void draggedTo(String path, String textBoxContents) {
System.out.println("Dragged the contents '" + textBoxContents + "' to '" + path + "'");
}
}
Here are some other people with the same problem, but looks like no solution so far:
Drag from SWT to Desktop, ..want destination path as String
The only way to do it is by creating a temporary file and then using the FileTransfer. I suspect that's what you'd have to do in native code anyways. I'll see if I have enough time to sketch the sample...
You don't get the file location from and write the file yourself. Dragging to the Desktop implies a FileTransfer (you can check what type of transfer is supported in dragSetData).
This means that SWT expecting a String[] of file paths in DragSourceEvent.data. If you set this in the dragSetData method, then SWT copies those files to your drop target - e.g. the Desktop.
#Override
public void dragSetData(DragSourceEvent e) {
System.out.println("--dragSetData--");
System.out.println("Is supported: " + FileTransfer.getInstance().isSupportedType(e.dataType));
FileTransfer f = FileTransfer.getInstance();
String[] filePaths = {"C:\\CamelOut\\4.xml" } ;
e.data = filePaths;
}
};