我的問題是,我需要幫助從fxml文件中的imageview中的圖像獲取路徑,因爲我需要存儲圖像的路徑在一個對象中。我正在使用filechooser將圖像輸入到imageView中。我打算將該路徑存儲到一個對象中,並在稍後的時間將其聲明出來,就像個人資料頁面會保留個人資料圖片一樣。我知道這是簡短的但我希望如果你們有任何幫助我的方式,將不勝感激。JavaFX獲得Imageview中的路徑在FileView的幫助下,需要幫助D:
因此,根據我編輯的意見: 我的問題是我不能聲稱的圖像路徑的URL,雖然我使用filechooser。我實際上已經刪除了所有相關的聲明我所提供的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+"]";
}
因此,這是我在最後保存文件的詳細信息的用戶之後點擊註冊按鈕,並存儲在OrgRegData.dat文件中的DAO。 所以就像我說的,我的問題是,我無法從我從filechooser中選擇的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();
}
}
那麼你有什麼樣的困難 - 請編輯問題並添加相關的代碼。 –
當你從FileChooser獲得文件時,你知道路徑,所以問題出在哪裏? – fabian
編輯這個問題,如果有需要顯示fxml如何看起來像我會稍後放出的物理視圖 – Devilsbats