2017-01-02 84 views
-1

我的問題是,我需要幫助從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(); 
    } 

} 
+1

那麼你有什麼樣的困難 - 請編輯問題並添加相關的代碼。 –

+0

當你從FileChooser獲得文件時,你知道路徑,所以問題出在哪裏? – fabian

+0

編輯這個問題,如果有需要顯示fxml如何看起來像我會稍後放出的物理視圖 – Devilsbats

回答

1

你需要重新考慮存儲圖像就像Image對象一樣。

Image對象不包含關於路徑的任何信息(如你已經發現了),因爲你可以從任何InputStream加載它,有可能不使用URL訪問InputStream的方式。 (例如,圖像可以作爲blob存儲在數據庫中進行查詢以獲取它)...

當您打開文件時,將url存儲在其他字段中。

此外,我建議使用Image類直接加載圖像,否則你會在內存中創建一個BufferedImage和一個Image對象,只有其中一個對象所需內存的兩倍;該BufferedImage可用於垃圾收集反正該方法返回之後立即...

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

+0

嗨,所以我已經試過了你給我的答案,我管理使它適用於URL(字符串)但我正在嘗試使用圖像對象的建議,我能夠更改模型和控制器中的相關部件,但問題與DAO有關。我怎樣才能改變它能夠存儲它並收集圖像。 – Devilsbats

+0

@Devilsbats你需要將圖像數據包含在文件中嗎?然後,一個文本文件將不太適合(但它可以通過將圖像數據編碼爲字符串來完成;儘管將文件存儲在檔案中可能會更好)。否則,該文件可以從存儲在該文件中的URL構建... – fabian

+0

沒關係,我想我會在閱讀大量示例後混淆你的意思。我會再試一次。 – Devilsbats