• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file chooser

 
trinadh reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using JFileChhoser we can select a file.in that way can we open a directory.if so please tell me how to do this
 
Vasu Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;


public class DemoJFileChooser extends JPanel
implements ActionListener {
JButton go;

JFileChooser chooser;
String choosertitle;

public DemoJFileChooser() {
go = new JButton("Do it");
go.addActionListener(this);
add(go);
}

public void actionPerformed(ActionEvent e) {
int result;

chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
}
else {
System.out.println("No Selection ");
}
}

public Dimension getPreferredSize(){
return new Dimension(200, 200);
}

public static void main(String s[]) {
JFrame frame = new JFrame("");
DemoJFileChooser panel = new DemoJFileChooser();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
}
}


Hope you get idea, how to do.

Regards,

Vasu
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Starter/Vasu,

Welcome to JavaRanch!

Hi,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Joke names, "nicks" and "handles" aren't acceptable here. You can change your display name here. Thanks for your prompt cooperation.
 
Vasu Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for letting me know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic