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

Thread: ActionListener not working when button is clicked.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ActionListener not working when button is clicked.

    What I have is a basic window with 2 buttons on it: Get Values and Start. When the 'get values' button is clicked, I want the getInput(); method to run, causing 2 pop up windows to appear, asking for 2 numeric values. I am currently trying to get it so that the getInput(); method runs when I click the button.

    This is my code for the relevant methods so far (the name of the class is mainGui):

    public static boolean getInputs = false;
     
    public mainGui() {
     
    		getValues = new JButton("Get Values");
    		start = new JButton("Start");
     
    		add(start);
    		add(getValues);
     
    	}
     
    public void populateWindow(){
     
    		setLayout(new FlowLayout());
     
    		//Creating a test text area.
    		//JComponent comp = new JTextField();
     
    		getValues.addActionListener(this);
    		start.addActionListener(this);
     
    		//Create content pane
    		mainGui newContentPane = new mainGui();
     
    		newContentPane.setOpaque(true);
    		frame.setContentPane(newContentPane);
     
    	}
     
    public void actionPerformed(ActionEvent e) {
     
    		getInputs = true;
     
    	}

    If you need any more code, then just ask.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    Is the action listener method being called when you click on the button? To see, add a println statement to print a message that shows if it is being called.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    It doesn't appear to be called, so that is probably the problem. I'm wondering what code I need to add in, as I have looked on a few tutorials and my code appears to be no different.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    Can you post a small simple program that compiles, executes and shows the problem?
    What you posted won't compile or execute.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    Oh, did you want the actual program + source code? And I'm sorry, that isn't the entire source code, just the parts of it that were relevant to the problem.

    I'm posting a link to a .zip file that should contain the relevant code and an executable made from just that code.

    http://www.mediafire.com/?q2cvhaulp0fq1ql
    Last edited by JamEngulfer221; April 9th, 2012 at 10:01 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    To do a test, I need a small program that compiles, executes and shows the problem.
    Not the whole program, a special small testing program that shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    Edited my post whilst you were replying. It should be there.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    Please post the code in the forum. Not a link
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    Oh, ok. Sorry, I thought you wanted the compiled file xD.

    mainClass.java:

    public class mainClass {
     
    	static mainGui mgui = new mainGui();
     
    	public static void main(String[] args){
     
    		System.out.println("Program Loaded");
     
    		mgui.createWindow();
     
    	}
     
    }

    mainGui.java:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class mainGui extends JPanel implements ActionListener {
     
    	private static final long serialVersionUID = 1L;
     
    	//Class variables
     
    	static String title = "Auto Clicker";
    	static JFrame frame = new JFrame(title);
     
    	public static boolean getInputs = false;
     
    	protected static JButton getValues;
    	protected JButton start;
     
    	public mainGui() {
     
    		getValues = new JButton("Get Values");
    		start = new JButton("Start");
     
    		add(start);
    		add(getValues);
     
    	}
     
    	public void createWindow(){
     
    		populateWindow();
    		drawWindow();
     
    		System.out.println("Creating Window!");
     
    	}
     
     
    	public static void drawWindow(){
     
    		//Exit on close
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Show the frame
    		int width = 300;
    		int height = 300;
    		frame.setSize(width, height);
    		//frame.pack();
    		frame.setVisible(true);
     
    	}
     
    	public static void getInput() {
     
    		System.out.println("Input was simulated!");
     
    	}
     
    	public void populateWindow(){
     
    		setLayout(new FlowLayout());
     
    		//Creating a test text area.
    		//JComponent comp = new JTextField();
     
    		getValues.addActionListener(this);
    		System.out.println("getValues ActionListener called!");
    		start.addActionListener(this);
    		System.out.println("start ActionListener called!");
     
    		//Create content pane
    		mainGui newContentPane = new mainGui();
     
    		newContentPane.setOpaque(true);
    		frame.setContentPane(newContentPane);
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
     
    		getInputs = true;
    		System.out.println("Action performed!");
     
    	}
     
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    How many instances of the mainGui class do you create?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    Hopefully only one

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    Time to play computer with your program. You need to manually step through the code and see when each statement is executed and what the code is doing.

    As an aid you can use the println statement to print out messages when different methods are executed so you can see when it happens.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    Well, everything seems to actually be loading. Something I noticed that may be something was that mainGui was actually being called twice. Once by mainClass and once in populateWindow();

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    It should only be called once.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    I got it so that it only runs once, but now the buttons don't appear for some reason. I've had a look and the code is in the same order as before, just in one method for simplicity.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    Have you stepped through the code to see what it is doing and in what order?
    The code has several small methods that do a little bit here and a little bit there making it hard to see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    I'll post the updated code that is in one large lump so it's easy to see. I have stepped through the code and as far as I can see, it is in the right order.

    Edited guiMain code:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class mainGui extends JPanel implements ActionListener {
     
    	private static final long serialVersionUID = 1L;
     
    	//Class variables
     
    	static String title = "Auto Clicker";
    	static JFrame frame = new JFrame(title);
     
    	//mainGui newContentPane = new mainGui();
     
    	public static boolean getInputs = false;
    	private static boolean writeButtons = true;
     
    	protected static JButton getValues;
    	protected JButton start;
     
    	public mainGui() {
     
    		if(writeButtons != false){
     
    			getValues = new JButton("Get Values");
    			start = new JButton("Start");
     
    			add(start);
    			add(getValues);
     
    			System.out.println("makeButtons run first time!");
     
     
    		System.out.println("Window is being populated");
     
    		setLayout(new FlowLayout());
     
    		System.out.println("The layout flows now");
     
    		getValues.addActionListener(this);
    		System.out.println("getValues ActionListener called!");
    		start.addActionListener(this);
    		System.out.println("start ActionListener called!");
     
    		//Create content pane
     
    		writeButtons = false;
    		System.out.println("Write buttons is now false!");
     
    		mainGui newContentPane = new mainGui();
    		System.out.println("mainGui content pane is created");
     
    		newContentPane.setOpaque(true);
    		System.out.println("The content pane is opaque");
    		frame.setContentPane(newContentPane);
    		System.out.println("The content is paned");
     
    		//Exit on close
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//Show the frame
    		int width = 300;
    		int height = 300;
    		frame.setSize(width, height);
    		//frame.pack();
    		frame.setVisible(true);
    		System.out.println("Window visible");
    		System.out.println("Drawing Window");
     
    		} else {
    			System.out.println("Run second time avoided");
    		}
     
    	}
    	public static void getInput() {
     
    		System.out.println("Input was simulated");
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
     
    		getInputs = true;
    		System.out.println("Action performed!");
     
    	}
     
    }
    Last edited by JamEngulfer221; April 10th, 2012 at 06:57 AM.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    Why do you create a new MainGui object in the MainGui class's constructor?

    The code you posted will not execute. It does not have a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener not working when button is clicked.

    By the way, that code was in addition to the mainClass class code I posted earlier.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ActionListener not working when button is clicked.

    I see that the code will create 2 instances of the mainGui class. Why not just create and use one instance?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. When a button is clicked: get my Data!!!
    By aStudentofJava in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 15th, 2012, 08:45 AM
  2. Calculate Button Not Working
    By czerenborg in forum AWT / Java Swing
    Replies: 8
    Last Post: September 8th, 2011, 09:25 PM
  3. Help with button actionlistener
    By umerahmad in forum Java Theory & Questions
    Replies: 9
    Last Post: August 25th, 2011, 07:21 AM
  4. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM
  5. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM