FileFilter geht nicht

Status
Nicht offen für weitere Antworten.

mukkerson

Mitglied
Ich habe folgendes Problem und zwar mache ich gerade einen Client für einen Server, aber beim FileChooser kann ich aufeinmal keinen FileFilter setzen( es ist wahrscheinlich nur ein blöder Fehler, aber ich erkenne ich gerade überhaupt nicht). Ich bedanke mich schonmal im voraus.

hier ist der quellcode:

[HIGHLIGHT="Java"]import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
import javax.swing.text.JTextComponent;

public class TVorklassifikator extends JFrame implements ActionListener
{


private JList nameList;
private DefaultListModel nameListModel;
//private Vector<String> nameListModel;
private JTextComponent file1;

public TVorklassifikator()
{
super("TVorklassifikator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridBagConstraints constraints = new GridBagConstraints();
setLayout(new GridBagLayout());

constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 2;
constraints.insets = new Insets(0, 2, 0, 2);
Label header = new Label("TVorklassifikator");

header.setFont(new Font("Arial",Font.BOLD, 16));
add(header, constraints);

constraints.gridheight = 4;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 5);
JButton choose1 = new JButton("Auswählen");
choose1.addActionListener(this);
add(choose1, constraints);

constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 5);
JLabel labelFile = new JLabel("Datei 1:");
add(labelFile, constraints);


constraints.gridheight = 8;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.insets = new Insets(5, 5, 5, 5);
file1 = new JTextField(" ");
add(file1, constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
JButton choose2 = new JButton("Auswählen 2");
choose2.addActionListener(this);
add(choose2, constraints);

constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
JLabel labelFile2 = new JLabel("Datei 2:");
add(labelFile2, constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
nameListModel = new DefaultListModel();
nameList = new JList(nameListModel);
//nameListModel = new Vector<String>();
JScrollPane fileName = new JScrollPane(nameList);
add(fileName, constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5,5,5,5);
JButton delete = new JButton("Eintrag entfernen");
delete.addActionListener(this);
add(delete,constraints);

constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 4;
constraints.anchor = GridBagConstraints.SOUTH;
constraints.insets = new Insets(5, 10, 5, 10);
JButton end = new JButton("Ende");
end.addActionListener(this);
add(end, constraints);



setSize(330, 450);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();

System.out.println("cmd: " + cmd);

if(cmd.equals("Auswählen"))
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
File f = fc.getSelectedFile();

file1.setText(f.getName());

}
}
if(cmd.equals("Auswählen 2"))
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
File f2 = fc.getSelectedFile();

nameListModel.addElement(f2.getName());
// nameListModel.add(f2.getName());
// nameList.setListData(nameListModel);

}
}

if(cmd.equals("Ende"))
{
int n = JOptionPane.showConfirmDialog(
this, "Anwedung wirklich beenden?", "Ende",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(n == JOptionPane.YES_OPTION)
System.exit(0);
}
if(cmd.equals("Eintrag entfernen"))
{
System.out.println("Eintrag entfernt");
}
}
public class MyFileFilter extends FileFilter
{
public boolean accept(File file)
{
if(file.isDirectory())
return true;
String name = file.getName();

if(name.endsWith(".xml"))
return true;
else
return false;
}
public String getDescription()
{
return "Text file(*.xml)";
}
}

public static void main(String[] args)
{
new TVorklassifikator();
}
}[/HIGHLIGHT]
ich will halt einen FileFilter haben, der xml Dateien filtert.

EDIT (Ebenius): Hab den Quelltext mal lesbar gemacht. Das geht mit [noparse][HIGHLIGHT="Java"]Quelltext[/HIGHLIGHT][/noparse]
 
Zuletzt bearbeitet von einem Moderator:
Hier im Forum gibts ne super Funktion um Java-Code zu posten. Ich muss persönlich sagen, dass ich es sehr schwer finde diesen "Text" zu lesen. (Das ist dein erster Post drum sei dir verziehen) 😀
Zweitens und das ist die wichtigere Frage WAS genau geht nicht? Ich sehe keinen Aufruf von setFileFilter()
 
jaa das werde ich mir für das nächste mal merken 🙂 eigentlich geht alles wunderbar nur , wenn ich auf die beiden Buttons Auswählen und Auswählen 2 gehe, dann erscheint im FileChooser kein Filter mit der Endung .xml... vielleicht kannst du mir den richtigen Ansatz reinschreiben, damit er das Anzeigt und auch wirklich filtriert#

gruß 😉
 
naja etwas in der Art
fc.setFileFilter(new MyFileFilter());

Weiß jetzt aba ned ob das syntaktisch richtig is ^^

Mist zu spät 😀
 
MyFileFilter filter = new MyFileFilter();

JFileChooser fc = new JFileChooser();
fc.setFileFilter(filter);
...
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben