Can you return user action from ActionEvent?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Can you return user action from ActionEvent?

  1. #1
    Join Date
    Apr 2010
    Posts
    4

    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.

    Code:
    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));
    			log.setEditable(false);
    			JScrollPane logScrollPane = new JScrollPane(log);
    			fc = new JFileChooser();
    			//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    			fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    			openButton = new JButton("Chose a directory");
    			openButton.addActionListener(this);
    			JPanel buttonPanel = new JPanel(); 
    			buttonPanel.add(openButton);
    			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);
    	}
    	log.setCaretPosition(log.getDocument().getLength());
    }
    	 
    } 
    
    public static void createAndShowGUI() {
        JFrame frame = new JFrame("TestMiningGui");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        frame.add(new GUIOptions());
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
    	 createAndShowGUI();
                
    	File[] testFiles = getFileListFromDirectory(This is where I cant get it to call the user specified directory);

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center