I'm working in a desktop application using javafx.
This is my code. The method telechargerImage() is called when the user clicks on the button uploadimage in order to choose an image and then I want to save it in localhost/images.
Excuse my english.
private Image imagelog;
#FXML
private ImageView image;
#FXML
private JFXButton uploadimage;
private FileChooser fileChooser = new FileChooser();
private File file = null;
void telechargerImage(ActionEvent event) {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
imagelog = new Image(file.toURI().toString());
image.setImage(imagelog);
}
}
Related
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();
});
The picture here is where the picture will be set.
private void btnOpenPic1ActionPerformed(java.awt.event.ActionEvent evt)
{
int pic1 = jFileChooser1.showOpenDialog(this);
if (pic1 == jFileChooser1.APPROVE_OPTION)
{
File f = jFileChooser1.getSelectedFile();
btnPic1.setIcon(new ImageIcon(""+ f));
btnPic1.setText("");
}
}
now after setting the picture. How will i be able to save that picture into the project java so that next time it loads, it will still appear there.
private void btnSavePic1ActionPerformed(java.awt.event.ActionEvent evt)
{
}
In my code I am asking the user to choose an image. My JFileChooser window worked fine. Then I restarted my computer and now whenever that window comes up it is not clickable in any way. I can't open a file, I can't cancel, I can't chooser folders or files. Here is the necessary code.
JFileChooser jfc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg");
jfc.setFileFilter(filter);
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setVisible(true);
int ret = jfc.showOpenDialog(null);
if (ret == JFileChooser.CANCEL_OPTION) {
return;
}
File file_1 = jfc.getSelectedFile();
file_path = file_1.getAbsolutePath();
Breakpoints show that the program never leaves this line:
int ret = jfc.showOpenDialog(null);
As I said the same exact code was working fine moments ago. Not sure what is causing this situation.
In my main program I click "Add image" which calls the previously mentioned code. I try clicking on the window "Open" opened by "showOpenDialog" but it doesn't matter where I click. Nothing changes. The main program resumes once I close the "Open" window from my task manager. Also in my task manager "Open" window doesn't say not responding, it looks fine and closes at an instance and furthermore inside the text field the text cursor is blinking.
EDIT: Same exact code works on a separate project that consists of only this code.
EDIT 2: Some additional codes.
Here is the complete load_file() function.
public class Image {
private static String file_path;
private static ImageFrame frame;
public static boolean isImgLoaded = false;
public static void load_file(){
JFileChooser jfc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg");
jfc.setFileFilter(filter);
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setVisible(true);
int ret = jfc.showOpenDialog(null);
if (ret == JFileChooser.CANCEL_OPTION) {
return;
}
File file_1 = jfc.getSelectedFile();
file_path = file_1.getAbsolutePath();
ImageFrame frm = new ImageFrame();
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frm.setVisible(true);
frame = frm;
isImgLoaded = frame.get_component().is_img_loaded();
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
closeFrame();
}
});
}
Here is the button code that calls the function when pressed. shlErgo is my shell that the UI is built on.
Button btnAddImage = new Button(shlErgo, SWT.NONE);
btnAddImage.setBounds(230, 10, 75, 25);
btnAddImage.setText("Add Image");
btnAddImage.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
if (!Image.isImgLoaded){
Image.load_file();
}
else{
Error.translate(2);
}
}
});
I'm creating a program to register employees, and was wondering how to put a picture of the employees registered When do your record. I would use a button to select this file and attach the photo along with your registration? But how would this be done using JavaFX? Thank you!!
I managed as follows:
#FXML
private void searchPicture(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
File file = fileChooser.showOpenDialog(new Stage());
if (file != null) {
Image img = new Image(file.toURI().toString());
imagem.setImage(img);
imagem.setFitWidth(171);
imagem.setFitHeight(176);
imagem.setPreserveRatio(false);
}
}
}
My problem is that all the examples of using FileChooser requires you to pass in a stage. Only problem is that my UI is defined in an fxml file, which uses a controller class separate from the main stage.
#FXML protected void locateFile(ActionEvent event) {
FileChooser chooser = new FileChooser();
chooser.setTitle("Open File");
chooser.showOpenDialog(???);
}
What do I put at the ??? to make it work? Like I said, I don't have any references to any stages in the controller class, so what do I do?
For any node in your scene (for example, the root node; but any node you have injected with #FXML will do), do
chooser.showOpenDialog(node.getScene().getWindow());
You don't have to stick with the Stage created in the Application you can either:
#FXML protected void locateFile(ActionEvent event) {
FileChooser chooser = new FileChooser();
chooser.setTitle("Open File");
File file = chooser.showOpenDialog(new Stage());
}
Or if you want to keep using the same stage then you have to pass the stage to the controller before:
FXMLLoader loader = new FXMLLoader(getClass().getResource("yourFXMLDocument.fxml"));
Parent root = (Parent)loader.load();
MyController myController = loader.getController();
myController.setStage(stage);
and you will have the main stage of the Application there to be used as you please.
From a menu item
public class SerialDecoderController implements Initializable {
#FXML
private MenuItem fileOpen;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void fileOpen (ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(fileOpen.getParentPopup().getScene().getWindow());
}
Alternatively, what worked for me: simply put null.
#FXML
private void onClick(ActionEvent event) {
File file = fileChooser.showOpenDialog(null);
if (file != null) {
//TODO
}
}