Question

I have a little application that opens a fileChooser when clicking a button and, after choosing a file, works with the contents of it. So, let's say we have this pseudo-code:

    File file = new File();
    JFileChooser fileChooser = new JFileChooser();

    [...]

    actionPerformed() {
      file = fileChooser.getSelectedFile();
      doStuffWithFile(file);
    }

At this point, it seems the code is taking on a procedural style, because everything that happens with file now actually happens while still inside that actionPerformed()-method. I'm not sure if this ok.

Is this okay coding style? Intuitively, I would want to end actionPerformed() and have the methods to work on my file called from somewhere else. But how would I do that?

An idea would be to just set the new value of file inside actionPerformed(). Then I could get that value with a getter. But how so? It should be the next thing that happens.

I've seen a propertyChangeListener here at stackoverflow, but I'm not sure if that's the right thing, too.

Was it helpful?

Solution

One option is multithreading. It would still do everything, but not in the event thread. for example:

actionPerformed(){
    new Thread(){
        public void run(){
            file = fileChooser.getSelectedFile();
            doStuffWithFile(file);
        }
     }.start()
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top