Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 6 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

L2FProd: Choose Directories with JDirectoryChooser

At 11:24 PM on May 30, 2005, R.J. Lorimer wrote:

Hey everybody - another tip about L2FProd Swing components today. Today I want to talk about the com.l2fprod.common.swing.JDirectoryChooser control. For those of you who have used the standard Swing javax.swing.JFileChooser , you're in luck - the JDirectoryChooser widget has, for the most part, the same functional API. Functionally, this is because the JDirectoryChooser extends from the JFileChooser .

For those of you who haven't used the file chooser, or simply need a refresher course, here is a quick run through on using the directory chooser widget that ships with L2FProd.

The first step is to create a control to trigger the directory chooser (assuming you want it to be used as a dialog). It is quite simple to do so - here is an example:

JButton button = new JButton("Show Directory Chooser");
button.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		JDirectoryChooser chooser = new JDirectoryChooser();
		int choice = chooser.showOpenDialog(frame);
		if(choice == JDirectoryChooser.CANCEL_OPTION) {
			System.out.println("User Canceled");					
		}
		else {
			System.out.println("Dialog Selection: " + chooser.getSelectedFile().getAbsolutePath());
		}
	}	
});

You would then put that button on a UI, or on some panel so that the user could press it to enable the directory chooser dialog. Alternatively, the JDirectoryChooser can be used in an existing component -potentially for some tool that needs a directory navigator (such as a file explorer application). In that case you would want to respond (most likely) whenever the selection in the chooser changed - in that case you can associate a property change listener:

JDirectoryChooser inlineChooser = new JDirectoryChooser();
inlineChooser.addPropertyChangeListener(new PropertyChangeListener() {
	public void propertyChange(PropertyChangeEvent evt) {
		String prop = evt.getPropertyName();
		// If the directory changed, don't show an image.
		if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
			File file = (File) evt.getNewValue();
			System.out.println("Inline Selection: " + file.getAbsolutePath());
		}
 
	};
});

You would then want to add that inlineChooser component to a panel, frame, etc as you would any other control. Another feature (inherited from the JFileChooser ) you would probably want to take advantage of is the ability to hide the OK/Cancel buttons (as they don't really apply in an inline component scenario). Here is how you do that:

inlineChooser.setControlButtonsAreShown(false);

Here is a screenshot:

... and here is a full source code implementation of the details discussed here today:

package com.javalobby.tnt.l2fprod;
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
 
import javax.swing.*;
 
import com.l2fprod.common.swing.JDirectoryChooser;
import com.l2fprod.common.swing.plaf.LookAndFeelAddons;
 
public class JDirectoryChooserTest {
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			LookAndFeelAddons.setAddon(LookAndFeelAddons
					.getBestMatchAddonClassName());
		}
		catch(Exception e) {
		}
 
		final JFrame frame = new JFrame();
 
		frame.setLayout(new BorderLayout());
		JDirectoryChooser inlineChooser = new JDirectoryChooser();
		inlineChooser.setControlButtonsAreShown(false);
		inlineChooser.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				String prop = evt.getPropertyName();
				// If the directory changed, don't show an image.
				if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
					File file = (File) evt.getNewValue();
					System.out.println("Inline Selection: " + file.getAbsolutePath());
				}
 
			};
		});
 
		JButton button = new JButton("Show Directory Chooser");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JDirectoryChooser chooser = new JDirectoryChooser();
				int choice = chooser.showOpenDialog(frame);
				if(choice == JDirectoryChooser.CANCEL_OPTION) {
					System.out.println("User Canceled");
				}
				else {
					System.out.println("Dialog Selection: "
							+ chooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
 
		frame.getContentPane().add(inlineChooser, BorderLayout.CENTER);
		frame.getContentPane().add(button, BorderLayout.SOUTH);
 
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
 
	}
 
}

Until next time,

R.J. Lorimer
rj -at- javalobby.org
http://www.coffee-bytes.com

1 . At 9:31 AM on Jun 9, 2005, Philippe Lhoste wrote:
  Click to reply to this thread Reply

Re: L2FProd: Choose Directories with JDirectoryChooser

