public class MainFrame{
JDesktopPane desktop;
File curFile=null;
static JFrame topFrame;
bOpen.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
NewFrame internalFrame=new NewFrame();
desktop.add(internalFrame,new Integer(1));
try{
internalFrame.setVisible(true);
internalFrame.setSelected(true);
}catch(java.beans.PropertyVetoException e2){}
JTextPane textpane=internalFrame.getTextPane();
ReadDocContent rIS=new ReadDocContent(textpane,topFrame,internalFrame,curFile);
curFile=rIS.getFile();
System.out.println(curFile.getName());//java.lang.NullPointerException,curFile是空的 }
});
public class ReadDocContent implements Runnable{
JFrame mainframe;
JTextPane textpane=null;
JInternalFrame innerframe=null;
JFileChooser fileChooser=null;
File myfile=null;
Thread athread;
SimpleAttributeSet attrset=new SimpleAttributeSet();
public ReadDocContent(JTextPane pane, JFrame mf, JInternalFrame current,File file){
this.mainframe=mf;
this.textpane=pane;
this.innerframe=current;
this.myfile=file;
fileChooser=new JFileChooser();
athread=new Thread(this);
athread.start();
}
public File getFile(){
return myfile;
}
@Override
public void run() {
// File file=null;
int result=fileChooser.showOpenDialog(mainframe);
textpane.setText("");
if(result==JFileChooser.APPROVE_OPTION){
myfile=fileChooser.getSelectedFile(); //这句就给myfile赋值了,但为什么传不过去???
innerframe.setTitle("你选择的文件名为: "+myfile.getName());
} else if(result==JFileChooser.CANCEL_OPTION){
innerframe.setTitle("你没有选择任何文件");
}
FileInputStream inputStream;
if(myfile!=null){
try{
inputStream=new FileInputStream(myfile);
}catch(FileNotFoundException fe){
innerframe.setTitle("File Not Found");
return;
}
ProgressMonitorInputStream pmInputStream=new ProgressMonitorInputStream(mainframe,"Get File Content....",inputStream);
ProgressMonitor pMonitor=pmInputStream.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(10);
pMonitor.setMillisToPopup(0);
Document docs=textpane.getDocument();
int readbyte;
try{
while((readbyte=pmInputStream.read())!=-1){
docs.insertString(docs.getLength(), String.valueOf((char)readbyte), attrset);
try{
Thread.sleep(3);
}catch(InterruptedException ie){}
if(pMonitor.isCanceled()){
docs.insertString(docs.getLength(),"\n\n读取文件中断!!", attrset);
}
}
}catch(IOException ioe){
innerframe.setTitle("读取文件错误");
}catch(BadLocationException ble){
System.out.println("BadLocationException: "+ble);
}
finally {
try{
if(pmInputStream!=null)
pmInputStream.close();
}catch(IOException ioe2){
}
}
}
}
}
JDesktopPane desktop;
File curFile=null;
static JFrame topFrame;
bOpen.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
NewFrame internalFrame=new NewFrame();
desktop.add(internalFrame,new Integer(1));
try{
internalFrame.setVisible(true);
internalFrame.setSelected(true);
}catch(java.beans.PropertyVetoException e2){}
JTextPane textpane=internalFrame.getTextPane();
ReadDocContent rIS=new ReadDocContent(textpane,topFrame,internalFrame,curFile);
curFile=rIS.getFile();
System.out.println(curFile.getName());//java.lang.NullPointerException,curFile是空的 }
});
public class ReadDocContent implements Runnable{
JFrame mainframe;
JTextPane textpane=null;
JInternalFrame innerframe=null;
JFileChooser fileChooser=null;
File myfile=null;
Thread athread;
SimpleAttributeSet attrset=new SimpleAttributeSet();
public ReadDocContent(JTextPane pane, JFrame mf, JInternalFrame current,File file){
this.mainframe=mf;
this.textpane=pane;
this.innerframe=current;
this.myfile=file;
fileChooser=new JFileChooser();
athread=new Thread(this);
athread.start();
}
public File getFile(){
return myfile;
}
@Override
public void run() {
// File file=null;
int result=fileChooser.showOpenDialog(mainframe);
textpane.setText("");
if(result==JFileChooser.APPROVE_OPTION){
myfile=fileChooser.getSelectedFile(); //这句就给myfile赋值了,但为什么传不过去???
innerframe.setTitle("你选择的文件名为: "+myfile.getName());
} else if(result==JFileChooser.CANCEL_OPTION){
innerframe.setTitle("你没有选择任何文件");
}
FileInputStream inputStream;
if(myfile!=null){
try{
inputStream=new FileInputStream(myfile);
}catch(FileNotFoundException fe){
innerframe.setTitle("File Not Found");
return;
}
ProgressMonitorInputStream pmInputStream=new ProgressMonitorInputStream(mainframe,"Get File Content....",inputStream);
ProgressMonitor pMonitor=pmInputStream.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(10);
pMonitor.setMillisToPopup(0);
Document docs=textpane.getDocument();
int readbyte;
try{
while((readbyte=pmInputStream.read())!=-1){
docs.insertString(docs.getLength(), String.valueOf((char)readbyte), attrset);
try{
Thread.sleep(3);
}catch(InterruptedException ie){}
if(pMonitor.isCanceled()){
docs.insertString(docs.getLength(),"\n\n读取文件中断!!", attrset);
}
}
}catch(IOException ioe){
innerframe.setTitle("读取文件错误");
}catch(BadLocationException ble){
System.out.println("BadLocationException: "+ble);
}
finally {
try{
if(pmInputStream!=null)
pmInputStream.close();
}catch(IOException ioe2){
}
}
}
}
}
innerframe.setTitle("你选择的文件名为: "+myfile.getName());这个输出了么?
this.mainframe=mf;
this.textpane=pane;
this.innerframe=current;
this.myfile=file;首先执行到这里的时候,传过来的file==null;
this.myfile=file 之后,myfile==null;
然后你myfile=fileChooser.getSelectedFile();这样myfile就有值了,但是file还是null,
这里myfile和file引用的不是同一对象。。
curFile=rIS.getFile();执行这2句后,rIS 里的那个线程的run方法可能还没执行
所以getFile()会返回null其实这里没必要用2个线程的吧,选择文件不要另起线程
System.out.println(myfile.getName());时出现空指针异常,也就是myfile是空的
但是在run()方法中加入System.out.println(myfile.getName());打印出值了,所以是myfile只在run()中有效,run()方法执行完就没值了!
怎么才能把这个值给保存下来呀?