1

I'm currently monitoring a sql file continuously for changes using a while loop, and if my sql is updated I'm performing certain task like playing a media file.

I want to do changes in the GUI also, if the sql is updated. But I'm not able to do so. My GUI contains of many buttons and when the sql is updated i want to shift the focus from one button to another button.

I know that I have to use Swing Worker to solve this issue. but I'm not able to do that. Even after using swing worker, I can't seem to get the current focus written in the textfield area. here is the code:

private SwingWorker <Void,Void> worker;
    private JTextField forr;
    public void dorun(){


        // this is for printing the current focus on the text area  

        worker = new SwingWorker <Void,Void>(){

            @Override
            protected Void doInBackground() throws Exception {
                if(current_focus.equals("EMERGENCY")){

                    //Thread.sleep(2000);
                    forr.setText("EMER");
                }
                // TODO Auto-generated method stub
                return null;

            }
            protected void done() {
                forr.setText("EMER");
            }

        };
        worker.execute();
    }

// it print everything on the console and nothing on the gui

    void core(JFrame f){


        // the large while loop to keep updating sql and playing sounds
while(count2!=10000){


            entry=0;
            java.sql.PreparedStatement pst = null;
            //System.out.println(4);
            try{
            String query = "SELECT * FROM threeorfive";
                pst =  connection.prepareStatement(query);
                ResultSet rs = pst.executeQuery();
                while(rs.next()){

                    a[entry]=rs.getInt(4);
                    //System.out.println(a[entry]);
                    entry++;

                }

                //System.out.println(5);

                if(a[0]==1 && a[1]==1)
                    {dorun();
                    current_focus = button_infocus();


                    System.out.println("Focus on :");
                      System.out.println(current_focus);



                    }

                else if(a[0]==0 && a[1]==1){

                    current_focus = button_infocus();
                    current_focus = focuson_next(current_focus);
                    System.out.println("Focus on :");
                    System.out.println(current_focus);
                    FileInputStream Fis1 = new FileInputStream("C:\\Users\\Arohi\\workspace\\HCI\\3second.mp3");
                    Player playMp32 = new Player(Fis1);
                    playMp32.play();

                    String query2 = "UPDATE threeorfive set value=1 WHERE ID=1 ";   
                    java.sql.PreparedStatement pst2 = connection.prepareStatement(query2);
                    pst2.execute();

                    String query3 = "UPDATE threeorfive set value= 1 WHERE ID= 2  " ;   
                    java.sql.PreparedStatement pst3 = connection.prepareStatement(query3);
                    pst3.execute();


                }

                else if(a[0]==1 && a[1]==0){
                    FileInputStream Fis2 = new FileInputStream("C:\\Users\\Arohi\\workspace\\HCI\\5second.mp3");
                    Player playMp321 = new Player(Fis2);
                    playMp321.play();

                    if(current_focus.equals("EMERGENCY")){
                    try{

                            FileInputStream Fis = new FileInputStream("C:\\Users\\Arohi\\workspace\\HCI\\Emergency.mp3");
                            Player playMp3 = new Player(Fis);
                            playMp3.play();

                        }
                            catch (Exception e1){
                                System.out.println(e1);
                            }           



                    }else if(current_focus.equals("WASHROOM")){
                        try{

                            FileInputStream Fis = new FileInputStream("C:\\Users\\Arohi\\workspace\\HCI\\washroom.mp3");
                            Player playMp3 = new Player(Fis);
                            playMp3.play();

                        }
                            catch (Exception e1){
                                System.out.println(e1);
                            }

                    }
                    else if(current_focus.equals("TEXT")){
                        //JOptionPane.showMessageDialog(null, "text");
                    System.out.println("text");








                    }else if(current_focus.equals("LIGHTS")){
                        try{

                            FileInputStream Fis = new FileInputStream("C:\\Users\\Arohi\\workspace\\HCI\\light.mp3");
                            Player playMp3 = new Player(Fis);
                            playMp3.play();
                            String query4 = "UPDATE threeorfive set value=0 WHERE ID=3 ";   
                            java.sql.PreparedStatement pst4 = connection.prepareStatement(query4);
                            pst4.execute();


                        }
                            catch (Exception e1){
                                System.out.println(e1);
                            }


                    }else if(current_focus.equals("FAN")){
                        try{
                            FileInputStream Fis = new FileInputStream("C:\\Users\\Arohi\\workspace\\HCI\\fan.mp3");
                            Player playMp3 = new Player(Fis);
                            playMp3.play();
                            String query5 = "UPDATE threeorfive set value=0 WHERE ID=4 ";   
                            java.sql.PreparedStatement pst4 = connection.prepareStatement(query5);
                            pst4.execute();
                        }
                            catch (Exception e1){
                                System.out.println(e1);
                            }

                        }else if(current_focus.equals("WATER")){
                            try{
                                FileInputStream Fis = new FileInputStream("C:\\Users\\Arohi\\workspace\\HCI\\glass.mp3");
                                Player playMp3 = new Player(Fis);
                                playMp3.play();

                            }
                                catch (Exception e1){
                                    System.out.println(e1);
                                }



                        }else if(current_focus.equals("CLOSE")){
                            f.dispose();
                            System.exit(0);

                        }else ;

                    String query2 = "UPDATE threeorfive set value=1 WHERE ID=1 ";   
                    java.sql.PreparedStatement pst2 = connection.prepareStatement(query2);
                    pst2.execute();

                    String query3 = "UPDATE threeorfive set value= 1 WHERE ID= 2  " ;   
                    java.sql.PreparedStatement pst3 = connection.prepareStatement(query3);
                    pst3.execute();



                }else {
                    a[0]=1;
                    a[1]=1;
                }


                try {
                    Thread.sleep(1000);  //1000 milliseconds is one second.
                    count2++;
                } catch(InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }



            }catch(Exception  e){
                JOptionPane.showMessageDialog(null, e);
            }   

        }   
   }
}
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
Arohi Gupta
  • 95
  • 1
  • 8
  • Swing is single threaded and not thread safe, you should never modify the ui from within the doInBackground method, instead, you should call publish method to push what you want to the EDT and the process method to update the ui with the information you published – MadProgrammer Dec 29 '15 at 05:40
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Dec 29 '15 at 06:08
  • 1
    Possible duplicate of [*WatchService and SwingWorker: how to do it correctly?*](http://stackoverflow.com/q/7784909/230513) – trashgod Dec 29 '15 at 10:17

0 Answers0