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 3 of 3

Thread: Can't use button.addActionListener(this); ?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't use button.addActionListener(this); ?

    So.. In a nutshell, I am working on a basic GUI project. I have attempted to break everything down in to small factored down methods inside my class.
    I am able to build my GUI this way, but now having issues when trying to create my ActionListener functionality. Here is the code I have below


    package jLottery;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Event;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
     
    import javax.swing.*;
     
    public class jLottery {
    	//create objects and vars
    	static JFrame frame = new JFrame("The National Lottery");
    	static Container con = frame.getContentPane();
    	Random RandomNum = new Random();
     
    	int matches = 0;
    	int[] WinningNums = winningNUM();
     
    	//Create Window
    	public void CreateFrame (){
    		frame.setSize(1200,800);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setAlwaysOnTop(true);
    		frame.setResizable(false);
    	}
     
    	//Set Up Content Container
    	public void CreateContainer(){
    		con.setBackground(new Color(56,48,44));
    		con.setLayout(new GridLayout(0,5));
    	}
     
    	//Add a Label
    	public void AddHead(String i) {
    	    JLabel label = new JLabel(i);
    	    label.setFont(new Font("Sans-Serif", Font.BOLD, 20));
    	    label.setForeground(new Color(225,98,25));
    	    con.add(label);
    	}
     
    	public void AddSubHead(String i) {
    	    JLabel label = new JLabel(i);
    	    label.setFont(new Font("Sans-Serif", Font.PLAIN, 20));
    	    label.setForeground(new Color(225,98,25));
    	    con.add(label);
    	}
     
     
    	//function to create check boxes and add them to frame ( complete with action listeners )
    	public void AddCheckButton(int i){
    		String ButtonNum = Integer.toString(i);
    		JCheckBox select = new JCheckBox(ButtonNum);
    		select.setBackground(new Color(56,48,44));
    		select.setForeground(new Color(225,255,255));
     
    		con.add(select);
    		select.addActionListener(this);		
    	}
     
    	public void AddButton(String i){
    		JButton button = new JButton(i);
    		button.setBackground(new Color(225, 98, 25));
    		button.setForeground(new Color(255, 255, 255));
    		button.setFont(new Font("Sans-Serif", Font.BOLD, 20));
     
    		con.add(button);
    		button.addActionListener(this);
    	}

    Here is my implementation code:

    package jLottery;
     
    import java.util.Scanner;
     
    public class jMain {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    	jLottery lottery = new jLottery();
    		//create our main window and container	
    		lottery.CreateFrame();
    		lottery.CreateContainer();
     
    		//create our Header
    		lottery.AddHead("The Lottery: ");
    		//create our subHeader
    		lottery.AddSubHead("Pick Six ");
     
    		lottery.AddHead("");//spacer
    		lottery.AddHead("");//spacer
    		lottery.AddHead("");//spacer
     
    		//add our check-buttons
    		for (int i=0; i<35; i++){
    			lottery.AddCheckButton((i+1));
    		}
     
    		//add our submit button
    		lottery.AddButton("Play!");
    }



    The main issue I am having is that it is not letting me use:
    select.addActionListener(this);
    or
    button.addActionListener(this);

    On a side note, my layout is very inconsistent, and appears to often show partial amounts of components when ran.


    Is my breaking down/factoring approach all wrong? I am just trying to avoid a huge long class (as would be the case with all the formatting and options etc)

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Can't use button.addActionListener(this); ?

    To add an ActionListener to a JButton (or other component with an addActionListener method), the must add an object that implements the ActionListener interface. I recommend reading the following:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Further, I recommend reading about code conventions
    Code Conventions for the Java Programming Language
    Methods should start with lowercase letters, classes with uppercase letters. One might consider this nit-picky, but not following conventions makes it that much harder to read code.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't use button.addActionListener(this); ?

    Quote Originally Posted by copeg View Post
    To add an ActionListener to a JButton (or other component with an addActionListener method), the must add an object that implements the ActionListener interface. I recommend reading the following:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Further, I recommend reading about code conventions
    Code Conventions for the Java Programming Language
    Methods should start with lowercase letters, classes with uppercase letters. One might consider this nit-picky, but not following conventions makes it that much harder to read code.
    Thank you so much. I read through that documentation. Just simply had to add the "implements" to my class and I am now able to trigger action events
    As for the conventions, I am a little sloppy with that agreed, I am still a student though so still learning. Also, I was using a lot of C# last semester, that uses Camel Caps for method names, so I guess it is an issue of adjusting.

Similar Threads

  1. Button disable
    By deependeroracle in forum AWT / Java Swing
    Replies: 6
    Last Post: April 22nd, 2012, 09:06 AM
  2. Not sure how to deal with addActionListener! :3
    By Asteroid555 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2011, 01:57 PM
  3. Reset button.
    By ish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 07:57 AM
  4. disable a button ?
    By dime111 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 28th, 2011, 02:58 PM
  5. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM