Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with actionlistener

    I'm designing a simple log in page, im pretty sure ive got most of the things in order, however.. the actionlisteners on my buttons dont appear to be working, and i have no idea why. If someone could help me solve this dilemma id be most grateful.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class LogInGUI extends JFrame implements ActionListener {
    	JButton button_1;
    	JButton exitButton;
    	JLabel welcome;
    	JTextField username;
    	JLabel label_1;
    	JLabel label_2;
    	JPasswordField password;
    	JButton logInButton;
    	JLabel info;
    
    	public LogInGUI() {
    		loginLayout customLayout = new loginLayout();
    
    		getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
    		getContentPane().setLayout(customLayout);
    
    		button_1 = new JButton("Create New Portfolio");
    		getContentPane().add(button_1);
    
    		exitButton = new JButton("EXIT");
    		getContentPane().add(exitButton);
    		exitButton.addActionListener(this);
    
    		welcome = new JLabel("Welcome the GMS Stock Portfolio Manager");
    		getContentPane().add(welcome);
    
    		username = new JTextField("");
    		getContentPane().add(username);
    		username.addActionListener(this);
    
    		label_1 = new JLabel("Username");
    		getContentPane().add(label_1);
    
    		label_2 = new JLabel("Password");
    		getContentPane().add(label_2);
    
    		password = new JPasswordField();
    		getContentPane().add(password);
    		password.addActionListener(this);
    
    
    		logInButton = new JButton("Log In");
    		getContentPane().add(logInButton);
    		logInButton.addActionListener(this);
    
    		info = new JLabel("Please Enter your Log In details or Create a new Account");
    		getContentPane().add(info);
    
    		setSize(getPreferredSize());
    
    		addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
    	public void actionPerformed(ActionEvent e) {
    		if (e.getActionCommand().equals("logInButton")){
    			System.out.print("test");
    			//	String pw = getPassword();
    			//	String un = username.getText();
    
    			//	System.out.print(un);
    			//	try {
    			//			UserManagement.logIn(un, pw);
    			//	} catch (IOException e1) {
    			//		// TODO Auto-generated catch block
    			//			e1.printStackTrace();
    			//		}
    		}
    
    
    
    	}
    
    	public static void main(String args[]) {
    		LogInGUI window = new LogInGUI();
    
    		window.setTitle("login");
    		window.pack();
    		window.show();
    	}
    
    
    
    
    	private String getPassword() {
    		char[] input = password.getPassword();
    		String password = input.toString();
    		return password;
    	}
    
    
    
    	class loginLayout implements LayoutManager {
    
    		public loginLayout() {
    		}
    
    		public void addLayoutComponent(String name, Component comp) {
    		}
    
    		public void removeLayoutComponent(Component comp) {
    		}
    
    		public Dimension preferredLayoutSize(Container parent) {
    			Dimension dim = new Dimension(0, 0);
    
    			Insets insets = parent.getInsets();
    			dim.width = 773 + insets.left + insets.right;
    			dim.height = 575 + insets.top + insets.bottom;
    
    			return dim;
    		}
    
    		public Dimension minimumLayoutSize(Container parent) {
    			Dimension dim = new Dimension(0, 0);
    			return dim;
    		}
    
    		public void layoutContainer(Container parent) {
    			Insets insets = parent.getInsets();
    
    			Component c;
    			c = parent.getComponent(0);
    			if (c.isVisible()) {c.setBounds(insets.left+152,insets.top+432,144,64);}
    			c = parent.getComponent(1);
    			if (c.isVisible()) {c.setBounds(insets.left+424,insets.top+432,144,64);}
    			c = parent.getComponent(2);
    			if (c.isVisible()) {c.setBounds(insets.left+256,insets.top+8,224,64);}
    			c = parent.getComponent(3);
    			if (c.isVisible()) {c.setBounds(insets.left+328,insets.top+200,184,24);}
    			c = parent.getComponent(4);
    			if (c.isVisible()) {c.setBounds(insets.left+192,insets.top+200,96,24);}
    			c = parent.getComponent(5);
    			if (c.isVisible()) {c.setBounds(insets.left+192,insets.top+240,96,24);}
    			c = parent.getComponent(6);
    			if (c.isVisible()) {c.setBounds(insets.left+328,insets.top+240,184,24);}
    			c = parent.getComponent(7);
    			if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+304,136,48);}
    			c = parent.getComponent(8);
    			if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+104,152,48);}
    		}
    	}
    }

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    replace your actionPerformed method with this one:
    Code:
    public void actionPerformed(ActionEvent e) 
    	{
    		//if (e.getActionCommand().equals("logInButton"))
    		if(e.getSource().equals(logInButton))
    		{
    			System.out.print("test");
    			//	String pw = getPassword();
    			//	String un = username.getText();
    
    			//	System.out.print(un);
    			//	try {
    			//			UserManagement.logIn(un, pw);
    			//	} catch (IOException e1) {
    			//		// TODO Auto-generated catch block
    			//			e1.printStackTrace();
    			//		}
    		}
    		else if(e.getSource().equals(exitButton))
    		{
    			System.exit(0);
    		}
    
    
    
    	}
    Software and cathedrals are much the same - first we build them, then we pray.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •