Getting Input using KeyBindings - java

I have a program which I want to use keybindings:
// Imports..
public class Test{
JButton button = new JButton();
Test(){
//...
button.getInputMap().put(KeyStroke.getKeyStroke("A"), "A");
button.getActionMap().put("Action", action);
//...
}
}
Now how do I make the button respond when it is clicked?
Is it like KeyListeners where I have an actionPerformed method?

Now how do I make the button respond when it is clicked?
from KeyBindings is there only one way
button.doClick();
then this code line to invoke ActionListener or Swing Action added to the JButton

Is it like KeyListeners where I have an actionPerformed method?
A KeyListener has no actionPerformed method! The solution is to add an ActionListener, or as #mKorbel points out, create the button using an Action.

Related

Locking JButton to prevent multiple SwingWorkers

I cant seem to find a way to prevent a SwingWorker from being called twice on a double click.
The issue is that simply setting the JButton to setEnabled(false) doesn't prevent someone from double clicking fast enough to run it more than once.
startButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
makeItSo();
}
});
private void makeItSo () {
startButton.setEnabled(false);
myWorker myW = new myWorker();
myW.execute(); // Executes allot of work. But errors if this is running more than once.
}
Don't use a MouseListener for buttons, you should be using an ActionListener
See How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener for more details
Use ActionListener instead of MouseListener
why you use ActionListener, An ActionListener is used to handle the logical click of a button.
A click happens
- when the mouse is pressed then released on a button,
- when the keyboard shortcut of that button is used,
- when the button has the focus and the space bar is pressed,
- when the button is the default button and Enter is pressed,
- when the button's click() method is called programmatically
A MouseListener only handles low-level mouse events.

When it's called actionPerformed?

When I add a object o1 to a button with addActionListener(), for what actions is the actionPerformed() of o1 called? This is available for JTextField?
actionPerformed is called when user performs any operation on that swing component. ActionListner can be added every Swing component. So yes you can add it on the JTextField. But it gets called only in case if somebody press enter key on JTextField. For other actions you need to add other listners such as DocumentListner.
actionPerformed(ActionEvent e) is a abstract method of ActionListener interface. You should add it following way.
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//............
}
});
Is this available for JtextField?
Yes, this is available for JTextField.
For a JButton the ActionListener is called when the button is clicked.
The ActionListener of a JTextField is fired when Enter is pressed.
This is explained in the documentation.

adding action listener to a button in netbeans

How to add an action listener to a button in netbeans
I tried to do that, but I don't know how
button.addactionlistener(null);
First you need to implement action listener in your class.
public class YourProject extends JFrame implements ActionListener{
yourbutton.addActionListener(this);
The you need to add the override method.
I hope this helps you.
YOu can also add by another way
JButton jb= new JButton("ok");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{System.out.println("You have clicked ok button");
}
});

What does a addActionListener do?

I have the following code:
JButton button = new JButton("Clear");
button.addActionListener(this);
As far as I understand, I create a button on which it is written "Clear". Then I have to associate an action with this button (what will happen if the button is pressed) and it is done by addActionListener. Is it right?
But what I do not understand is where the action is specified. The press of the button should clear text area and, as far as I understand, there should be somewhere a code which clear the text area. But in the given example there is only "this" in the arguments of the addActionListener().
How does the program know that it should clear the text area when the button is pressed?
If it is needed, the full code is given here.
An ActionListener is a callback mechanism. Whenever a control it is added to fires an ActionEvent, the public void actionPerformed(ActionEvent e) method will be invoked.
What I do not understand is where the actionPerformed is called. I see that it is defined within the class but there is no place where this method is called.
This is called by the internal mechanisms of the UI component. Conceptually, you can think of the code looking a bit like this:
public class Button {
private final List<ActionListener> listeners = new ArrayList<ActionListener>();
public void addActionListener(ActionListener l) {
listeners.add(l);
}
public void click() {
ActionEvent event = new ActionEvent(this, 0, "click");
for (ActionListener l : listeners) {
l.actionPerformed(event);
}
}
}
Each JButton, has an EventListenerList. Calling addActionListener(this) adds your ActionListener, a callback function named actionPerformed(), to the list. You can see an outline of the code that calls your method here. The actual fireActionPerformed() method is in AbstractButton. A JButton is a JComponent, which you can see listed among the various classes that use the event listener list mechanism.
You call button.addActionListener( this ), because this implements the interface ActionListener. When the button is clicked, the method actionPerformed(ActionEvent e) (defined by the interface and implemented by your class) is called.
Basically, the mechanism of UI event handling is the JVM queue's events, and each type of event has its subscribers. When an event is fired, like the button is clicked, the JVM will accordingly delegate the processing to the event's subscriber. And this subscriber class has to define the method, or, event handler, to process the event.
In your case, when calling
button.addActionListener(this);
the code actually subscribes this KeyEventDemo instance to the event of type click. Then, when the button is clicked, the KeyEventDemo's method of actionPerformed will be triggered.

Setting a button

I have 2 buttons. One is "add". The other is "cancel". In the cancel button action, I want to write that until the add method is not called, this button do nothing. How can I do that?
Disable the cancel button initially. Then, in the add action listener, enable the cancel button.
JButton add = new JButton("Add");
JButton cancel = new JButton("Cancel");
cancel.setEnabled(false);
// Then something along these lines...
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
cancel.setEnabled(true);
}
});
NOTE: I haven't used Swing in a while, so my syntax could be off...
When you initialize your app, you could call setEnabled(false) on your cancel JButton. Then in your handler for your add JButton, you could call setEnabled(true) on the cancel JButton. Here is a demo from Sun's Swing Tutorial for disabling/enabling JButtons (source code available at link).

Categories