CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2010

    Can you return user action from ActionEvent?

    Hello, I am working on a program and trying to add a GUI to it. I want the user to chose a directory for files to be run into the program. I am using a separate class for the GUI and I cant seem to pass the anything from ActionEvent to my main. Is this possible? My code is below. Any help or advice at all would be greatly appreciated.

    public class GUIOptions extends JPanel
    implements ActionListener {
    	static private final String newline = "\n";
    	JButton openButton;
    	JTextArea log;
    	JFileChooser fc;
    	public GUIOptions() {		
    			super(new BorderLayout());
    			log = new JTextArea(25,50);
    			log.setMargin(new Insets(5,5,5,5));
    			JScrollPane logScrollPane = new JScrollPane(log);
    			fc = new JFileChooser();
    			openButton = new JButton("Chose a directory");
    			JPanel buttonPanel = new JPanel(); 
    			add(buttonPanel, BorderLayout.PAGE_START);
    			add(logScrollPane, BorderLayout.CENTER);
    public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
    	int returnVal = fc.showOpenDialog(GUIOptions.this);
    	if (returnVal == JFileChooser.APPROVE_OPTION) {
    		File file = fc.getSelectedFile();
    		log.append("Opening: " + file.getName() + "." + newline);
    	//	File[] testFiles = fc.getSelectedFiles();
    		//return testFiles;
    	} else {
    		log.append("Open command cancelled by user." + newline);
    public static void createAndShowGUI() {
        JFrame frame = new JFrame("TestMiningGui");
        frame.add(new GUIOptions());
    public static void main(String[] args) {
    	File[] testFiles = getFileListFromDirectory(This is where I cant get it to call the user specified directory);

  2. #2
    Join Date
    May 2006

    Re: Can you return user action from ActionEvent?

    Your GUIOptions class doesn't have a method getFileListFromDirectory() and if it did it would have to be an instance method as the FileChooser is called from an instance method. This means your main method would need a reference to the GUIOptions instance so it can call the method. You also need an instance attribute to keep a record of what file/dir was selected so you can generate the file list.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.