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.
Related
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");
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.
By using doClick() method, the action of a button can be triggered. Is there such a way to change combobox value when a button is clicked in java? Thank You.
As you didn't state the graphical framework I assume you use Swing, as it contains a doClick function.
The doClick() function is only used to emulate a button click programmatically, for example in testing. This will fire all actions that are normally associated with that button, however you need to register those yourself, using a listener.
Is there a way to add/override the action event of THIS button ? I couldn't find anywhere how to acces this button and override it's action. In my case I need to do this because I need to save the resources before I exit my window, and if I press the x button, It will exit automatically.
On the JFrame:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then add a WindowsListener and in the windowClosing(WindowEvent event) methods do your job and then call dispose()
I'd use a shut-down hook instead that way it'll save even if you close the app via another method.
Looky here
You can try using a WindowListener interface. If I remember correctly, it should allow you to do things when something on the frame is clicked on(like exiting).
See http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowListener.html
I am writing a GUI, and I have made custom buttons using null layouts, however whenever I am adding a action listener each time the button is pressed it is reloading the entire image, making it look different, is it possible to use an action listener several times without necessarily doing a new one?
You don't need to add a new ActionListener every time you want to handle an action. Just add one and it will keep working, everytime the button is pressed the actionPerformed method is called.. and I actually don't get why you add another one everytime (they will coexist so the actionPerformed is invoked many times)