FileDialog und der Dateityp

Status
Nicht offen für weitere Antworten.

el_vital

Aktives Mitglied
du kannst einen FilenameFilter verwenden, den du mittels setFilenameFilter() setzt.


habe ich und es hat gar keine funktion. Wird es von SUN nicht mehr unterstützt?

Code:
FilenameFilter ff = new FilenameFilter (){
   public boolean accept(File dir, String name)
   {
      String upperCaseName = name.toUpperCase();
      return (upperCaseName.endsWith(".JPG") ||
                upperCaseName.endsWith(".JPEG") ||
                upperCaseName.endsWith(".GIF"));
    }
};

sbDialog.setFilenameFilter(ff);
sbDialog.setModal(true);
 

X5-599

Top Contributor
mmm. vielleicht deswegen:

Sets the filename filter for this file dialog window to the specified filter. Filename filters do not function in Sun's reference implementation for Microsoft Windows

bin mir aber nicht ganz sicher, was die damit sagen wollen... daskommt übrigens aus der api. java6
 

Ebenius

Top Contributor
Ich denke es wird noch unterstützt. Aber schau Dir einfach mal das FileChooserDemo2 im Sun Tutorial: How to Use File Choosers an, da kann man gut spicken. Soweit ich weiß, benutzen die den FileFilter (und nicht FilenameFilter), aber ob das was damit zu tun hat, kann ich Dir nicht sagen.

Ebenius
 

el_vital

Aktives Mitglied
mmm. vielleicht deswegen:



bin mir aber nicht ganz sicher, was die damit sagen wollen... daskommt übrigens aus der api. java6

ja, das habe ich auch gesehen. Eine alternative wäre ja JFileChooser, aber mit dem Aussehen des Dialogs bin ich überhaupt nicht zufrieden. Gibt es eine Möglichkeit das JFileChooser wie das FileDialog aussehen zu lassen?
 

Ebenius

Top Contributor
Oh oh... FileDialog... Da hab ich mich verlesen... Ich dachte, es ginge bereits um den JFileChooser.

Ebenius
 

L-ectron-X

Gesperrter Benutzer
Am besten geht das, wenn du folgendes als erste Anweisung in deine main()-Methode schreibst:
[HIGHLIGHT="Java"]try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
//nichts zu tun, automatisches Setzen des Metal-LAF
}[/HIGHLIGHT]
 

el_vital

Aktives Mitglied
JFileChooser kommt wohl nicht in Frage, da z.B. auf dem Desktop unter Windows Vista kein "Neuer Ordner" erstellt werden kann. Diese Option ist deaktiviert.
 

Ebenius

Top Contributor
JFileChooser kommt wohl nicht in Frage, da z.B. auf dem Desktop unter Windows Vista kein "Neuer Ordner" erstellt werden kann. Diese Option ist deaktiviert.
Hast Du das ausprobiert oder gelesen? Wenn Du es irgendwo gelesen hast, dann hätte ich den Link gern. Ich habe kein Vista (eigentlich kein Windows), interessiere mich aber trotzdem dafür, was für Probleme meine Kunden mit meiner Software so haben können. :)

Ebenius
 

Ebenius

Top Contributor
Hm... also bei dem Bug den ich gefunden habe -- Java-Bug 6493523 -- geht es, wie in dem von Dir verlinkten Artikel, nur um den "My Documents"-Folder. Der Bug ist seit JRE 6 Update 7 geflickt. Wogegen testest Du?

Ebenius
 

el_vital

Aktives Mitglied
keine ahnung. Ich habe die neueste Java-Version mit den neuesten updates und ich kann auf dem Desktop keine neue Ordner mit dem JFileChooser erstellen. Die funktion ist deaktiviert.
 
Zuletzt bearbeitet:

Ebenius

Top Contributor
Stimmt, der andere Bug oben ist auch erst in Java 7 geflickt. Hab mich verlesen. :)

Wenn es nicht dringend notwendig ist, dass auf dem Desktop Verzeichnisse erzeugt werden, würde ich trotzdem JFileChooser nehmen und damit leben, dass es bis Java 7 in diesem Fall nicht funktioniert. :-S

Ebenius
 

el_vital

Aktives Mitglied
ich betrachte es als extrem wichtig die Freiheit zu besitzen überall Verzeichnisse erstellen zu können wenn ich etwas abspeichern möchte.
 

mvitz

Top Contributor
oder der JFileChooser den Bug nicht hätte ;)

Du musst wohl oder übel mit einem der beiden Sachen leben oder den Workaround aus dem Sun Issue Tracker den du gepostet hast benutzen.

Ich persönlich könnte wohl eher mit dem Bug vom JFileChooser leben. Aber wie Ebenius sagte ist das wohl subjektiv.
 

Ebenius

Top Contributor
es wäre doch alles perfekt wenn man bei dem FileDialog den Dateityp vorgeben könnte...
Ja. Oder wenn der Create-Folder-Button funktionieren würde. Wäre auch cool. Oder wenn meine Bank jede Woche anrufen würde, dass ich Geld abholen soll, weil der Tresor nicht zugeht. ;)

Ich bastele Dir für letzteres mal einen Workaround, wenn ich's hinbekomme. Pure AWT-Komponenten fasse ich aber aus Überzeugung nicht an. :)

Ebenius
 

Ebenius

Top Contributor
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 &ge; 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 &quot;enabled&quot; property of the given new folder action to
* the UI's new folder action's &quot;enabled&quot; 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
 
