2014-09-26 90 views
-2

我使用JFileChooser打開了一個文件。如果我打開一個文件,它會首次打開,如果我第二次打開同一個文件,它也會在新選項卡中打開。我只想打開一個文件一次。如果我們打開一個文件,當用戶打開相同的文件時不會再次打開

這裏是我的代碼,

final JFileChooser jc = new JFileChooser(); 
    int returnVal= jc.showOpenDialog(Open.this); 
     String title; 
     String sts; 
     File file=null; 
      if(returnVal == JFileChooser.APPROVE_OPTION)  
     file = jc.getSelectedFile();  

    JTextArea text = new JTextArea(); 
if (jc.getSelectedFile()!= null) { 

    tx = new JTextArea(); 
    BufferedReader br = null; 
    StringBuffer str = new StringBuffer(""); 
    StringBuffer st = new StringBuffer(""); 



    try { 
     br = new BufferedReader(new FileReader(file)); 
     String line; 

       while ((line = br.readLine()) != null) { 
       str.append(line + "\n"); 
      } 
     } 
     catch (IOException ex) { 
       Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex); 
     } 


     String t = str.toString(); 
    final JInternalFrame internalFrame = new JInternalFrame("",true,true); 
    title=file.getName(); 
    sts=file.getPath(); 

     tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13)); 
    //tx.setLineWrap(true); 
    internalFrame.add(tx); 
    i+=1; 
    internalFrame.setName("Doc "+i); 
    JScrollPane scrollpane=new JScrollPane(tx); 

      internalFrame.setTitle(title); 
    tp.add(internalFrame); 
    try{ 
     tp.setSelectedIndex(i-1); 
    } 
    catch(IndexOutOfBoundsException ioe){  
    } 
     internalFrame.add(scrollpane); 
    internalFrame.setVisible(true); 
    internalFrame.addInternalFrameListener(new InternalFrameAdapter() { 
     @Override 
     public void internalFrameClosing(InternalFrameEvent e) { 
      tp.remove(internalFrame); 
     } 
    }); 


    tx.setText(t); 
    try { 
      br.close(); 
      } 

     catch (IOException ex) { 
      Logger.getLogger(Open.class.getName()).log(Level.SEVERE, null, ex); 
     } 
} 

感謝

+0

你在說哪個標籤?請包括**所有相關的代碼**。 – BackSlash 2014-09-26 10:00:36

+0

jtabbedpane @backslash – lavanya 2014-09-26 10:02:38

+0

這裏有問題嗎? – erikduvet 2014-09-26 10:03:31

回答

0

你先進口的java.util。*,然後ArrayList<String> = new ArrayList<String>()店臨時文件名String tempfile="get current filename"店臨時VAL檢查文件列表int i=0存在,檢查文件存在於數組for(String s:) al) { if(s.equals(tempfile)) i++;}中,如果文件存在於列表中,則if(i==0) al.add("tempfile");否則顯示消息「文件已經打開」。如果你關閉一個標籤,那麼你可以使用刪除方法從列表中刪除文件名al.remove("filename")

+0

如何知道封閉標籤的文件名 – lavanya 2014-09-26 11:15:52