1

I have a javaFx application that works no problem at all when I run the main method from Eclipse. However when I convert the application into an executable .jar and run the .jar when I click the button that uses FileChoose to select a file i get this error.

"Exception in thread "Thread-20" java.lang.IllegalStateException: Thisoperation is permitted on the event thread only; currentThread = Thread-20"

Here is the code that I have and again this code works great when I run the application from eclipse, just not when its converted to a .jar. I used eclipse to convert the application to a jar.

public void uploadMessagesButtonActionPerformed(ActionEvent event) {
    if(uploadCSVThread.isAlive()) {
        uploadCSVThread.interrupt();
        try {
            uploadCSVThread.join();
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }

    uploadCSVThread = new Thread() {
        public void run() {
            Platform.runLater( new Runnable() {
                public void run() {
                    loader.setProgress(0.0);
                    loader.setVisible(true);
                }
            });

            FileChooser currUploadFile = new FileChooser();
            File selectedFile = currUploadFile.showOpenDialog(null);
            if(selectedFile != null) {
                System.out.println(selectedFile.getPath());

                try {
                    FileReader currRdr = new FileReader(selectedFile);
                    CSVReader currReader = new CSVReader(currRdr);
                    final ArrayList<String[]> listOfMessages = (ArrayList<String[]>) currReader.readAll();
                    for(int i = 0; i < listOfMessages.size(); i++) {
                        final int x = i;
                        String[] shipmentMessages = listOfMessages.get(i);
                        //String[] shipmentMessages = currReader.readNext();
                        //while(shipmentMessages != null) { 
                        /*for(int i = 0; i < shipmentMessages.length; i++) {
                            System.out.println("shipmentMessages -> " + shipmentMessages[i]);
                        }*/
                        Map<String, String> headers = createHeaderMapFromHeaderString(shipmentMessages[0], "\n");
                        String body = shipmentMessages[1];
                        shipmentMessages = currReader.readNext();

                        try {
                            /*ObjectName queue = new ObjectName("org.apache.activemq:type=Broker,brokerName=" + currentBrokerName
                                                           + ",destinationType=Queue,destinationName=" + VM_AND_DIRECTVM_Q);*/
                            //headers.put("TO_DESTINATION", returnSelectedEndpoints());
                            QueueViewMBean queueView = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn,
                                                                                                                      currentQueue,
                                                                                                                      QueueViewMBean.class,
                                                                                                                      true);
                            queueView.sendTextMessage(headers, body, serverLookUp.serverUserNameLookUp(currentServer), serverLookUp.serverPasswordLookUp(currentServer));
                            Platform.runLater( new Runnable() {
                                public void run() {
                                    loader.setProgress((double)x/(double)listOfMessages.size());
                                }
                            });

                        } catch (MalformedObjectNameException e) {
                            logger.error(e);
                        }
                    }

                    updateQueueInList(currentQueue, currentQueueIndx, true);
                    readQueue(conn, currentQueue);
                    loader.setVisible(false);   
                } catch (Exception e) {
                    messageCouldntSendPopUpBox.setVisible(true);
                    logger.error(e);
                }
            }
            loader.setVisible(false);
        }
    };  
    uploadCSVThread.start();
}
Connor Blair
  • 73
  • 2
  • 10
  • Have you tried putting your `FileChooser` into its own `Thread`? – Weasemunk Jul 05 '16 at 18:15
  • what do you mean by that? In a way its in its own thread right now right, the uploadCSVThread? – Connor Blair Jul 05 '16 at 18:24
  • I tried putting it into a Platform.runlater thread but it needs to know the file immediately, not later. If it waits till later to get the file the code will run and the selectedFile will just be null until the runLater thread starts – Connor Blair Jul 05 '16 at 19:26
  • Connor, see also: [Can I pause a background Task / Service?](http://stackoverflow.com/questions/14941084/javafx2-can-i-pause-a-background-task-service) – jewelsea Jul 05 '16 at 19:37

1 Answers1

0

A bit long so moved to answer. I misread your braces; it is certainly in its own thread. I would add some e.printStackTrace()s in your catches to find exactly where you're encountering your error. It seems one of your method calls is not valid within the body of your thread. If you find the location and still can't figure it out, please update your post to let us know. Otherwise, you may want to look into using Task objects .

Weasemunk
  • 455
  • 4
  • 16
  • Well gang, it looks likes I got it working. I kind of cheated haha, but it still works. I made a global File variable and whenever the button is clicked i set that global File variable outside of the thread, and then reference it inside the thread. That way I dont actually call the FileChooser in the uploadCSVThread. Thanks everyone for their help and assistance it is really appreciated =]. I would like to point out though that this was not a duplicate question. I tried using Platform.runLater and it did not solve my problem. – Connor Blair Jul 06 '16 at 12:15