-2
    //boutton
    ent.setBounds(490,400,100,50);      
    ent.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {   
            String text;
            text=chatbox.getText().toLowerCase();
            chatarea.append("\t\t\t\t"+text+" <- YOU \n");
            chatbox.setText("");
            if(text.contains("hii") || text.contains("hey") ||  text.contains("hi"))
            {
                bot("hey there");
                bot("how can i help you?");
            }
            else if(text.contains("info") || text.contains("help") || text.contains("i need information") || text.contains("help me"))
            {
                bot("sure i am here to help you");
                bot("what you want to know about?");
                bot("1 info about anything?");
                bot("2 info about place?");
                bot("enter your choice : ");
                if(text.contains("1")|| text.contains("info about anything") || text.contains("number 1") || text.contains("first one") || text.contains("first"))
                {
                    bot("sure which blood group info you are looking for?");
                }else if(text.contains("2") || text.contains("info about place") || text.contains("number 2") || text.contains("second one") || text.contains("second"))
                else
                {
                    bot("I Don't Understand you");
                }
            }
        }
    });
}
private void bot(String string)
{
    chatarea.append("BOT -> "+string+"\n");
}
public static void main(String[] args)
{
    new bot();
}
} 

I am making a chat bot in which when I click on the JButton it takes the value from JTextField and posts it on a JTextArea. But the problem is I want my code to be continue not stop but every time click on button it start the code from starting.

How to make code continue from same place in action performed when I second time click on the button?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Check the question title . Improve it. I am not able to understand it – Himanshu sharma Aug 11 '18 at 11:21
  • I already change the title – Shubhanshu Dani Aug 11 '18 at 11:26
  • 1
    Seems like an `if else` with a boolean value or an `if else` for checking if text area has any text can help you. – Emre Dalkiran Aug 11 '18 at 11:40
  • Basically what I want is when a user write any text on jtextbox and click on jbutoon the system will respond automatically on that text but the prob when user press on jbutoon second time it goes on the top I want it to not go to start of if else I want it to continue the if else – Shubhanshu Dani Aug 11 '18 at 13:19
  • 1
    So it’s about state. You need a member variable to hold the state and logic to change the state and to act depending on current state – Joakim Danielson Aug 11 '18 at 14:03
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 11 '18 at 14:08
  • Can you show me some examples how to do it – Shubhanshu Dani Aug 11 '18 at 15:13

1 Answers1

0

Add a boolean member to your botclass to keep track of the state (first time / not first time) and initialise it to true

boolean isFirstTimeButtonWasPressed = true

public void actionPerformed(ActionEvent e) {
    if (isFirstTimeButtonWasPressed) {
        //do stuff that should only happens the firs time   
        //...

        isFirstTimeButtonWasPressed = false;
    } 
    //do stuff that should be done every time the button is pressed
});
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52