• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

JFrame application

Hi, My JFrame application is almost complete but I have one idea that I would like to implement.

How can I keep the last 5 open file in the menu bar?

if I click on one of the las open file it should be able to open? can Java do that?


Thank You
C-begin
0
C-begin
Asked:
C-begin
  • 4
  • 3
  • 2
  • +2
4 Solutions
 
InteractiveMindCommented:
Sure.
There's loads of ways: Really, though, I would personally create a class, like so:

class LastFive implements Serializable
{
    String [] files = new String [5];
   
    public LastFive()
    {
        for ( int i = 0; i < files.length; i++ )
        {
            files [i] = "";
        }
    }
   
    public void add( String filename )
    {
        for ( int i = 0; i < files.lengthl i++ )
        {
            if ( files [i] == "" )
                files [i] = filename;
            else if ( i == 5 && files[i] != "" )
                files[0] = filename;
        }
    }
   
    public String getFile( int index )
    {
        return files[index];
    }
}

Then, just use that.. then serialize it to a file (and then deserialize it.., etc);

Got the idea? :)
0
 
OBCTCommented:
Is this the sort of thing your looking for?
It's very similar to InteractiveMind's suggestion but implemented slightly differently.

http://www.javaworld.com/javaworld/javatips/jw-javatip119.html
0
 
VolatileVoidCommented:
I don't know if there's a more graceful way of doing it, but let's assume there isn't.

If you want to remember the last 5 filenames opened in your program even between program restarts (i.e. I open 3 files, exit the program and then start it up again - the program should remember the last 3 files I opened), I suggest storing the paths of the last 5 files opened in some file and then reading that at startup and writing to it when you exit (since it's a JFrame, you'd write to the file on windowClosing).

If you just want to remember the last 5 files opened in the current instance of the program as well, every time a file is opened, I'd store it in an array of, say, JMenuItems.

Consider the following:
ArrayList files = new ArrayList ();
...
when I open a file:
if (files.size () < 5)
  files.add (new JMenuItem (pathToFile))'
else {
  files.remove (0); // 0 is always the oldest file last opened.
  files.add (new JMenuItem (pathToFile));
}

I add a JMenu to the JMenuBar of my JFrame, and I add a listener to it.
Say:
JMenu menu = new JMenu ();
... add stuff ...
menu.addMenuListener (new MenuListener () {
                                     public void menuSelected (MenuEvent e) {
                                       // over here, loop through the ArrayList and add the JMenuItems to the menu
                                     }
                                     public void menuDeselected (MenuEvent e) {
                                     // over here, loop through the ArrayList and remove whatever is in the ArrayList from the menu
                                     }
                                     public void menuCancelled (MenuEvent e) {
                                     // over here, loop through the ArrayList and remove whatever is in the ArrayList from the menu
                                     }
                                  });

Hope this helps.
 

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
OBCTCommented:
Just like in the link I posted ;-)
0
 
NaeemgCommented:
add menu and vector of file list as globall in ur class.

  JMenu jMenuRecentFiles = new JMenu();
  Vector vRecentFileList = new Vector();

now suppose u've array of list then loop ur list and call the following method in loop. and pass path of file and boolean arguments,
in this u may add upto 10 recent files. this method also first check if file exists then add otherwise will not add.

  /**
   * This method adds the given file path to Recent File menu List
   * @param filePath String
   * @param addAtTop boolean, if true then add this menu item at the TOP of list
   */
  private void addFilePath(String filePath, boolean addAtTop)
  {
    //** check whether this file exists or not, if yes then add.
     File thisFile = new File(filePath);
    if(thisFile.isFile() && thisFile.exists())
    {
      if(!vRecentFileList.contains(filePath))
      {
        //-- if Recent file list contains 10 items then remove last item from list and add new at top
        if(vRecentFileList.size() == 10) //- you may change this to 5
        {
          vRecentFileList.remove(vRecentFileList.get(vRecentFileList.size() - 1));
          jMenuRecentFiles.remove(jMenuRecentFiles.getItemCount() - 1);
        }

        final JMenuItem thisItem = new JMenuItem(filePath);
        thisItem.setIcon(new ImageIcon("Images/xml/doc.gif")); //- set icon for this menuitem
        if(addAtTop)
        {
          vRecentFileList.add(0, filePath);
          jMenuRecentFiles.add(thisItem, 2);
        }
        else
        {
          vRecentFileList.add(filePath);
          jMenuRecentFiles.add(thisItem);
        }
        thisItem.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            //System.out.println(thisItem.getText());
            //-- set this File path at the TOP of list
            vRecentFileList.remove(thisItem.getText());
            vRecentFileList.add(0, thisItem.getText());
            jMenuRecentFiles.remove(thisItem);
            jMenuRecentFiles.add(thisItem, 2);
            jMenuRecentFilesItem_actionPerformed(e);
          }
        });
      }
    }
  } //-- end of method private void addFilePath(String filePath)


  /**
   * This action is executed by dynamic Menu Item
   * @param e ActionEvent
   */
  void jMenuRecentFilesItem_actionPerformed(ActionEvent e)
  {
    //-- don't delete this method
  }
0
 
C-beginAuthor Commented:
OBCT, Thanks for the link,

I thought it would be easier than that,


is the another wayx
0
 
InteractiveMindCommented:
Have you considered my way?

copy and paste that class where necessary, then here's an example of it being used:

...
String filename = "a file name and location";

LastFive lf = new LastFive();
lf.add( filename );
lf.add( "another filename" );
...
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( "saveFile.ser" ) );   // Save to file
out.writeObject( lf );                                                                                                              //
out.close();                                                                                                                           //

Then, later on, to load it back up, use this:

...
ObjectInputStream in = new ObjectInputStream( new FileInputStream( "saveFile.ser" ) );
LastFive lf = (LastFive) in.readObject();

String [] lastFive = new String [5];
for ( int i = 0; i < 5; i++ )
{
   lastFive[i] = lf.getFile( i );
   if ( lastFive[i] == "" )
      break;
}
...


I hope that helps..?

Regards;
0
 
InteractiveMindCommented:
By the way, to save you scrolling back up again, here's the class you'd require for my idea:


class LastFive implements Serializable
{
    String [] files = new String [5];
   
    public LastFive()
    {
        for ( int i = 0; i < files.length; i++ )
        {
            files [i] = "";
        }
    }
   
    public void add( String filename )
    {
        for ( int i = 0; i < files.lengthl i++ )
        {
            if ( files [i] == "" )
                files [i] = filename;
            else if ( i == 5 && files[i] != "" )
                files[0] = filename;
        }
    }
   
    public String getFile( int index )
    {
        return files[index];
    }
}

0
 
C-beginAuthor Commented:
InteractiveMind  I don't quit understand your suggestion,

I am a nocive in java programing so I need to see a few example,

Thank you
0
 
InteractiveMindCommented:
Okay, fair enough. Could you post the code that you've done so far? Then I'll implement my idea into it for you?
0
 
NaeemgCommented:
dear C-begin, didn't u test my code?
0
 
C-beginAuthor Commented:
Hi all,

I would like to thank you for helping me,

Your inputs will be rewarded just to be fair and to show my appreciatio. However OBCT
gave me a great Start with the link that he gave me. I was able to solve 90% of my request.


Thank You All.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now