How to assign the enter key on my keyboard to a JButton - java

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.

Related

Difference between ChangeListener and ItemListener

What is the difference between ChangeListener and ItemListener for JCheckBox and JRadioButton? Both of them work fine when they are selected/deselected.
I know that some components doesn't support ChangeListener like the JComboBox. Other than the reason that ChangeListener or ItemListener work for only some components. Is there any difference between them like when are they generated?
Any answer is appreciated. Thanks in advance.
both listeners for JCheckBox work similarly in that both will fire event upon change in state, whether by clicking or toggle by spacebar or programmatically through doClick() method (Similar to mouse click). One major difference though is that JCheckBox’s itemListener can be fired through setSelected(boolean) method which allows one to fire the event based on desired state whereas others will act only after the state is altered. So why is it important ? Consider when application startup, the GUI needed to configure for defined state, and using setSelected will trigger ItemListener. Note that setSelected is exclusive to ItemListener and has no effect on ActionListener. Do not register both ActionListener and ItemListener as both will be fired, landing the component in a random state
ChangeListener is notyfied when there's any change to the button state. ChangeListener is not notified of what has changed, only that the object has changed. Item listener is only notyfied when an item is selected; by user or setSelected method. It's also not true that ChangeListener is not notyfied when setSelected method is invoked. It is the change of the object state.

Implementing interfaces

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.

Determine which button called an actionPerformed method

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

itemStateChanged on JButton

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.

Swing make a JButton not focussable

I want to make a Java swing button 'not-focussable'.
The button should not receive focus at all, but should be able to receive mouse clicks.
I thought the following options, but these either dont solve my problem completely, or dont seem elegant. Are there any other/better/suggested options?
Move the focus to the next component immediately when the button receives focus (but then what do I do if the button is the only component on the UI other than labels?)
Implement another non-focusable component as a button (a label with mouse events, borders...) (this does not look very elegant to me)
Create a anonymous button implementation that overides the keyboard events so that it does not respond to keyboard events (this does not solve the focus problem, but is somwhat ok for me, since the root of the problem is to avoid accidental keyboard clicks. I will do this only if there are no options at all, but even then prefer option 2)
All Swing components have a setFocusable method to do this:
JButton button = ...
button.setFocusable(false);
Did you try to call the setFocusable() method inherited from java.awt.Component ?
Resources :
Javadoc - Component.isFocusable()
Oracle.com - Focus tutorial
You can implement your own FocusTraversalPolicy (or extend e.g. ContainerOrderFocusTraversalPolicy) with an accept method that just doesn't like your button.
JFrame frame = new JFrame();
... /* create other components */
frame.setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy() {
public boolean accept(Component c) {
return super.accept(c) && c!=iDontLikeYouButton;
}
});

Categories