I am making a maze program in Java which consists of a grid of MazeButtons which extend JButton and have a field for State (which is the location of the button and some other information about how the maze should work). Another class, MazeFrame, extends JFrame and implements ActionListener. When I construct the GUI in the setup class, I add the MazeFrame ActionListener to each button. I want the actionPerformed method in MazeFrame to be able to check to see if the action the user attempted is allowed, but in order to do that I need to know which button was clicked.
How can you know which button called a given actionPerformed method?
Use the getSource method on the event and it will return the object that fired it
Related
I have created a Jframe with a JButton for a specific action.
Now please I want a situation whereby anytime I hit my enter key on my keyboard it will perform the action I have coded in my Jbutton.
My Jframe was designed with Netbeans 7.3.
Now please I want a situation whereby anytime I hit my enter key on my keyboard it will perform the action I have coded in my Jbutton.
Make your JButton the default button of the JRootPane.
You do this by calling setDefaultButton(myJButton) on the root pane.
And you can get the JRootPane by calling getRootPane() on your JFrame.
Note that if you want this action when a JTextField has focus, then the solution is different; here you'll want to add the same ActionListener given to the JButton to the JTextField.
Implement ActionListener Interface
The listener interface for receiving action events. The class that is
interested in processing an action event implements this interface,
and the object created with that class is registered with a component,
using the component's addActionListener method. When the action event
occurs, that object's actionPerformed method is invoked.
I know what interface is and how to build/use one. Lets take the ActionListener interface for example.
My question is what happens after I click on a button, which class calls the actionPerformed method?what is the process from the part that I click the button to the part that the actionPerformed is executed?
The JButton calls the ActionListener.
Internally, it listens to keyboard and mouse events. When it receives a mouse click or a keypress which means "click the button", it creates an ActionEvent instance, loops through all the ActionListener instances that have been added to itself, and calls each one with the ActionEvent as argument.
I'm busy with an application which runs in a JFrame (using BorderLayout), and has:
A status bar at the bottom
Fixed Buttons on the left
Menu on top (which change relating to specific function),
Buttons on the right (which change relating to specific function).
I'm setting it up that for every button on the left, a separate class file will be created for its functions and procedures. At this stage it's about 8 extra classes.
How to go about changing the values on the buttons and menu for each class, by that specific class?
If you're creating a separate class for each button, I would make sure that the class extends JButton. By doing this, to change the values on the buttons, you still have access to all the normal JButton methods like setText();
So hopefully your custom button classes would look something like this...
public class MyButton1 extends JButton {
public MyButton1(String label){
super(label);
}
// your other methods go here
}
To create the button you would do this, which would set the button label...
MyButton1 button1 = new MyButton1("Hello");
And you can still call JButton methods if you want to change the label later, like this...
button1.setText("Goodbye");
I have an actionlistener that fires when I click a button lets call it box, I have another actionlistener for another button call it restart. What I am trying to do is when I click a box button besides calling its own actionlistener inside of that actionlistener I want to call the actionlistener for restart when a certain condition is met. Any ideas?
You could invoke
restart.doClick();
which will click the button which in turn will invoke the ActionListener.
Otherwise you can invoke the ActionListener directly by doing:
restartActionListener.actionPerformed( new ActionEvent(...) );
I'm trying to create a simple GUI with a menu bar at the top of the jwindow using java. I'm able to get the menu bar to show and I'm able to implement the actionlistener class, however, how do I get the ok and ye tab, once clicked, to print out information? In other words how do I use the actionlistener class to create an action to print information out once ok or ye has been clicked by the user?
// Creates a menu bar
z.setJMenuBar(b);
File.add(ok);
File.add(ye);
Help.add(laws);
bar.add(HELP);
bar.add(FILE);
// Listener objects created
ok.addActionListener(this);
ye.addActionListener(this);
if you are adding this like action listener the you should implement abstract method actionPerformed. You can do everything inside that method.