Code execution from Button without ActionListener - java

So i have a button in my program (made with java.awt.Button) and I want to execute some code when i press the button. But i don't want to do this with an ActionListener. Is there a way to avoid the ActionListener?

You could override the processActionEvent(ActionEvent e) method, to execute the code.
Button btn = new Button("No Listener Button") {
void processActionEvent(ActionEvent e) {
// "some code" here
}
};
Note that doing this will create a new anonymous class for every button you create this way, so it is not really a Good Idea™. But it will do what you are asking; buyer beware.

Related

Waiting for a click input after a button press Java Fx

Im having some issues trying to wait for an input after clicking a button.
With my team, we are making a card game, in which cards attack one another, the problem is that i don't know how to, after a button is clicked, make the event handler wait for the user to click another button.
The code looks like this:
private Button attackingButton(){
Button b1 = new Button();
b1.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent event){
//Here i want the user to press another button and, depending which one he
//pressed, asing a variable
Card aCard = //The card that the button pressed has inside
}
}
That's just it, you don't make the handler wait. Instead you change the behavior of the handler depending on the state of the object. If the object is in the "user has not pressed the first button yet" state, the handler does one thing. If the object is in the "user has previously pressed the first button", then the handler does something else. Your handler should query the state of the object's instance fields to determine this state.
e.g.,
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event){
// may need to use boolean fields or .equals(...) method......
if (someStateField == someValue) {
doBehavior1();
} else {
doBehavior2();
}
}
}

Make button available after certain time?

So I have a method to create a button. I'd like that button to be activated every 120 (2 min) seconds. Once the button is clicked, the timer is reset.
addButton( new RedButton(TXT_SATURNUP) {
#Override
public void onClick() {
Game.instance.showAd();
Game.instance.wasAnswerCorrect();
}
});
If possible, I'd also like to be able to have a log message. Say...
if ( timerFinished ) {
// log message to screen informing user
} else {
// Button cannot be clicked at this time
}
You can use a Timer. You will have to set it up with an ActionListener that enables your button, and you button click will have to reset your timer.
Given the interconnections I would make a new button class using the template below:
class TimedButton extends JButton implements ActionListener {
private Timer T;
// Override constructors to set up timer
// and disable button (if/as desired)
public void onClick(){
super.onClick();
setEnabled(false);
T.restart();
}
public void actionPerformed(ActionEvent e){
setEnabled(true);
}
}
Then you need a method to start the timer externally, or a way to start the timer internally. You might want to avoid doing this in your constructor, as the timer may run out before you make the button is visible. It all just depends on when the button is first available.
As for the logging you can add it in the above methods or use isEnabled().

JavaFX bind buttons to eachother

Not to sure if my title question is correct.
What I'm trying to achieve is to have one button create a Vehicle object. Then have a different button call the method embark (which will just update some fields).
So in general:
One button to create an instance of the object Vehicle.
A second button to call a method on this instance.
btnCar.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
Vehicle C = new Car(amountPass, "hej", "hej");
}
});
btnEmbark.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
ferry.embark(C);
}
});
Thanks!
There are several ways to that, the first that comes to my mind is with JavaFX properties:
ObjectProperty<Object> object = new SimpleObjectProperty<>();
Button button1 = new Button("create");
button1.setOnAction(ev -> object.set(new Object()));
Button button2 = new Button("magic");
button2.setOnAction(ev -> object.get().hashCode());
button2.disableProperty().bind(Bindings.isNull(object));
With Bindings we ensure that the second button can only be fired when the custom object has already been created and stored.
Another way would be to write a subclass of Button doing the communication with another Button. That is a matter of personal taste.

Can I call ActionPerformed method from an Event Handler class for JButton?

I have a JButton titled "select"
In the class that creates that JButton and other classes, I want to use an if condition with ActionPerformed method.
Something like(pseudo-code)
if(_selectListener.actionPerformed(ActionEvent)) { //i.e., if select Button is clicked,
//do something
}
Is this possible?
I want to call this method because I have to handle a situation in which a player should be able to choose something by clicking "select" button, or another "scroll" button, and I want to control it using something similar to a bunch of if statements like the one above.
If it is possible, what is the syntax for it? What is the argument ActionEvent?
Thank you!
The easiest and cleanest way is to add a dedicated, specific action listener to each button. That way, when the actionPerformed() method is called, you're sure that the associated button has been clicked, and don't need to test which button has been clicked:
selectButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// handle click on select button
}
});
scrollButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// handle click on scroll button
}
});
Another way is to use a common ActionListener, and use the getSource() method of ActionEvent to know which component triggered the event. Compare the result with each potential button to determine which is the one that has been clicked:
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == selectButton) {
// handle click on select button
}
else if (e.getSource() == scrollButton) {
// handle click on scroll button
}
}
What is the argument ActionEvent?
The answer is in the documentation. Read it.
no you cant call, if needs boolean expression/value, but this method returns void.

Java ActionListener buttonPress() restriction

Is there a way to restrict this button to only being impressed once? The reason I ask is because for some reasons every time the button is pressed it disrupts the rest of my code. So in effort to save a massive amount of time debugging, it would be much easier to just somehow restrict the number of times it can be pressed. Thanks in advance.
ActionListener pushButton = new buttonPress();
start.addActionListener(pushButton);
To prevent clicking a button you can use JButton.setEnabled(false). So you could do this as the first statement in your ActionListener.
An alternative would be to set a flag in your ActionListener like so:
final ActionListener pushButton = new ActionListener()
{
private boolean clicked;
public void actionPerformed(final ActionEvent e)
{
if(clicked)
{
JOptionPane.showMessageDialog(null, "Action already started");
return;
}
clicked = true;
// ... rest of the action to do ...
}
}
Note that you should not execute long running tasks in your event handler, see design considerations to keep in mind when implementing event handlers in The Java Tutorials.

Categories

Resources