Det er skam ikke så svært: import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.FileFilter;
public class Test extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JLabel label; private JButton button;
public Test() { setTitle("ChooserDemo"); label = new JLabel("no file"); add(label, BorderLayout.NORTH); button = new JButton("Choose"); button.addActionListener(this); add(button, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JFrame.setDefaultLookAndFeelDecorated(true); setSize(300, 200); setLocationRelativeTo(this); }
public void actionPerformed(ActionEvent e) { JFileChooser fc = new JFileChooser(new File("C:\\WINDOWS")); fc.addChoosableFileFilter(new ImageFilter());
int retVal = fc.showDialog(this, "Choose file"); switch (retVal) { case JFileChooser.CANCEL_OPTION: label.setText("Cancel"); break; case JFileChooser.ERROR_OPTION: label.setText("Error"); break; case JFileChooser.APPROVE_OPTION: label.setText(fc.getSelectedFile().getAbsolutePath()); break; } }
public static void main(String args[]) { new Test().setVisible(true); } }
class ImageFilter extends FileFilter { public boolean accept(File f) { return f.getName().endsWith(".bmp"); }
public String getDescription() { return "Images (bmp)"; } }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.