Search code examples
javamultithreadingswingconcurrencyswingworker

Unresponsive jbutton requesting concurrency


Can anyone help me to implement the thread for my unresponsive jbutton ? i want to insert to the database by using the pepraredStatement . however whenever i'm adding the database part (connection and pepraredStatement ) the 'UPLOAD' button goes unresponsive . but when i remove anything related to the database , all my buttons are working . you can find here the code http://pastebin.com/euKdWhr2 .

I will really appreciate any help or suggestion . i've had explanation about the thread , but i still can't include it to my code .

public void actionPerformed(ActionEvent ev)
        {
            String file = fileField.getText();
            SetGetQuestionFileName pattern = new SetGetQuestionFileName(file);
                ConnectToDatabase database = new ConnectToDatabase();
            try
            {

            ///////// check whether textfile is empty or not 

            if( ev.getActionCommand().equals("UPLOAD"))
            {
                if(fileField.getText().isEmpty())
                {
                    JOptionPane.showMessageDialog(null,"File field can not be empty!!! Please try again.","ALERT", JOptionPane.ERROR_MESSAGE);
                }
                else
                    {
                        File fi = new File(fileField.getText());
                        ////////////////  perform upload 

                        try 
                            {

                    String sql = "INSERT INTO testsystem.questionnaire (category_questions, questions, correct_answer)" + "VALUES (?, ?, ?)";

                    PreparedStatement st =  null;

                    Connection dbconnection = database.getConnection();

                    st = dbconnection.prepareStatement(sql);

                                    if(fi.getAbsoluteFile().exists())
                                    {
                                        List<String> lines = Files.readAllLines(Paths.get(fileField.getText()), Charset.defaultCharset());


                                        for (int i = 0; i < lines.size(); i+=10) 
                                            {
                                                    String category = lines.get(i);
                                                    System.out.println(category);
                                                    String question = lines.get(i+1);
                                                   System.out.println(question);

                                                    String answers =
                                                                    lines.get(i+2)+System.lineSeparator()
                                                                    +lines.get(i+3)+System.lineSeparator()
                                                                    +lines.get(i+4)+System.lineSeparator()
                                                                    +lines.get(i+5);
                                                    System.out.println(answers);

                                                    String correct = lines.get(i+7);
                                                    System.out.println("correct answer is: "+correct);
                                                    System.out.println("----------------");


                                    st.setString(1, category);
                                    st.setString(2, answers);
                                    st.setString(3, correct);
                                    st.executeUpdate(); 

                                        }

                                        JOptionPane.showMessageDialog(null,"File has been successfully uploaded in the database.","NOTIFCATION",JOptionPane.INFORMATION_MESSAGE);
                                    }
                else

                        JOptionPane.showMessageDialog(null,"File could not be found. Please try again","ALERT",JOptionPane.ERROR_MESSAGE);
                }

                        catch(SQLException ex)
                    {

                    }   

                    catch(Exception ex)
                    {

                    }

Solution

  • When you have an action it is highly recommended that you'll encapsulate your code that actually does the job. In your case it's the db (call it DBThread). What will work for you so the button will not go unresponsive is to divide it in to 3 parts (sounds too much at first but it makes sense). So you'll have

    1. Action (that invokes the DBThreadProgress)
    2. DBThreadProgress (it's a thread that will have a join at the end and will invoke DBThread)
    3. DBThread (this is the job)

    So the DBThread as mentioned before is your business logic. The Action invokes a progress thread where you can put a gif image on your panel that shows that something is going in the background for the end user. This progressThread allows you also to wait until the DBthread is completed so you can advise the end users about the result. I usually set the button to setEnable(false) in the beginning and setEnable(true) so the user will know that he can't click twice.

    Hope this helps :-) enter image description here