Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 100: Add a history mechanism to JFileChooser

Enhance JFileChooser by implementing a directory history and file previewer accessory

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 3

You use a configuration file called <Application_Name>_DIRECTORY_HISTORY.cfg in the user's home directory to store the combo box state between sessions. The application name differentiates between two or more Java applications that use the EFileChooser class. The name is provided in the constructor. If the application is started and the EFileChooser class is loaded, you examine the configuration file in order to build up the combo box model. Similarly, if the application is closed, you must provide a call to the static public EFileChooser.saveDirectoryEntries() method in order to store the current state. That call typically resides in the windowClosing() method of the application's frame WindowListener. I have used the Java serialization mechanism here, but you could also use a text file to store the entries in a human-readable and -editable format.

The PreviewerAndHistoryPanel

The File Chooser accessory for your new features is placed in a PreviewAndHistoryPanel that subclasses JPanel. You register a ComboItemListener that watches for item state changes. Additionally you register keyboard actions for Delete and Shift-Delete. If the user has selected a directory in the combo box and presses the Delete key, that item is removed from the history list. Further, if the user presses the Shift-Delete keys, all history items are removed. To handle long directory path names, you implement your own combo-box renderer and replace the default one. The DeleteKeyListenener is straightforward: You use an action command to differentiate between the two states "Delete Selected Item" and "Delete All Items". Of course, you can also write two different listeners, but the code will essentially be the same.

private final class DeleteKeyListener implements ActionListener {
  String action_;
  DeleteKeyListener(String action) {
    action_ = action;
  }
  public void actionPerformed(ActionEvent e) {
    if (action_.equals("ONE")) {
      combo.removeItemAt(combo.getSelectedIndex());
    }
    else {
      combo.removeAllItems();
    }
  }
}


The ItemListener watches for directory selections in the combo box list and sets the JFileChooser's current directory according to that selection. You use an ItemListener instead of an ActionListener because an action event is also generated if the Delete key or the Shift-Delete keys are pressed to delete an item.

An important detail is setting the tooltip text for the combo box item if the full directory name is too large to be displayed completely. To display tooltips, you must extend BasicComboBoxRenderer as shown in the code's inner class ComboBoxRendererWithToolTips. Note that when JComboBox is located near the border of a frame, the tooltip doesn't display outside the frame because of current Swing limitations.

private final class ComboItemListener implements ItemListener {
   String dir;
   public void itemStateChanged(ItemEvent e) {
      if (e.getStateChange() == ItemEvent.SELECTED) {
         selectNewDirectory();
      }
   }
   private void selectNewDirectory() {
      dir = (String)combo.getSelectedItem();
      EFileChooser.this.setCurrentDirectory(new File(dir));
      JLabel label = new JLabel(dir);
      label.setFont(combo.getFont());
      if (label.getPreferredSize().width > combo.getSize().width){
         combo.setToolTipText(dir);
      }
      else {
         combo.setToolTipText(null);
     }
   }
}


To review the actions completed so far, look at the PreviewAndHistoryPanel constructor to see how things are connected:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources