JavaFX 在文件选择器的帮助下获取 Imageview 中图像存储的路径需要帮助 D:
JavaFX getting path of image store in Imageview with the help of filechooser need help D:
我的问题是我需要帮助从 fxml 文件中的图像视图中的图像获取路径,因为我需要将图像的路径存储在对象中。我正在使用文件选择器将图像输入到 imageView 中。我计划将该路径存储到一个对象中,并在以后声明它,就像个人资料页面会保留个人资料图片一样。我知道它有点简短,但我希望如果你们能以任何方式帮助我,我将不胜感激。
所以根据我编辑的评论:
我的问题是尽管我使用了文件选择器,但我无法声明图像路径的 URL。 实际上我已经删除了所有关于声明 URL 的相关部分我做的,因为我不能认领它。
package orgRegistrationtest;
public class OrgRegistrationController {
@FXML
private JFXHamburger hamburger;
@FXML
private JFXTextField email;
@FXML
private JFXCheckBox health;
@FXML
private JFXCheckBox animal;
@FXML
private JFXCheckBox children;
@FXML
private JFXCheckBox elderly;
@FXML
private JFXCheckBox environment;
@FXML
private JFXCheckBox others;
@FXML
private JFXTextField name;
@FXML
private JFXTextField contact;
@FXML
private JFXTextField address;
@FXML
private JFXPasswordField password;
@FXML
private JFXPasswordField password2;
@FXML
private JFXTextArea desc;
//pic not added
@FXML
private ImageView pic;
@FXML
private JFXButton upload;
@FXML
private JFXButton clear;
@FXML
private JFXButton confirm;
@FXML
private JFXDrawer drawer;
@FXML
void handleClear(ActionEvent event) {
}
private String type=null;
@FXML
private void handleConfirmButtonAction(ActionEvent event) throws IOException {
if(health.isSelected())
type=health.getText();
if(animal.isSelected()){
type+=";"+animal.getText();
}
if(children.isSelected()){
type+=";"+children.getText();
}
if(elderly.isSelected()){
type+=";"+elderly.getText();
}
if(environment.isSelected()){
type+=";"+environment.getText();
}
if(others.isSelected()){
type+=";"+others.getText();
}
String Email=email.getText();
String Password = password.getText();
String Password2=password2.getText();
String Name = name.getText();
String Contact= contact.getText();
String Address=address.getText();
String Type = ""+type;
String Desc = desc.getText();
//String url = pic.getImage()._____
//.getURL(); i know getURL is not inside same for getPath();
// So how am i able to claim the Path of the Image and store it?
OrgData table = new OrgData(Email,Password,Password2,Name, Contact,Address,Type,Desc,url);
OrgDataDAO dao = new OrgDataDAO();
dao.createFriend(table);
}
@FXML
public void handleUpload(ActionEvent t) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
pic.setImage(image);
} catch (IOException ex) {
System.out.println(ex);
}
}
private Main Main;
public Main getMainApp() {
return Main;
}
// setter for mainApp
public void setMainApp(Main testmainApp) {
this.Main = testmainApp;
}
这是我拥有的模型 我从来没有为 URL 输入一个字符串,但因为我无法声明它,可能有更好的方法来代替使用路径,尽管我确实尝试过,但它什么也没给我。
public class OrgData implements Serializable {
private String email, pw, pw2, name, contact, address, type, desc,url;
final static ObservableList<OrgData> data = FXCollections.observableArrayList();
public OrgData(String email, String pw, String pw2, String name, String contact, String address, String type,
String desc,String url) {
super();
this.email = email;
this.pw = pw;
this.pw2 = pw2;
this.name = name;
this.contact = contact;
this.address = address;
this.type = type;
this.desc = desc;
//this.url = url;
}
//public String getUrl() {
// return url;
//}
//public void setUrl(String url) {
// this.url= url;
//}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getPw2() {
return pw2;
}
public void setPw2(String pw2) {
this.pw2 = pw2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getType() {
return type;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public static ObservableList<OrgData> getData() {
return data;
}
public String toString(){
return "["+email+","+pw+","+name+","+contact+","+address+","+type+","+desc+","+url+"]";
}
所以这是 DAO,在用户单击注册按钮并存储在 OrgRegData.dat 文件后,我在最后保存文件详细信息。
所以就像我说的,我的问题是我无法从我从文件选择器中选择的 imageView 图像获取路径并将其存储到我的模型中,即使是字符串,因为没有 getPath() 或 getURL();点击注册后排序方法。我没有使用数据库或任何仅使用文件和文本文件来存储数据并从文件中读取数据的东西,所以我希望现在我的问题更清楚
public class OrgDataDAO {
private static final String Friend_File="OrgRegData.dat";
private File dataFile;
public OrgDataDAO() {
Path dPath = FileSystems.getDefault().getPath("resources/data/",Friend_File);
dataFile=new File(dPath.toString());
}
public ObservableList<OrgData> getAllFriends() {
Scanner in;
String record=null;
String[] fields;
ImageView[] images;
ObservableList<OrgData> friends=FXCollections.observableArrayList();;
try {
in=new Scanner(dataFile);
while (in.hasNextLine()) {
record=in.nextLine();
fields=record.split(",");
String email=fields[0];
String password=fields[1];
String password2=fields[2];
String name=fields[3];
String contact = fields[4];
String address=fields[5];
String type=fields[6];
String desc=fields[7];
//String url = fields[8];
OrgData f=new OrgData(email,password,password2,name,contact,address,type,desc,url);
friends.add(f);
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("No record found!");
//e.printStackTrace();
}
return friends;
}
/* assuming friend has a unique name, otherwise may return a list */
public OrgData getEvent(String email) {
// TODO Auto-generated method stub
ObservableList<OrgData> event=getAllFriends();
OrgData Event=null;
for (OrgData f:event) {
if (f.getEmail().equals(email)){
Event=f;
break;
}
}
return Event;
}
public void updateEvent(OrgData Event) {
ObservableList<OrgData> Events=getAllFriends();
for (int i=0; i<Events.size(); i++) {
OrgData f=Events.get(i);
if (f.getEmail().equals(Event.getEmail())){
Events.set(i, Event);
}
}
synToFile(Events);
}
public void deleteEvent(OrgData Event) {
ObservableList<OrgData> Events=getAllFriends();
OrgData delEvent=null;
for (OrgData f:Events) {
if (Event.getEmail().equals(f.getEmail())){
delEvent=f;
break;
}
}
if (delEvent!=null){
Events.remove(delEvent);
synToFile(Events);
}
}
public boolean createFriend(OrgData Event) {
boolean existing=false;
ObservableList<OrgData> Events=getAllFriends();
for (OrgData f:Events) {
if (f.getEmail().equals(Event.getEmail())){
existing=true;
break;
}
}
if (!existing) {
Events.add(Event);
synToFile(Events);
}
return !existing;
}
private void synToFile(ObservableList<OrgData> EventList) {
if (EventList==null)
return;
try {
FileWriter out = new FileWriter(dataFile);
for (OrgData f: EventList) {
out.append(f.toString()+"\n");
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
您需要重新考虑将图像存储为 Image
对象。
Image
对象不包含任何关于路径的信息(正如您已经发现的那样),因为您可以从任何 InputStream
加载它并且可能没有办法使用 URL
访问 InputStream
。 (例如,图像可以作为 blob 存储在数据库中,需要查询才能检索它...)
打开文件时将 url 存储在附加字段中。
此外,我建议使用 Image
class 直接加载图像,否则您会在内存中创建一个 BufferedImage
和一个 Image
对象,这大约需要 2 分钟。仅其中一个对象所需内存的两倍; BufferedImage
在方法 returns 之后立即可用于垃圾回收...
private String imageUrl;
@FXML
private void handleUpload(ActionEvent t) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
if (file != null) {
try {
imageUrl = file.toURI().toURL().toExternalForm();
Image image = new Image(imageUrl);
pic.setImage(image);
} catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
}
}
@FXML
private void handleConfirmButtonAction(ActionEvent event) throws IOException {
...
String url = imageUrl;
...
}
请注意,如果您在 fxml 中添加默认图像,则需要使用此 url 初始化 imageUrl
。
基本的文件选择器函数是
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(null);
String imgAddress3;
if (file != null) {
try {
imgAddress3 = file.toURI().toURL().toExternalForm();
Image image = new Image(imgAddress3);
imageId.setImage(image);
System.out.println(imgAddress3);
} catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
}
为了澄清,你可以看到这个完整的项目。
https://github.com/ashrafulais/java_projects/tree/master/onCLickImageLoader
我的问题是我需要帮助从 fxml 文件中的图像视图中的图像获取路径,因为我需要将图像的路径存储在对象中。我正在使用文件选择器将图像输入到 imageView 中。我计划将该路径存储到一个对象中,并在以后声明它,就像个人资料页面会保留个人资料图片一样。我知道它有点简短,但我希望如果你们能以任何方式帮助我,我将不胜感激。
所以根据我编辑的评论: 我的问题是尽管我使用了文件选择器,但我无法声明图像路径的 URL。 实际上我已经删除了所有关于声明 URL 的相关部分我做的,因为我不能认领它。
package orgRegistrationtest;
public class OrgRegistrationController {
@FXML
private JFXHamburger hamburger;
@FXML
private JFXTextField email;
@FXML
private JFXCheckBox health;
@FXML
private JFXCheckBox animal;
@FXML
private JFXCheckBox children;
@FXML
private JFXCheckBox elderly;
@FXML
private JFXCheckBox environment;
@FXML
private JFXCheckBox others;
@FXML
private JFXTextField name;
@FXML
private JFXTextField contact;
@FXML
private JFXTextField address;
@FXML
private JFXPasswordField password;
@FXML
private JFXPasswordField password2;
@FXML
private JFXTextArea desc;
//pic not added
@FXML
private ImageView pic;
@FXML
private JFXButton upload;
@FXML
private JFXButton clear;
@FXML
private JFXButton confirm;
@FXML
private JFXDrawer drawer;
@FXML
void handleClear(ActionEvent event) {
}
private String type=null;
@FXML
private void handleConfirmButtonAction(ActionEvent event) throws IOException {
if(health.isSelected())
type=health.getText();
if(animal.isSelected()){
type+=";"+animal.getText();
}
if(children.isSelected()){
type+=";"+children.getText();
}
if(elderly.isSelected()){
type+=";"+elderly.getText();
}
if(environment.isSelected()){
type+=";"+environment.getText();
}
if(others.isSelected()){
type+=";"+others.getText();
}
String Email=email.getText();
String Password = password.getText();
String Password2=password2.getText();
String Name = name.getText();
String Contact= contact.getText();
String Address=address.getText();
String Type = ""+type;
String Desc = desc.getText();
//String url = pic.getImage()._____
//.getURL(); i know getURL is not inside same for getPath();
// So how am i able to claim the Path of the Image and store it?
OrgData table = new OrgData(Email,Password,Password2,Name, Contact,Address,Type,Desc,url);
OrgDataDAO dao = new OrgDataDAO();
dao.createFriend(table);
}
@FXML
public void handleUpload(ActionEvent t) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
pic.setImage(image);
} catch (IOException ex) {
System.out.println(ex);
}
}
private Main Main;
public Main getMainApp() {
return Main;
}
// setter for mainApp
public void setMainApp(Main testmainApp) {
this.Main = testmainApp;
}
这是我拥有的模型 我从来没有为 URL 输入一个字符串,但因为我无法声明它,可能有更好的方法来代替使用路径,尽管我确实尝试过,但它什么也没给我。
public class OrgData implements Serializable {
private String email, pw, pw2, name, contact, address, type, desc,url;
final static ObservableList<OrgData> data = FXCollections.observableArrayList();
public OrgData(String email, String pw, String pw2, String name, String contact, String address, String type,
String desc,String url) {
super();
this.email = email;
this.pw = pw;
this.pw2 = pw2;
this.name = name;
this.contact = contact;
this.address = address;
this.type = type;
this.desc = desc;
//this.url = url;
}
//public String getUrl() {
// return url;
//}
//public void setUrl(String url) {
// this.url= url;
//}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getPw2() {
return pw2;
}
public void setPw2(String pw2) {
this.pw2 = pw2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getType() {
return type;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public static ObservableList<OrgData> getData() {
return data;
}
public String toString(){
return "["+email+","+pw+","+name+","+contact+","+address+","+type+","+desc+","+url+"]";
}
所以这是 DAO,在用户单击注册按钮并存储在 OrgRegData.dat 文件后,我在最后保存文件详细信息。 所以就像我说的,我的问题是我无法从我从文件选择器中选择的 imageView 图像获取路径并将其存储到我的模型中,即使是字符串,因为没有 getPath() 或 getURL();点击注册后排序方法。我没有使用数据库或任何仅使用文件和文本文件来存储数据并从文件中读取数据的东西,所以我希望现在我的问题更清楚
public class OrgDataDAO {
private static final String Friend_File="OrgRegData.dat";
private File dataFile;
public OrgDataDAO() {
Path dPath = FileSystems.getDefault().getPath("resources/data/",Friend_File);
dataFile=new File(dPath.toString());
}
public ObservableList<OrgData> getAllFriends() {
Scanner in;
String record=null;
String[] fields;
ImageView[] images;
ObservableList<OrgData> friends=FXCollections.observableArrayList();;
try {
in=new Scanner(dataFile);
while (in.hasNextLine()) {
record=in.nextLine();
fields=record.split(",");
String email=fields[0];
String password=fields[1];
String password2=fields[2];
String name=fields[3];
String contact = fields[4];
String address=fields[5];
String type=fields[6];
String desc=fields[7];
//String url = fields[8];
OrgData f=new OrgData(email,password,password2,name,contact,address,type,desc,url);
friends.add(f);
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("No record found!");
//e.printStackTrace();
}
return friends;
}
/* assuming friend has a unique name, otherwise may return a list */
public OrgData getEvent(String email) {
// TODO Auto-generated method stub
ObservableList<OrgData> event=getAllFriends();
OrgData Event=null;
for (OrgData f:event) {
if (f.getEmail().equals(email)){
Event=f;
break;
}
}
return Event;
}
public void updateEvent(OrgData Event) {
ObservableList<OrgData> Events=getAllFriends();
for (int i=0; i<Events.size(); i++) {
OrgData f=Events.get(i);
if (f.getEmail().equals(Event.getEmail())){
Events.set(i, Event);
}
}
synToFile(Events);
}
public void deleteEvent(OrgData Event) {
ObservableList<OrgData> Events=getAllFriends();
OrgData delEvent=null;
for (OrgData f:Events) {
if (Event.getEmail().equals(f.getEmail())){
delEvent=f;
break;
}
}
if (delEvent!=null){
Events.remove(delEvent);
synToFile(Events);
}
}
public boolean createFriend(OrgData Event) {
boolean existing=false;
ObservableList<OrgData> Events=getAllFriends();
for (OrgData f:Events) {
if (f.getEmail().equals(Event.getEmail())){
existing=true;
break;
}
}
if (!existing) {
Events.add(Event);
synToFile(Events);
}
return !existing;
}
private void synToFile(ObservableList<OrgData> EventList) {
if (EventList==null)
return;
try {
FileWriter out = new FileWriter(dataFile);
for (OrgData f: EventList) {
out.append(f.toString()+"\n");
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
您需要重新考虑将图像存储为 Image
对象。
Image
对象不包含任何关于路径的信息(正如您已经发现的那样),因为您可以从任何 InputStream
加载它并且可能没有办法使用 URL
访问 InputStream
。 (例如,图像可以作为 blob 存储在数据库中,需要查询才能检索它...)
打开文件时将 url 存储在附加字段中。
此外,我建议使用 Image
class 直接加载图像,否则您会在内存中创建一个 BufferedImage
和一个 Image
对象,这大约需要 2 分钟。仅其中一个对象所需内存的两倍; BufferedImage
在方法 returns 之后立即可用于垃圾回收...
private String imageUrl;
@FXML
private void handleUpload(ActionEvent t) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
if (file != null) {
try {
imageUrl = file.toURI().toURL().toExternalForm();
Image image = new Image(imageUrl);
pic.setImage(image);
} catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
}
}
@FXML
private void handleConfirmButtonAction(ActionEvent event) throws IOException {
...
String url = imageUrl;
...
}
请注意,如果您在 fxml 中添加默认图像,则需要使用此 url 初始化 imageUrl
。
基本的文件选择器函数是
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(null);
String imgAddress3;
if (file != null) {
try {
imgAddress3 = file.toURI().toURL().toExternalForm();
Image image = new Image(imgAddress3);
imageId.setImage(image);
System.out.println(imgAddress3);
} catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
}
为了澄清,你可以看到这个完整的项目。 https://github.com/ashrafulais/java_projects/tree/master/onCLickImageLoader