Zuletzt bearbeitet:

el_vital

Aktives Mitglied
Danke! Ich teste es heute Abend. Wenn es mir ermöglicht überall neue Ordner anzulegen, dann bist du in dieser Sportart ja richtig fit.
 

el_vital

Aktives Mitglied
ich habe es nun getestet. Einen neuen Ordner kann man jetzt erstellen, aber keinen Namen vergeben bzw. nicht umbenennen.
 

Ebenius

Top Contributor
Und jetzt funkioniert auch das. Wer hat Lust zu testen?

[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 &ge; 7, both bugs are filed
* as fixed in JRE 7.
*
* @version 1.1
* @author Sebastian Haufe
*/
public class JXFileChooser extends JFileChooser {

/** Serial version UID */
private static final long serialVersionUID = 5754629044130732867L;

// -------------------------------------------------------------------------
// Instance fields
// -------------------------------------------------------------------------

private Set<Action> newFolderActions;
private boolean workaround4847375Active;

// -------------------------------------------------------------------------
// 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() {
workaround4847375Active = true;
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);
}
}
}
}

workaround4847375Active = true;
}

/** 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);
}
}

@Override
public File getCurrentDirectory() {
final File orgFile = super.getCurrentDirectory();
if (workaround4847375Active
&& canWriteInDirectory(orgFile)
&& !orgFile.canWrite()) {
final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
boolean workaround = false;
for (StackTraceElement element : stackTrace) {
if (element.getClassName().endsWith(".FilePane")
&& element.getMethodName().equals("editFileName")) {
workaround = true;
}
}
if (workaround) {
return new File(orgFile.getParent(), orgFile.getName()) {

/** Serial version UID */
private static final long serialVersionUID = 2415160092213139529L;

@Override
public boolean canWrite() {
return true;
}

@Override
public int hashCode() {
return orgFile.hashCode();
}

@Override
public boolean equals(Object obj) {
return orgFile.equals(obj);
}
};
}
}
return orgFile;
}

/**
* Reset the &quot;enabled&quot; property of the given new folder action to
* the UI's new folder action's &quot;enabled&quot; property value, if the
* current file is a file system.
*
* @param action the action to possible enable
*/
void enableNewFolderActionAsNecessary(Action action) {
final File directory = getCurrentDirectory();
if (workaround4847375Active && canWriteInDirectory(directory)) {
final BasicFileChooserUI ui = (BasicFileChooserUI) getUI();
action.setEnabled(ui.getNewFolderAction().isEnabled());
}
}

private boolean canWriteInDirectory(File directory) {
final FileSystemView fsView = getFileSystemView();
return directory != null
&& (directory.canWrite() || fsView.isFileSystem(directory));
}
}[/HIGHLIGHT]

Ebenius
 

el_vital

Aktives Mitglied
das Kopieren des codes in diesem neuen Forum ist umständlich. Die Zeilennummern werden mit kopiert und müssen von Hand entfernt werden.
 

el_vital

Aktives Mitglied
umbenennen ist leider immer noch nicht möglich. Auch da nicht wo es mit der normalen Klasse möglich ist.
 

Ebenius

Top Contributor
Vielleicht liegt es daran, dass ich Vista 64Bit habe? Ich mache morgen ein kleines Beispiel und lasse es jemanden mit Vista 32Bit testen.
Vista kann ein Grund sein, 64 Bit glaub ich nicht. Kann den Test mal jemand mit XP machen? Möglichst mit einem eingeschränkten (also nicht Admin-) Nutzer?

Mir fehlt leider das Vista zum Test, sonst würde ich dafür sicher auch einen Workaround finden. :(

Ebenius
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
N FileDialog - Detailansicht Java Basics - Anfänger-Themen 0
N Input/Output mit FileDialog Verzeichnis wechseln Java Basics - Anfänger-Themen 20
G Filedialog und Mac OS X Leopard: Aktueller Pfad? Java Basics - Anfänger-Themen 3
G Nach MenuKlick FileDialog öffnen Java Basics - Anfänger-Themen 3
D FileDIalog klappt. aber geladenes bild nicht angezeigt Java Basics - Anfänger-Themen 2
A Filedialog Java Basics - Anfänger-Themen 3
B JFileChooser <-> FileDialog Java Basics - Anfänger-Themen 4
O Bei FileDialog alten Inhalt löschen Java Basics - Anfänger-Themen 7
S FileDialog/ JFileChooser, Bilder laden, Dateifilter, Diashow Java Basics - Anfänger-Themen 8
V Daten aus FileDialog verwenden Java Basics - Anfänger-Themen 4
P FileDialog Problem Java Basics - Anfänger-Themen 2
B Dateityp von File bekommen Java Basics - Anfänger-Themen 2
D Operatoren Variable mit einem Dateityp vergleichen Java Basics - Anfänger-Themen 8
T Selbst erstellten Dateityp mit java Programm assoziieren? Java Basics - Anfänger-Themen 4
M JFileChooser Dateityp vorbelgen Java Basics - Anfänger-Themen 3
G Dateityp Java Basics - Anfänger-Themen 2
S Dateityp verhindert Positionswechsel Java Basics - Anfänger-Themen 20
H Datei mit bestimmten Dateityp Java Basics - Anfänger-Themen 2
G Filechooser ohne Auswahl für Dateityp Java Basics - Anfänger-Themen 2

Ähnliche Java Themen

Neue Themen


Oben