O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

150
|
Chapter 4, File Choosers
#28 Add a Right-Click Context Menu to the JFileChooser
HACK
the millions of users out there with older versions of Java, then you need
your own implementation. This hack creates a right-click menu on the
JFileChooser
to give the user those missing features.
The goal of this task is to create a contextual menu. This means it’s a menu
that pops up when you hit the right mouse button (or Control-click on one-
button mice). It also is contextual, or context sensitive. This means the menu
changes—or does something different—depending on what you currently
have selected. In this case, there will be two actions: Delete will delete the
currently selected file or directory, if one is selected; New Folder will create
a new folder (called, not surprisingly, New Folder) in the current directory.
Both of these actions depend on the current state of the file selection, so
they are considered context sensitive.
The Problem
For the most part, Swing is quite extensible, but often only in ways that the
Swing team thought of beforehand. The
JFileChooser has ways to change
the rendering of file icons, filtering the file list, adding components, and
changing the text. It does not have any way to add pop-up menus, though.
In short, we’ll have to hack it, and there’s no better way to start than by
reusing another hack.
Chapter 8 has a hack for adding pop-up menu support to any frame; see
“Create a Global Right-Click”
[Hack #57]. By reusing its RightClickGlassPane,
you can start off with a pop-up menu in place. That just leaves making a
connection between the pop up and the file chooser component. Sounds
easy, right? Well, if it were easy, it wouldn’t be in this book!
First, create a
JFileChooser subclass and attach the right-click glass pane:
public class ContextMenuFileChooser extends JFileChooser {
protected Component right_click_pane;
public ContextMenuFileChooser( ) {
super( );
JPopupMenu popup = new JPopupMenu( );
popup.add(new DeleteAction(this));
popup.add(new NewFolderAction(this));
right_click_pane = new RightClickGlassPane(this,popup);
}
}
This code defines a new subclass of JFileChooserContextMenuFileChooser.
The constructor builds a custom subclass of the
RightClickGlassPane and a
new
JPopupMenu pre-filled with two custom actions: one for deleting files and
one for creating new folders.
Add a Right-Click Context Menu to the JFileChooser #28
Chapter 4, File Choosers
|
151
HACK
With the RightClickGlassPane initialized, you need to install it in the actual
window that contains the
JFileChooser. While you can create your own
window, the most common usage of
JFileChooser
is to show it with the
showDialog( )
method; this method creates a new window and installs the
chooser in it automatically. However, this means that you have no access to
the real window until it’s already on screen. Fortunately, the Swing team
thought of this (let’s give them some credit) and provided the
createDialog( )
function. This protected method can be overridden to modify the dialog
before it shows up on screen. A new implementation of
createDialog( ) to
add to
ContextMenuFileChooser
is shown here:
protected JDialog createDialog(Component parent) {
JDialog dialog = super.createDialog(parent);
// create the right-click glass pane.
dialog.setGlassPane(right_click_pane);
right_click_pane.setVisible(true);
return dialog;
}
With this code in place, you can right-click anywhere on the file chooser to
see the context menu. It’s pretty useless, however, since the menu items
don’t actually do anything. You still have to create the Delete and New
Folder actions. The
JFileChooser API makes this pretty easy, though:
class DeleteAction extends AbstractAction {
protected JFileChooser chooser;
public DeleteAction(JFileChooser chooser) {
super("Delete");
this.chooser = chooser;
}
public void actionPerformed(ActionEvent evt) {
File file = chooser.getSelectedFile( );
if(file != null) {
file.delete( );
chooser.rescanCurrentDirectory( );
}
}
}
The new DeleteAction above is a standard AbstractAction subclass, with a
small change: it requires a
JFileChooser in its constructor. The action needs
the chooser to tell it which file has been selected. In the
actionPerformed( )
implementation, you can see that the action gets the selected file, deletes it,
and then tells the chooser to refresh itself. That’s it! Pretty simple.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required