-2

I opened a file using JFileChooser. If I opened a file it is opened for first time and if I open the same file for second time it is also opened in new tab. I want to open a file for one time only.

Here is my code,

    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);
        }
}

Thanks

lavanya
  • 1
  • 1
  • Which tab are you talking about? Please include **all the relevant code**. – BackSlash Sep 26 '14 at 10:00
  • jtabbedpane @backslash – lavanya Sep 26 '14 at 10:02
  • Is there a question here? – erikduvet Sep 26 '14 at 10:03
  • 2
    When the code opens a `File`, put it in a collection like a `HashMap`. The `File` would be the key and the internal frame (or tab or whatever) would be the value. Before opening a new file, check if it is in the map. If so, just refocus that internal frame or tab. – Andrew Thompson Sep 26 '14 at 10:04
  • is there any example@andrewthompson – lavanya Sep 26 '14 at 10:06
  • @lavanya That 4th comment is almost an answer. Try to implement that. You won't get that much descriptive solution. I mean code. Go ahead. Come back to us, if you face any implementation hurdles. In short, do not expect working code :) Good luck. – Suresh Atta Sep 26 '14 at 10:10
  • Thank you for reply, i understand that but how to refocus particular internal frame.@AndrewThompson – lavanya Sep 26 '14 at 10:46
  • You can remove filename from arraylist using index then getindex of tabbedpane and remove it`al.remove(tabbedpane.getselectedindex())` – Benjamin Sep 26 '14 at 11:36
  • Here is the link to similar question with working code http://stackoverflow.com/a/26060927/4015305 – VaibJ Sep 26 '14 at 15:43

1 Answers1

0

You first import java.util.* and then ArrayList<String> = new ArrayList<String>() store temp filename String tempfile="get current filename" store temp val to check file exist in list int i=0 and check a file exist in array for(String s:) al) { if(s.equals(tempfile)) i++;} if file doent exist in list thenif(i==0) al.add("tempfile");otherwise show message "file already open" . If you close a tab then you can remove a filename from list using remove method al.remove("filename")

Benjamin
  • 2,257
  • 1
  • 15
  • 24