Hi,
i'm trying to understand how actionPerformed is working.
I'd like to know how to add more than one action in actionPerformed method.

Here is an example of my code.
I have two button b and c
and the button b must open the gmail.com page. // this first action is working
the button c must open the google.com // this second action is ignored, why??

Thanks

package Gui;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;



public class Gui extends Frame implements WindowListener,ActionListener {
	//TextField text = new TextField(20);
	Button b, c;
        Process p, p2;
        
        
	//private int numClicks = 0;

	public static void main(String[] args) {
		Gui myWindow = new Gui("My first window");
		myWindow.setSize(350,100);
		myWindow.setVisible(true);
               
	}

	public Gui(String title) {

		super(title);
		setLayout(new FlowLayout());
		addWindowListener(this);
		b = new Button("Open Gmail");
                c = new Button ("Open Google");


		add(b);
                add(c);
		//add(text);
		b.addActionListener(this);
                c.addActionListener(this);

	}

	public void actionPerformed(ActionEvent e)

        {
        try
        {
            Object source=e.getSource();
            if (source==b)
            {
             p = Runtime.getRuntime().exec("cmd /c start https://mail.google.com");
             //p2 = Runtime.getRuntime().exec("cmd / c start https://google.com");
            }

           
            if (source == c)
            {
             p2 = Runtime.getRuntime().exec("cmd / c start https://google.com");
            }






        } 
        catch (IOException ex)
        {
            Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
        }
	}

	public void windowClosing(WindowEvent e) {
		dispose();
		System.exit(0);
	}

	public void windowOpened(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}


}

You have a typo in the command string. Take the space out of "/ c" in "cmd / c start https://google.com"

Thank you so much Ezzaral i ll close this thread immediately

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.