How to define the command for a button with a variable - java

At the moment I'm trying to figure out how to make a method in my java program set what a button in my project does when is is clicked.
I was thinking about something like
myButton.addClickHandler(event -> command);
where `command´ is a String variable.
Is this even possible? I am thankful for every response!

Can a button itself access the information and check what needs to be done?
If so, you can always add an ActionListener and implement its method actionPerfomed which is going to be called when button is pressed.

Related

Java - One ActionListener for multiple JButtons

I'm writing a little Java Application. I have multiple JButtons. The code for each button is exactly the same, thus I want only one ActionListener. But in that ActionListener I need to call "setText()" for the corresponding button, which was clicked. Is that possible? How would I achieve this?
I tried the following:
private void btnClicked(java.awt.event.ActionEvent evt) {
(JButton)evt.setText("Hello");
}
But that doesn't work - it says "Cannot find symbol".
Thanks in advance ;)
(JButton)evt.setText("Hello");
The ActionEvent class doesn't have a getText() method.
You need to invoke the getSource() method to access the button, then you can invoke the getText() method of the button.
I always like to do it the long way so I don't make mistakes:
JButton button = (JButton)evt.getSource();
button.setText( "Hello" );
but the short way would be:
((JButton)evt.getSource()).setText("Hello");

equivalent of getch() for Android

Is there an equivalent for C's getch() in Android or Java? I want the execution to stop until the user does some action, like tap on the screen or maybe press one of the hardware buttons like volume control or whatever is available. Another option would be to show a modal message box window, which does not let the program continue until the user presses on OK. Is it possible to do this in a simple way in Android? What is the simplest way to get something equivalent to getch() function in android?
I need to be able to use this in a Thread as well
There's no getch() equivalent function/method in java.
You must event handlers to do that.
Like having click handler for button,Which let you to some stuff on onClick() method,Once you click the button.
This example might helpful:Button Click Listeners in Android
There's an event listener for android like onCLickListener. Then you can also used Jdialog then set its .isEditable value to false like dialog.isEditable(false);

Java:Click on every JButton from container

I have a container that has only JButtons in it and I want to click on all the buttons from code so that I don't click on them manually from the interface, how do I do that?
You can actually call a method called doClick() to do this
http://docs.oracle.com/javase/6/docs/api/javax/swing/AbstractButton.html#doClick()
Basically, you should be able to do something like:
myButton.doClick()
and if you have an ActionListener attached to the button it will act as if the user clicked the button.
You can call clickListener method explicitly. Or write a method like clickListener and call it.

JButton .doClick() without doing anything?

Is it possible to simulate a click on a button, similar to doClick() but just graphical simulate it, not generate any ActionEvent´s. If know that i can extend the class, do my own doClick with a simple if-statement. But is it any other possibility?
I want to this because I have a button that the user can press, but sometimes the computer (it s in a game) "presses" the button. All the logic is done in another thread, I just wanna display it for the user.
if you dig into the details in the look and feel you're using, you might be able to see how it detects and paints the button in its "down" state, and then simulate that?
or you could extend/implement ButtonModel, and mess with the setPressed/isPressed state?
I think the simplest solutions is to just check the model in the ActionListener.
I don't see a way to distinguish between the two; the model is oblivious as to who calls its methods. Instead, you could save all the listeners, invoke doClick(), and restore the listeners. It looks like you would have to check action, change and item listeners.
If you want to click on the button try this:
try
{
robot = new Robot();
robot.mouseMove(0,500);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}catch(Exception e){}
But maybe you will prefer to simply disable the button when the user shouldn't click it
Button.setEnabled(false);
You can set whether it's enabled or not, so use "buttonName.setEnabled(false);"

calling actionPerformed() of a button explicitly

I have one class where there are certain swing components.
In second class (as per requirement), I need to simulate the button click event ultimately calling the actionPerformed(ActionEvent ae). This button is in the first class(described in the first line).
How do I do so?
I have tried to go through the fireActionPerformed(ActionEvent ae) . However, I am unable to come to a solution.
A small snippet (as an example) would be very much helpful.
EDIT
Note: I also need to disable the button, when I click it or simulate its click.
Pardon me if this has already been asked before. Though I have searched for similar question before.
Thanks and Regards.
JButton has doClick() method but it's better to define one doMyAction() method and call it from both places.

Categories

Resources