Farrell Coleman
Farrell Coleman

Reputation: 79

Java - Passing a button click from one class to the main class

Firstly I am sure there is an answer lurking in this site and I did try and look but all the methods I tried continuously failed. I am still quite new at programming in Java so go easy on me, because what you are about to witness is some incredibly bodged code!

I am trying to learn Selenium, but before I write tests I wanted to make a simple IDE that asks what browser you'd like to run and what test you'd like to run. So far I had it running fine in a pop up for the browser but that wasn't useful if I wanted to add more options. So I am now trying to create a Jframe in my main class containing other classes which contain the content of any buttons I wish to add. Here is where things go wrong.

CLASS 1 (MAIN)

public class DynamicBrowsers {

public static void main(String[] args) {


    BrowserBox b = new BrowserBox();
    JFrame IDE = new JFrame("IDE");
    IDE.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    BrowserBox newContentPane = new BrowserBox();
    IDE.setContentPane(newContentPane);
    IDE.setPreferredSize(new Dimension(200, 100));
    IDE.pack();
    IDE.setVisible(true);

    WebDriver driver = null;

    if(b.browserValue == 0){
        //driver=new FirefoxDriver();   
        System.out.println("No browser Selected");
    }else if(b.browserValue == 1){
        driver = new ChromeDriver();
        System.out.println("FF!");  
    }else if(b.browserValue == 2){
        driver = new ChromeDriver();
        System.out.println("Chrome!");
    }else if(b.browserValue == 3){
        driver = new InternetExplorerDriver();
        System.out.println("IE!");
    }
    }
}

CLASS 2 (combo box and button)

public class BrowserBox extends JPanel {

public String browserPick;
String[] browsers = {"Please Select a Browser","Mozilla", "Chrome", "IE"};
public int browserValue = 0;
JButton runButton = new JButton("Run Test");
public JComboBox browserPicker = new JComboBox(browsers);     

public BrowserBox() { 


    add(runButton);        
    add(browserPicker);

    ActionListener cbActionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent listChoice) {

            String s = (String) browserPicker.getSelectedItem();//get the selected item

            switch (s) {//check for a match

                case "Please Select a Browser":
                    System.out.println(browserPick);
                    break;                
                case "Mozilla":
                    browserPick = "Mozilla";
                    System.out.println("Could have been a worse choice than " + browserPick);
                    break;
                case "Chrome":
                    browserPick = "Chrome";
                    System.out.println("Good choice picking " + browserPick);
                    break;
                case "IE":
                   browserPick = "IE";
                    System.out.println("For some reason you chose " + browserPick);
                    break;
                default:
                    browserPick= "Please Select a Browser";
                    System.out.println("No match selected, defaulting too " + browserPick);
                    break;
            } 
        }            
    };

    ActionListener bActionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent runClicked) {

            if (browserPick == "Mozilla"){
                browserValue = 1;     
                System.out.println("FF clicked " + browserValue);
            }
            else if (browserPick == "Chrome"){
                browserValue = 2;
                System.out.println("Chrome clicked " + browserValue);
            }
            else if (browserPick == "IE"){
                browserValue = 3;
                System.out.println("IE clicked " + browserValue);
            } 

        }

    };

    browserPicker.addActionListener(cbActionListener);
    runButton.addActionListener(bActionListener);

}
}  

I imagine I am implementing it all wrong. I kind of feel like I should have made the button a seperate class or in the main class, but I'm unsure. If anyone could point me in the right direction, point out what I'm doing wrong and if possible offer a simple fix that would be great.

Thank you, Farrell

Upvotes: 1

Views: 1262

Answers (2)

Farrell Coleman
Farrell Coleman

Reputation: 79

Unbelievable, It seems I had forgotten to capitalise the C in chrome at one point in a string compare and that is what cost me 2 hours of my life lol.

Thank you all for the input, now that I've worked out what was wrong I'm going to go back and try make it more object orientated by having only the JFrame in the main class. Ty again

Upvotes: 0

arcy
arcy

Reputation: 13113

There are a couple of things wrong here.

You declare two vars of type BrowserBox -- you use one and test the other (b and newContentPane).

You also use "==" to test whether one string is equal to another. That won't work in the general case; you need to use "String".equals(value) or some other form of the equals() method.

Good luck.

Upvotes: 1

Related Questions