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);


Reply With Quote

Bookmarks