This discussion is archived
5 Replies Latest reply: Apr 9, 2013 12:33 AM by 1001591 RSS

impossible to click button twice

1001591 Newbie
Currently Being Moderated
Hello dear java users,
I am a bgineer to both java and this forum. I have been trying to develop a small desktop application. I have JFrame and a button. When I click the button, the app is supposed to save record to database. But it is impossible to click the botton twice. To explain in detail: when a user fill in the form and click the button, the app will warn the user if he/she haven't fill in the appropriate JTextFields. After warning it is impossible to click the button again. I mean after first click, the button is busy and impossible to click.
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.awt.Dimension;
public class KelimeGir extends JFrame{
    JFrame frameKelimeGir;
    JTextField fieldKelime;
    JComboBox comboGrup;
    JComboBox comboTur;
    JTextField fieldAnlam1;
    JTextField fieldAnlam2;
    JTextArea areaOrnekCumle;
    JButton buttonKaydet;
    JButton buttonIptal;
    public KelimeGir(){
        createInterface();
    }
    public void createInterface(){
         SOME GUI CODE
    }//end of createInterface method
    private JComboBox createGrupComboBox(){

    }
    private boolean saveNewVoc(){
        boolean result=false;
        int dialogResult;
        long id=findVocID();
        String kelime=fieldKelime.getText();
        Object grup=comboGrup.getSelectedItem();
        Object tur=comboTur.getSelectedItem();
        String anlam1=fieldAnlam1.getText();
        String anlam2=fieldAnlam2.getText();
        String ornek=areaOrnekCumle.getText();
        String message;
        String mesTitle;
        if(kelime.equals("")){
            message="'Kelime' kısmı boş bırakılamaz!";
            mesTitle="Kelime Girişi Hatası";
            JOptionPane.showMessageDialog(frameKelimeGir, message,mesTitle,JOptionPane.ERROR_MESSAGE);
            return result;
        }
        else if(grup==null){
            message="'Grubu' kısmı boş bırakılamaz!";
            mesTitle="Kelime Girişi Hatası";
            JOptionPane.showMessageDialog(frameKelimeGir, message,mesTitle,JOptionPane.ERROR_MESSAGE);
            return result;
        }
        else if(tur==null){
            message="'Türü' kısmı boş bırakılamaz!";
            mesTitle="Kelime Girişi Hatası";
            JOptionPane.showMessageDialog(frameKelimeGir, message,mesTitle,JOptionPane.ERROR_MESSAGE);
            return result;
        }
        else if(anlam1.equals("")){
            message="'1.Anlamı' kısmı boş bırakılamaz!";
            mesTitle="Kelime Girişi Hatası";
            JOptionPane.showMessageDialog(frameKelimeGir, message,mesTitle,JOptionPane.ERROR_MESSAGE);
            return result;
        }
        else{
            mesTitle="Veri Bankası Uyarısı";
            message="Kelime: "+kelime+"\n";
            message+="Grubu: "+grup.toString()+"\n";
            message+="Türü: "+tur.toString()+"\n";
            message+="1.Anlamı: "+anlam1+"\n";
            message+="2.Anlamı: "+anlam2+"\n";
            message+="Örnek Cümle: "+ornek+"\n";
            message+="\n";
            message+="Yukarıdaki kelimeyi Veri Bankasına eklemek üzeresiniz!";
            message+="Devam etmek istiyor musunuz?"+"\n";
            dialogResult=JOptionPane.showConfirmDialog(frameKelimeGir, message,mesTitle, JOptionPane.YES_NO_OPTION);
            if(dialogResult==JOptionPane.YES_OPTION){
                result=true;
                JOptionPane.showMessageDialog(frameKelimeGir, "OK",mesTitle,JOptionPane.ERROR_MESSAGE);
            }
        }
        return result;
    }
    private long findVocID(){

    }
    class KaydetButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            boolean res=saveNewVoc();
            if(res){
                JOptionPane.showMessageDialog(frameKelimeGir,"Kelime, Veri Bankasına kaydedildi.",
                        "Kelime Girişi",JOptionPane.INFORMATION_MESSAGE);
            }
            else{
                //JOptionPane.showMessageDialog(frameKelimeGir, "Kelime, Veri Bankasına kaydedilemedi!","Kelime Girişi",JOptionPane.INFORMATION_MESSAGE);
            }
        }//end of actionPerformed method
    }//end of ButtonListener inner class
    class IptalButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            frameKelimeGir.setVisible(false);
            frameKelimeGir.dispose();
            //System.exit(0);
        }
    }
}//end of KelimeGir class
Edited by: 998588 on 08.Nis.2013 00:05

Edited by: 998588 on 08.Nis.2013 00:26

Edited by: 998588 on 08.Nis.2013 01:36
  • 1. Re: impossible to click button twice
    gimbal2 Guru
    Currently Being Moderated
    Repost the code with \
     tags to make it readable and prevent the forum from trying to interpret the special characters.                                                                                                                                                                                                                                                        
  • 2. Re: impossible to click button twice
    1001591 Newbie
    Currently Being Moderated
    I am trying to repost with gode tags but i can't. And which special characters?
  • 3. Re: impossible to click button twice
    Kayaman Guru
    Currently Being Moderated
    998588 wrote:
    I am trying to repost with gode tags but i can't.
    Sure you can. Just edit the post and put the code between
     tags.                                                                                                                                                                                                                                                                                                                                    
  • 4. Re: impossible to click button twice
    gimbal2 Guru
    Currently Being Moderated
    boolean res=saveNewVoc();
    Judging by what you say, this call here must be the culprit - it must be blocking for some reason or another (connection to database failing?). You invoke this directly from the button handler, meaning it is invoked on the Event Dispatch Thread (EDT) which takes care of dealing with updating the GUI and what not. If you somehow block it, it will seem like your user interface is locked up.

    So first order of business: when that action handler is invoked (IE: you press the button), how far does execution go? Put some System.out.println() statements in there to know what is invoked and what is not.
  • 5. Re: impossible to click button twice
    1001591 Newbie
    Currently Being Moderated
    Thank you for your reply. When I debug the project, I see that problem arises at the last line of "class KaydetButtonListener implements ActionListener". I mean AbstractButton.java class arises.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points