I am trying to implement a code where in there are two buttons, one is start and another is stop. The code inside the Start button has some process which runs infinitely unless it is stopped from an external source, so i have another button which is supposed to stop the execution and exit out from the code. The code is as follows
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
@SuppressWarnings("serial")
public class PiaAuto extends JFrame {
JFrame rframe = new JFrame();
JLabel piaurl, l, jlmsg;
JTextField tpiaurl;
JButton jbsubmit, jbexit;
Container cp;
public PiaAuto()
{
rframe.setSize(600,300);
rframe.setLocationRelativeTo(null);
cp=getContentPane();
cp.setLayout(null);
setSize(550,300);
rframe.setTitle("Middle Tier Up");
cp.setBackground(new Color(140,180,180));
piaurl=new JLabel("Enter PIA Url");
piaurl.setFont(new Font("Verdana", Font.BOLD, 14));
piaurl.setForeground(Color.RED);
l= new JLabel("Middle Tier Live");
l.setFont(new Font("Verdana", Font.BOLD, 20));
jlmsg=new JLabel("",JLabel.CENTER);
tpiaurl=new JTextField(100);
jbsubmit=new JButton("START");
jbexit=new JButton("STOP");
l.setBounds(190, 10, 250, 35);
piaurl.setBounds(10,100,150,35);
tpiaurl.setBounds(130,105,450,30);
jbsubmit.setBounds(200,180,92,25);
jbexit.setBounds(300,180,92,25);
jbsubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String action =ae.getActionCommand();
if(action=="START"){
int num=1;
String url= tpiaurl.getText();
if(url.equals(""))
{
System.exit(ABORT);
}
else{
while(num>0){
System.setProperty("webdriver.ie.driver","C:\\ITC\\DOP\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
try{
driver.get(url);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.name("userid")).sendKeys(Keys.DELETE);
driver.findElement(By.name("userid")).sendKeys(Keys.DELETE);
driver.findElement(By.name("userid")).sendKeys("PS");
driver.findElement(By.name("pwd")).sendKeys("PS");
driver.findElement(By.name("Submit")).click();
driver.findElement(By.linkText("Sign out")).click();
driver.quit();
num++;
String dbname= url.substring(39, 47).toUpperCase();
System.out.println("Login and Logout happened "+(num-1)+ " time(s) for "+dbname);
}
catch(Exception ea){
ea.printStackTrace();
}
finally{
driver.quit();
}
try {
TimeUnit.MINUTES.sleep(60);
} catch (InterruptedException eb) {
eb.printStackTrace();
}
}
}
}
}
});
jbexit.addActionListener(new buttonListener());
this.add(jbsubmit);
this.add(jbexit);
cp.add(piaurl);
cp.add(tpiaurl);
cp.add(l);
cp.add(jlmsg);
cp.add(jbsubmit);
cp.add(jbexit);
rframe.add(cp);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
private class buttonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String act = e.getActionCommand();
if(act=="STOP")
{
System.exit(ABORT);
}
}
}
public static void main(String args[])
{
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
new PiaAuto();
}
}
I have to listener for two buttons , but it is not working according to what i want. Let me know if more clarification is needed.