0

It's a good practice to call File methods(like read a text file) within JFrame class or what should I do if not? Thanks for reply.

This is my code:

private void filechooserButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  

        JFileChooser fileChooser = new JFileChooser();

        fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt", "text"));
        fileChooser.setAcceptAllFileFilterUsed(false);

        int returnVal = fileChooser.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {

            resultTextArea.setText(null);
            filePathTextField.setText(fileChooser.getSelectedFile().getAbsolutePath());

            try (BufferedReader buffReader = new BufferedReader(new FileReader(new File(fileChooser.getSelectedFile().getAbsolutePath())))) {

                String line;

                while ((line = buffReader.readLine()) != null) {
                    resultTextArea.append(line + "\n");
                }

            } catch (Exception exc) {
                JOptionPane.showMessageDialog(MainGUI.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
            }

        }

    }

UPDATE : I edited my code to use SwingWorker, so I hope it's better than was:

private class textFileReader extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {

            JFileChooser fileChooser = new JFileChooser();

            fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt", "text"));
            fileChooser.setAcceptAllFileFilterUsed(false);

            int returnVal = fileChooser.showOpenDialog(null);

            if (returnVal == JFileChooser.APPROVE_OPTION) {

                resultTextArea.setText(null);
                filePathTextField.setText(fileChooser.getSelectedFile().getAbsolutePath());

                try (BufferedReader buffReader = new BufferedReader(new FileReader(new File(fileChooser.getSelectedFile().getAbsolutePath())))) {

                    String line;

                    while ((line = buffReader.readLine()) != null) {
                        resultTextArea.append(line + "\n");
                    }

                } catch (Exception exc) {
                    JOptionPane.showMessageDialog(MainGUI.this, "Error: " + exc, "Error", JOptionPane.ERROR_MESSAGE);
                }

            }

            return null;
        }

    }
enkeyz
  • 43
  • 1
  • 7
  • Consider having a look at [How to Use Actions](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) for some more ideas – MadProgrammer Oct 22 '14 at 22:22
  • Do you mean calling them *on the event thread?* it isn't the same thing. – user207421 Oct 22 '14 at 22:24
  • `BufferedReader buffReader = new BufferedReader(new FileReader(new File(fileChooser.getSelectedFile().getAbsolutePath())))` should be `BufferedReader buffReader = new BufferedReader(new FileReader(fileChooser.getSelectedFile()))` and should be immediately followed by `resultTextArea.read(buffReader, fileChooser.getSelectedFile());` – Andrew Thompson Oct 22 '14 at 22:40
  • 1
    *"It's a good practice to call File methods within JFrame class?"* Given it's probably unnecessary to extend `JFrame`, then no. – Andrew Thompson Oct 22 '14 at 22:50

1 Answers1

3

Taking your question literally, there's nothing wrong with it. Since you can define your own methods, and code structure is somewhat up to the developer, there's nothing technically wrong with doing file handling inside a class that also happens to extend JFrame.

That said, what I think you're actually asking is "Is it a good practice to do file IO from within Swing methods, such as filechooserButtonActionPerformed()?" And the answer to that question is unequivocally no - never do this.

These methods are called by Swing on the UI thread, also known as the Event Dispatch Thread, and while the UI thread is waiting for these methods to return, your application is frozen. It cannot repaint, it cannot respond to user input, nothing. Therefore instead you want to offload IO and other long-running work to other threads. There's a good tutorial in the Swing documentation: Lesson: Concurrency in Swing.

See also: Java Event-Dispatching Thread explanation

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244