Setting a button - java

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).

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.

Fast double key pressed on Jbutton leads results in same operation twice

1> I have a JButton in Jframe.
2> The click of JButton opens new instance of another JFrame.
The problem is when a Key is pressed very fast on the above Jbutton .Two instances of the same JFrame opens up.
I have to open these frames. I knows there are other options also not using the Jframes as I read.
I managed to solve this problem for Doulbl click of Mouce by setMultiClickThreshHold('time in miliseconds'). But it worked only for mouse.
Tried some other stuffs which I got in google, But none worked.
Is there any other way to solve this issue?
For full control of how often/quickly again an Action is triggered, implement it to disable itself in its actionPerformed. Something like:
singlePerform = new AbstractAction("DoSomthing") {
#Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
doSomething();
}
};
JButton button = new JButton(singlePerform);
When it's safe for doSomething to be triggered again, simply re-enable the Action:
singlePerform.setEnabled(true);

change JPanel after clicking on a button

I'm building simple GUI for my app. I have couple of JPanels. I want to display them depending on action that was performed by clicking on a JButton. How can I disable one JPanel and enable another one ?
Couple of details. I have a class with JFrame where I'm building starting gui. Where I have buttons and some text. Clicking on one of the buttons should change the view in this JFrame
my button definition
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnStart.setBounds(10, 11, 110, 23);
contentPane.add(btnStart);
// edit
I've found the problem. buttons were in static method
Simple as:
jframe.setContentPane(your_new_panel);
jframe.invalidate();
jframe.validate();
You may want to use CardLayout.
Or you can simple remove the oldpanel and add new panel:
contentPane.remove(oldPanel);
contentPane.add(newPanel);
First remove the jPanel and add the new jPanel. Then use validate to perform relayout.
jFrame.remove(jPanelOld);
jFrame.add(jPanelNew);
jFrame.validate();

Getting Input using KeyBindings

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.

Categories