내 응용 프로그램에서 FileChooser를 사용하여 파일을 선택했습니다. 선택한 파일의 이름을 다른 클래스에 반환해야합니다. 이클립스에서 어떻게해야합니까?actionPerformed가 값을 반환 할 수 있습니까?
1
A
답변
0
FileChooser가 사용되는 방법에 대해 FileChooserDemo 및 FileChooserDemo2 here을 살펴보십시오.
public void actionPerformed(ActionEvent e) {
//Set up the file chooser.
if (fc == null) {
fc = new JFileChooser();
//Add a custom file filter and disable the default
//(Accept All) file filter.
fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(false);
//Add custom icons for file types.
fc.setFileView(new ImageFileView());
//Add the preview pane.
fc.setAccessory(new ImagePreview(fc));
}
//Show it.
int returnVal = fc.showDialog(FileChooserDemo2.this,
"Attach");
//Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
log.append("Attaching file: " + file.getName()
+ "." + newline);
} else {
log.append("Attachment cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
//Reset the file chooser for the next time it's shown.
fc.setSelectedFile(null);
}
0
가정 클래스 "A"이 파일 츄와 클래스 "B", 다음은 당신이 필요로 할 것입니다 값을 필요로를 표시하는 코드가 포함되어 있습니다 : 여기
코드의 관련 발췌 한 것입니다. 일부 이벤트 (버튼이 예를 들어 클릭 된) 및이가 직접 호출하지 말아야 할 때class A {
private PropertyChangerSupport changer = new PropertyChangerSupport(this);
private File selectedFile = null;
public void addPropertyChangeListener(String property, PropertyChangeListener listener) {
changer.addPropertyChangeListener(property, listener);
}
public void removePropertyChangeListener(String property, PropertyChangeListener listener) {
changer.removePropertyChangeListener(property, listener);
}
public void actionPerformed(ActionEvent evt) {
// Prompt the user for the file
selectedFile = fc.getSelectedFile();
changer.firePropertyChange(SELECTED_FILE_PROP, null, selectedFile);
}
}
class B {
public B(...) {
// ...
A a = ...
a.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChanged(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(A.SELECTED_FILE_PROP)) {
File selectedFile = (File)evt.getNewValue();
// Do something with selectedFile
}
}});
}
}
3
의 actionPerformed는 이벤트 발송 쓰레드에 의해 호출됩니다. FileChooser를 표시하고 선택한 파일을 반환하는 메서드가 필요한 경우 다른 곳에서뿐만 아니라 eventHandler가 호출 할 수있는 다른 메서드를 선언하십시오.
public void actionPerformed(ActionEvent e) {
File myFile = selectFile();
doSomethingWith(myFile);
}
public File selectFile() {
int returnVal = fc.showDialog(FileChooserDemo2.this,
"Attach");
//Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
} else {
return null;
}
}
관련 문제
- 1. 이벤트 처리기에서 값을 반환 할 수 있습니까?
- 2. Qt 신호가 값을 반환 할 수 있습니까?
- 3. 함수에서 값을 반환 할 수 없습니까?
- 4. DDD : 저장소가 부울 값을 반환 할 수 있습니까?
- 5. Factory 메서드의 반환 값을 올바르게 JsDoc 할 수 있습니까?
- 6. Lua의 string.byte 함수가 음수 값을 반환 할 수 있습니까?
- 7. 내 컨트롤러에서 뷰 대신 값을 반환 할 수 있습니까?
- 8. C# 4에서 method_missing을 구현하고 실제로 값을 반환 할 수 있습니까?
- 9. SharePoint : 다른 목록의 열에서 필드 값을 반환 할 수 있습니까?
- 10. Codeigniter : 동일한 함수에서 여러 값을 반환 할 수 있습니까?
- 11. AS3 함수 : 루프 내에서 값을 반환 할 수 있습니까?
- 12. 함수가 두 개의 값을 반환 할 수 있습니까?
- 13. 모든 키 값을 검색하고 키를 반환 할 수 있습니까?
- 14. 함수가 두 개의 값을 반환 할 수 있습니까?
- 15. 함수가 둘 이상의 값을 반환 할 수 있습니까?
- 16. .bat 파일이 SQL 쿼리를 실행하고 값을 반환 할 수 있습니까?
- 17. Actionperformed가 JComboBox에 대해 트리거되지 않았습니다.
- 18. LotusScript 함수에서 목록을 반환 할 수 있습니까?
- 19. void 함수로 반환 할 수 있습니까?
- 20. 절약 목록에서지도 목록을 반환 할 수 있습니까?
- 21. 메서드에서 StreamReader를 반환 할 수 있습니까?
- 22. Perl이 호출자에게 어떻게 반환 할 수 있습니까?
- 23. C# 메서드가 메서드를 반환 할 수 있습니까?
- 24. ImageMagick에서 이미지 크기를 반환 할 수 있습니까?
- 25. 클래스가 자체 객체를 반환 할 수 있습니까?
- 26. cocos2d Director가 시간을 반환 할 수 있습니까?
- 27. Zend_Auth가 클래스를 클래스로 반환 할 수 있습니까?
- 28. Object.GetType()이 null을 반환 할 수 있습니까?
- 29. NSFetchRequest - 속성 배열을 반환 할 수 있습니까?
- 30. PHP - 메서드에서 포인터를 반환 할 수 있습니까?