Ich bastele Dir für letzteres mal einen Workaround, wenn ich's hinbekomme. Pure AWT-Komponenten fasse ich aber aus Überzeugung nicht an.
Fertig. Habe auf Windows 2003 Server getestet. Wer macht den Test für Windows XP & Windows Vista?
[HIGHLIGHT="Java"]import java.awt.*;
import java.io.File;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicFileChooserUI;
/**
* A {@link JFileChooser} derivate, working around the <a
* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4847375"> Java Bug
* 4847375</a>.
* <p>
* Attention: This workaround is disabled for JRE ≥ 7, both bugs are filed
* as fixed in JRE 7.
*
* @version 1.0
* @author Sebastian Haufe
*/
public class JXFileChooser extends JFileChooser {
// -------------------------------------------------------------------------
// Instance fields
// -------------------------------------------------------------------------
private Set<Action> newFolderActions;
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/**
* Constructs a <code>JXFileChooser</code> pointing to the user's default
* directory. This default depends on the operating system. It is typically
* the "My Documents" folder on Windows, and the user's home directory on
* Unix.
*/
public JXFileChooser() {
super();
}
/**
* Constructs a <code>JXFileChooser</code> using the given current directory
* and <code>FileSystemView</code>.
*/
public JXFileChooser(File currentDirectory, FileSystemView fsv) {
super(currentDirectory, fsv);
}
/**
* Constructs a <code>JFileChooser</code> using the given <code>File</code>
* as the path. Passing in a <code>null</code> file causes the file chooser
* to point to the user's default directory. This default depends on the
* operating system. It is typically the "My Documents" folder on Windows,
* and the user's home directory on Unix.
*
* @param currentDirectory a <code>File</code> object specifying the path to
* a file or directory
*/
public JXFileChooser(File currentDirectory) {
super(currentDirectory);
}
/**
* Constructs a <code>JXFileChooser</code> using the given
* <code>FileSystemView</code>.
*/
public JXFileChooser(FileSystemView fsv) {
super(fsv);
}
/**
* Constructs a <code>JXFileChooser</code> using the given current directory
* path and <code>FileSystemView</code>.
*/
public JXFileChooser(String currentDirectoryPath, FileSystemView fsv) {
super(currentDirectoryPath, fsv);
}
/**
* Constructs a <code>JXFileChooser</code> using the given path. Passing in
* a <code>null</code> string causes the file chooser to point to the user's
* default directory. This default depends on the operating system. It is
* typically the "My Documents" folder on Windows, and the user's home
* directory on Unix.
*
* @param currentDirectoryPath a <code>String</code> giving the path to a
* file or directory
*/
public JXFileChooser(String currentDirectoryPath) {
super(currentDirectoryPath);
}
// -------------------------------------------------------------------------
// Overriding stuff from the UI
// -------------------------------------------------------------------------
@Override
protected void setUI(ComponentUI newUI) {
final ComponentUI oldUI = getUI();
if (oldUI != null
&& oldUI.getClass().getName().equals(
"com.sun.java.swing.plaf.windows.WindowsFileChooserUI")) {
uninstallCreateFolderWorkaround();
}
super.setUI(newUI);
final String javaVer = System.getProperty("java.specification.version");
try {
if (1.69 > Double.parseDouble(javaVer)
&& newUI.getClass().getName().equals(
"com.sun.java.swing.plaf.windows.WindowsFileChooserUI")) {
installCreateFolderWorkaround();
}
} catch (NumberFormatException ex) {}
}
private void uninstallCreateFolderWorkaround() {
if (newFolderActions != null) {
newFolderActions.clear();
}
}
private void installCreateFolderWorkaround() {
final Set<Action> newFolderActions = getNewFolderActions();
final LayoutManager layout;
if (!((layout = getLayout()) instanceof BorderLayout)) {
return;
}
final Component topComponent =
((BorderLayout) layout).getLayoutComponent(BorderLayout.NORTH);
if (!(topComponent instanceof Container)) {
return;
}
final Container topPanel = (Container) topComponent;
final int componentCount = topPanel.getComponentCount();
for (int i = 0; i < componentCount; i++) {
final Component child = topPanel.getComponent(i);
final Action action;
if (child instanceof AbstractButton) {
if ((action = ((AbstractButton) child).getAction()) != null) {
if ("New Folder".equals(action.getValue(Action.ACTION_COMMAND_KEY))) {
newFolderActions.add(action);
enableNewFolderActionAsNecessary(action);
}
}
}
}
}
/** Lazily creates the weak action hash set. */
private Set<Action> getNewFolderActions() {
if (newFolderActions == null) {
newFolderActions =
Collections.newSetFromMap(new WeakHashMap<Action, Boolean>());
}
return newFolderActions;
}
@Override
public void setCurrentDirectory(File dir) {
super.setCurrentDirectory(dir);
for (Action a : getNewFolderActions()) {
enableNewFolderActionAsNecessary(a);
}
}
/**
* Reset the "enabled" property of the given new folder action to
* the UI's new folder action's "enabled" property value, if the
* current file is a file system.
*
* @param action the action to possible enable
*/
void enableNewFolderActionAsNecessary(Action action) {
final File file = getCurrentDirectory();
final FileSystemView fsView = getFileSystemView();
if (file != null && fsView.isFileSystem(file)) {
final BasicFileChooserUI ui = (BasicFileChooserUI) getUI();
action.setEnabled(ui.getNewFolderAction().isEnabled());
}
}
}[/HIGHLIGHT]
Ich liebe solchen Sport!
Ebenius