Ah! That awful self-centered Microsoftish way of naming directories...
That's not the point, of course.

Two remarks:

  • Being lazy, I would have appreciated a link to the mentioned L2FProd. Not everybody know this product, and this would avoid a Google (or other) search...

  • This looks suspingly like the Windows file chooser, with the associated caveats:

    • Is it possible to specify a starting directory? Eg. setting it to the previously selected directory, or the "current" path, etc.

    • There should be a field to allow pasting/typing a path, as a shortcut to a known location.

    • I am very adept to drag'n'drop, so I would appreciate to be able to drag a folder from, say, Windows Explorer or Konqueror, to your dir. chooser to move directly there. I don't know if it is possible in Java, though.


It is often a pain to have to open a deep hierarchy to get to a place already opened elsewhere, and to have to do it again on next iteration...
2 . At 9:58 AM on Jun 9, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: L2FProd: Choose Directories with JDirectoryChooser

Philippe,

The link to L2FProd- at least, the component that was used in this particular tip is:
https://l2fprod-common.dev.java.net/
It was not intentional to leave this out; often times I write many tips in a batch, and sometimes those details get lost in the mix. I apologize.


  • It is possible to specify a starting directory, yes. As I tried to allude to in the tip:

    For those of you who have used the standard Swing javax.swing.JFileChooser , you're in luck - the JDirectoryChooser widget has, for the most part, the same functional API.

    Therefore, you specify the starting directory the same way you would with that API.

  • This Swing directory dialog implementation is designed to model a fairly standard dialog that is available on multiple platforms - you can see it as it exists in another Java windowing toolkit by visiting the SWT org.eclipse.swt.widgets.DirectoryDialog class.
    I would suppose the lack of a 'filtering' field by the implementors of this class is simply because the common pattern found in those platform implementations of this type of dialog also don't have the field - it's just not typically there. I don't think it would be difficult for you to add your own if necessary through some minor extending of the class.

  • I am not sure about drag and drop for these components - perhaps Fredric or someone else with L2FProd could step in to define the support available in that area. Let me re-stress as well that the directory chooser implementation from L2FProd extends the standard Java JFileChooser class, so in all likelihood it shares a lot of implementation support from that class, so a lot of your concerns may be easily mitigated in this area.


  • Regards,
Best, R.J. Lorimer
3 . At 10:11 AM on Jun 9, 2005, Frederic Lavigne wrote:
  Click to reply to this thread Reply

Re: L2FProd: Choose Directories with JDirectoryChooser

Hi Philippe,

> Two remarks:
> Being lazy, I would have appreciated a link to
> the mentioned L2FProd. Not everybody know this
> product, and this would avoid a Google (or other)
> search...

Main web site is www.L2FProd.com.

> There should be a field to allow pasting/typing a
> path, as a shortcut to a known location.
> I am very adept to drag'n'drop, so I would
> appreciate to be able to drag a folder from, say,
> Windows Explorer or Konqueror, to your dir. chooser
> to move directly there. I don't know if it is
> possible in Java, though.

I'll keep these two items in the TODO list for the JDirectoryChooser, specially the drag'n drop idea.

BTW, R.J, "perhaps Fredric or someone else with L2FProd ", well there is no someone else :) L2FProd.com is just me :)

cheers,
-fred
4 . At 10:12 AM on Jun 9, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: L2FProd: Choose Directories with JDirectoryChooser

Ferderic - slip of the fingers, it was supposed to be 'Frederic or someone else more familiar with ...'. Sorry!
Best, R.J. Lorimer
5 . At 9:54 AM on Jan 11, 2006, Steve Sare wrote:
  Click to reply to this thread Reply

Re: L2FProd: Choose Directories with JDirectoryChooser

Does JDirectoryChooser have the ability to create and delete directories?
6 . At 7:05 AM on Mar 29, 2006, michael-john wrote:
  Click to reply to this thread Reply

Re: L2FProd: Choose Directories with JDirectoryChooser

Hi there

i was trying to download the JDirectoryChooser from your site but was unable to find it on the site. Could you perhaps tell me how i could go about to downloading it. And i also wanted to know if your product has the ability to create and delete directories?

Regards Michael

thread.rss_message