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):
Code :
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.
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.
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.
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.
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
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.
Re: ActionListener not working when button is clicked.
Edited my post whilst you were replying. It should be there.
Re: ActionListener not working when button is clicked.
Please post the code in the forum. Not a link
Re: ActionListener not working when button is clicked.
Oh, ok. Sorry, I thought you wanted the compiled file xD.
mainClass.java:
Code :
public class mainClass {
static mainGui mgui = new mainGui();
public static void main(String[] args){
System.out.println("Program Loaded");
mgui.createWindow();
}
}
mainGui.java:
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);
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!");
}
}
Re: ActionListener not working when button is clicked.
How many instances of the mainGui class do you create?
Re: ActionListener not working when button is clicked.
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.
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();
Re: ActionListener not working when button is clicked.
It should only be called once.
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.
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.
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:
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!");
}
}
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.
Re: ActionListener not working when button is clicked.
By the way, that code was in addition to the mainClass class code I posted earlier.
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?