Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with Action Listener

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Action Listener

    Hi Guys,

    Ok im developing a student record system to enter details etc but im having problems with my action listener recognising my buttons so i can define what method to call.

    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Gui extends JFrame implements ActionListener {
    	Store StudentStore = new Store(200);
     
     
     
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("Test");
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		JPanel panel2 = new JPanel(new GridBagLayout());
    		frame.getContentPane().add(panel2, BorderLayout.NORTH);
    		GridBagConstraints d = new GridBagConstraints();
     
    		}
     
     
    	public Gui() {
     
     
    		JFrame frame = new JFrame("Test");
    		frame.setSize(500,500);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JPanel panel2 = new JPanel(new GridBagLayout());
    		frame.getContentPane().add(panel2, BorderLayout.NORTH);
    		GridBagConstraints d = new GridBagConstraints();
     
    		JLabel label1 = new JLabel("Please Enter Student Detailes Below");
    		d.gridx = 0;
    		d.gridy = 0;
    		d.insets = new Insets(5, 5, 5, 5);
    		panel2.add(label1, d);
     
    		/*JPanel panel3 = new JPanel(new GridBagLayout());
    		frame.getContentPane().add(panel3, BorderLayout.CENTER);
    		GridBagConstraints b = new GridBagConstraints();*/
     
    		/*JPanel panel4 = new JPanel(new GridBagLayout());
    		frame.getContentPane().add(panel4, BorderLayout.EAST);
    		GridBagConstraints h = new GridBagConstraints();*/
     
     
    		JPanel panel1 = new JPanel(new GridBagLayout());
    		frame.getContentPane().add(panel1, BorderLayout.WEST);
    		GridBagConstraints c = new GridBagConstraints();
    		JLabel label2 = new JLabel("Enter Name:");
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label2, c);
     
    		JTextField eName = new JTextField(15);
    		c.gridx = 1;
    		c.gridy = 0;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(eName);
     
    		JLabel label3 = new JLabel("Select Gender:");
    		c.gridx = 0;
    		c.gridy = 1;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label3, c);
     
    		JRadioButton maleButton = new JRadioButton("Male");
    		c.gridx = 1;
    		c.gridy = 1;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(maleButton, c);
     
    		JRadioButton fmaleButton = new JRadioButton("Female");
    		c.gridx = 2;
    		c.gridy = 1;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(fmaleButton, c);
     
    		JLabel label4 = new JLabel("Select Date of Birth:");
    		c.gridx = 0;
    		c.gridy = 2;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label4, c);
     
     
    		JLabel label5 = new JLabel("Enter Email Address:");
    		c.gridx = 0;
    		c.gridy = 3;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label5, c);
     
    		JTextField eMail = new JTextField(15);
    		c.gridx = 1;
    		c.gridy = 3;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(eMail, c);
     
    		JLabel label6 = new JLabel("Enter Student Number:");
    		c.gridx = 0;
    		c.gridy = 4;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label6, c);
     
    		JTextField stuNum = new JTextField(15);
    		c.gridx = 1;
    		c.gridy = 4;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(stuNum, c);
     
    		JLabel label7 = new JLabel("Select Enrolment Date:");
    		c.gridx = 0;
    		c.gridy = 5;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label7, c);
     
    		JLabel label8 = new JLabel("Enter Course Name:");
    		c.gridx = 0;
    		c.gridy = 6;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label8, c);
     
    		JTextField courseName = new JTextField(15);
    		c.gridx = 1;
    		c.gridy = 6;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(courseName, c);
     
    		JLabel label9 = new JLabel("Enter Year/Level:");
    		c.gridx = 0;
    		c.gridy = 7;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(label9, c);
     
    		JTextField eYear = new JTextField(15);
    		c.gridx = 1;
    		c.gridy = 7;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(eYear, c);
     
     
    		String[] dateString = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" };
    		String[] monthString = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    		String[] yearString = {"1970", "1971", "1972", "1973","1974", "1975", "1976", "1977", "1978", "1979", "1980","1981", "1982", "1983","1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993","1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005","2006","2007","2008","2009", "2010", "2011", "2012" };
    		JComboBox dateList = new JComboBox(dateString);
    		c.gridx = 1;
    		c.gridy = 2; 
    		panel1.add(dateList, c);
    		JComboBox monthList = new JComboBox(monthString);
    		c.gridx = 2;
    		c.gridy = 2; 
    		panel1.add(monthList, c);
    		JComboBox yearList = new JComboBox(yearString);
    		c.gridx = 3;
    		c.gridy = 2; 
    		panel1.add(yearList, c);
     
    		String[] dateEString = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" };
    		String[] monthEString = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    		String[] yearEString = {"1970", "1971", "1972", "1973","1974", "1975", "1976", "1977", "1978", "1979", "1980","1981", "1982", "1983","1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993","1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005","2006","2007","2008","2009", "2010", "2011", "2012" };
    		JComboBox dateEList = new JComboBox(dateEString);
    		c.gridx = 1;
    		c.gridy = 5; 
    		panel1.add(dateEList, c);
    		JComboBox monthEList = new JComboBox(monthEString);
    		c.gridx = 2;
    		c.gridy = 5; 
    		panel1.add(monthEList, c);
    		JComboBox yearEList = new JComboBox(yearEString);
    		c.gridx = 3;
    		c.gridy = 5; 
    		panel1.add(yearEList, c);
     
     
     
     
    		JButton enter = new JButton("Enter");
    		c.gridx = 1;
    		c.gridy = 8;
    		c.insets = new Insets(5, 5, 5, 5);
    		enter.addActionListener(this);
    		panel1.add(enter, c);
     
     
    		JButton wipe = new JButton("Clear");
    		wipe.addActionListener(this);
    		c.gridx = 2;
    		c.gridy = 8;
    		c.insets = new Insets(5, 5, 5, 5);
    		panel1.add(wipe, c);
     
     
     
    		JMenuBar menubar = new JMenuBar();
    		frame.setJMenuBar(menubar);
     
    		JMenu file = new JMenu("File");
    		menubar.add(file);
    		JMenuItem New = new JMenuItem("New");
    		JMenuItem save = new JMenuItem("Save");
    		JMenuItem open = new JMenuItem("Open");
    		file.add(New);
    		file.add(save);
    		file.add(open);
     
    		JMenu record = new JMenu("Record");
    		menubar.add(record);
    		JMenuItem add = new JMenuItem("Add");
    		JMenuItem display = new JMenuItem("Display");
    		JMenuItem clear = new JMenuItem("Clear");
    		record.add(add);
    		record.add(display);
    		record.add(clear);
     
     
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == wipe) 
    		{
    		addStudent();
    		}
     
    		if (e.getSource() == enter){
     
     
    		}
    }
    }

    Any help gettin the action listener to work and placing the method in the appropriate place would be greatly appreciated.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Problem with Action Listener

    Your JButtons are declared in the scope of the Gui() method [bad choice of method name, should start with a lowercase letter] so cannot be accessed from another method. To be able to access them in the actionPerformed(...) method, declare them as instance fields.

    You need to brush up on basics before going any further, this has nothing to do with GUI programming. Recommended reading:
    The Java™ Tutorials

    When you master the basics, you might prefer to give each button its own ActionListener in the form of an anonymous inner class. 'Switchboard' type class-level actionPerformed(...) methods quickly grow cumbersome and unwieldy.

    db

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with Action Listener

    To extend from Darryl's advice, another option you may wish to take advantage of is the action command of the ActionEvent

    public void actionPerformed(ActionEvent e){
        String command = e.getActionCommand();
        if ( "Clear".equals(command)){
            //clear 
        }else if{
     
        }
    }

  4. #4
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Action Listener

    Its ok guys thanks for help but i sorted it anyway, the file is not actually called GUI its called controller i just put gui in there as that was my test class. I just needed to initialize the buttons before the method.

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Problem with Action Listener

    To be more exact, you needed to declare the buttons in the class scope.

    db

Similar Threads

  1. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Swing Tutorials
    Replies: 2
    Last Post: March 14th, 2012, 06:49 AM
  2. Key Listener not working in jinternalframe
    By furqankhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 09:48 AM
  3. Key listener help
    By airsim15 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 13th, 2009, 03:15 PM
  4. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM