JFileChooser - "Öffnen-Button" aktivieren/deaktivieren möglich???

the_rookie

Mitglied
Hi zusammen,
gibt es eine Möglichkeit den Öffnen-Button zu deaktivieren - sprich erst wenn eine bestimmte Bedingung eintritt kann man diesen betätigen.

Habe die API und das Netz nach einer Lösung durchsucht, allerdings ohne Erfolg...

Hier mal mein bis zu diesem Zeitpunkt generiertes Coding:

[Java]
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

public class FileChoosing {

static File[] file;
static String[] dateiname_quelle;
static String dateipfad_quelle;
static String komplettpfad_quelle;

public FileChoosing(){

UIManager.put("FileChooser.readOnly", Boolean.TRUE);

JFileChooser fc = new JFileChooser("F:/Programmierung/Workspace/Programmierprojekt01/Musik/");
fc.setMultiSelectionEnabled(true);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);


fc.setFileFilter( new FileFilter() {

@Override public boolean accept( File f )
{
return f.isDirectory() || f.getName().toLowerCase().endsWith( ".mp3" );
}

@Override public String getDescription() {
return "MP3-Musikdateien";
}
} );

int state = fc.showOpenDialog( null );

if ( state == JFileChooser.APPROVE_OPTION ) {

file = fc.getSelectedFiles();

for(int i = 0; i<file.length;i++){

dateiname_quelle = new String[file.length];

dateiname_quelle = file.getName();
dateipfad_quelle = file[0].getParent();


}

for(int k = 0; k < dateiname_quelle.length; k++){

if(k == 0){

komplettpfad_quelle = dateipfad_quelle.concat("\\").concat(dateiname_quelle[k]);

}
else{

komplettpfad_quelle = komplettpfad_quelle.concat("\\").concat(dateiname_quelle[k]);

}
}
}

else{

System.out.println( "Auswahl abgebrochen" );
//System.exit( 0 );
}

}
}


[/code]


Das Coding ist für Euch zur besseren Übersicht, was ich da mal so generiert hab - funkt auf jeden Fall

Dank jedem schon mal im Voraus
:rtfm:
the_rookie
 
Erstaunlich. Wenn man nach den ensprechenden Stichworten im Web sucht, findet man die gleiche Frage ein paar zig-tausend mal. Und bei den allermeisten führt die Antwort zu einer Lösung, wo im AccessibleContext auf gut Glück irgendein Button in irgendeinem Panel unsichtbar gemacht wird :autsch:

Naja. Hab' mal was geschrieben, womit man den "Cancel" oder "Approve"-Button enablen oder disablen kann. Ohne Gewähr.

Java:
import java.awt.Component;
import java.awt.Container;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.plaf.FileChooserUI;

/**
 * Utility methods for file choosers
 */
public class FileChooserUtils
{
    /**
     * Sets whether or not the 'approve' button of the given JFileChooser is
     * enabled.
     * 
     * @param fileChooser The file chooser
     * @param enabled Whether the 'approve' button should be enabled.
     */
    public static void setApproveButtonEnabled(
        JFileChooser fileChooser, boolean enabled)
    {
        FileChooserUI ui = (FileChooserUI)fileChooser.getUI();
        String text = ui.getApproveButtonText(fileChooser);
        setButtonEnabled(fileChooser, text, enabled);
    }

    /**
     * Sets whether or not the 'cancel' button of the given JFileChooser is
     * enabled.
     * 
     * @param fileChooser The file chooser
     * @param enabled Whether the 'cancel' button should be enabled.
     */
    public static void setCancelButtonEnabled(
        JFileChooser fileChooser, boolean enabled)
    {
        Locale locale = fileChooser.getLocale();
        String text = UIManager.getString(
            "FileChooser.cancelButtonText", locale);
        setButtonEnabled(fileChooser, text, enabled);
    }

    /**
     * Sets whether or not the button with the given text is enabled. The given
     * component will be searched recursively for a JButton with the given text,
     * and the first JButton that is found with the given text will be enabled
     * or disabled accordingly.
     * 
     * @param component The component
     * @param buttonText The text on the button
     * @param enabled Whether the button should be enabled.
     */
    private static boolean setButtonEnabled(
        Component component, String buttonText, boolean enabled)
    {
        if (component instanceof JButton)
        {
            JButton button = (JButton)component;
            String text = button.getText();
            if (text != null && text.equals(buttonText))
            {
                button.setEnabled(enabled);
                return true;
            }
        }
        else if (component instanceof Container)
        {
            Container container = (Container)component;
            for (int i = 0; i < container.getComponentCount(); i++)
            {
                Component child = container.getComponent(i);
                boolean found = setButtonEnabled(child, buttonText, enabled);
                if (found)
                {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Private constructor to prevent instantiation
     */
    private FileChooserUtils()
    {}
}
 
Hi Marco13,

vielen Dank erstmal. Bin leider gerade krank im Bett und konnt mich bis dato nicht drum kümmern. Werde es allerdings ausprobieren, sobald ich wieder fit bin

:applaus:
 

Neue Themen


Zurück
Oben