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.
Related
I have a continous Action which I can call by pressing down a JButton b1. Currently I have added a MouseListener to b1 and I control the continous nature of this action by calling StartAction() in MousePressed() and StopAction() in MouseReleased(), if the source of the mouse event happens to be b1.
Now I want to be able to trigger the button by keyboard as well. One would usually do that by ActionListeners. I can do:
if (source.equals(b1)) {
startAction();
stopAction();
}
Only problem is, this way a mouse click would trigger both the ActionListener and the mouseListener. I thought I would try setting the action listener as:
if(source.equals(b1) && !(e instanceof MouseEvent))
{
...
}
But that gives me: inconvertible types cannot cast MouseEvent to ActionEvent.
So Question is: is there any way of knowing if an action event was a mouse click or an Keyboard trigger? Should I use another type of Buttons?
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 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
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 read that a JButton implements ItemSelectable and into documentation it has the method addItemListener so I can argue that it can generate an ItemEvent... but when I register with a JButton (but also for a JMenuItem) that interface the event is not rised?
Why?
I understand that if into docs is reported that a component has an add....Listener it means that it support that event... but for experience isn't often so...
What's the truth?
There is a difference between a button being "pressed" (which fires an ActionEvent) and a button being "selected" (which fires the ItemEvent). By default a JButton is backed by a javax.swing.DefaultButtonModel. If you look at the setPressed and setSelected methods in default button model you'll see the code that fires the different events.
So if you programmically call JButton.setSelected, your item listener will be fired. If you click the button, you'll only get the action event.
Note also that with a JButton (unlike, say, a JToggleButton) you probably won't see much visually when it is selected.