1

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Shubhro
  • 73
  • 2
  • 11
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Sep 11 '15 at 04:34
  • `action=="START"` is not how `String`'s are compared in Java, you should be using something more like `"START".equals(action)`. You're blocking the Event Dispatching Thread with a long running process, which would prevent the user from interacting with your program. Take a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for more details – MadProgrammer Sep 11 '15 at 04:36
  • @MadProgrammer : I really wonder whether `action=="START"` or `action.equals("START")` really matters here. My question was When I press the START button, other buttons gets inactive, I cant click on any of the buttons , neither i can close the frame unless i have to kill the program from IDE. – Shubhro Sep 11 '15 at 05:04
  • 1
    *"I cant click on any of the buttons*" - ***"You're blocking the Event Dispatching Thread with a long running process, which would prevent the user from interacting with your program. Take a look at Concurrency in Swing and Worker Threads and SwingWorker for more details"*** – MadProgrammer Sep 11 '15 at 05:06
  • `"Happy" == happy` may not be `true`, so you need to fix that as well. I just didn't know which part of your question I should close on, so I closed on what seemed like the most common issue and provided information to solving the other. Both issues are commonly asked around here. – MadProgrammer Sep 11 '15 at 05:07

0 Answers0