java: activating a mouse click event - java

I have a java method activated by a mouse click on a button
private void backButtonMouseClicked(java.awt.event.MouseEvent evt) { do stuff }
is there some way to virtually use this method from another method without clicking the mouse on the button?

Or you can just use button.doClick();

Yes, MouseEvent has public constructors, so you simply create one, and call the function with it.

I believe you can say backButtonMouseClicked(new MouseEvent()) to call the method. Or maybe backButtonMouseClicked(null).

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");

Define action listener in custom button class

I'm using javafx to create a GUI for my program, and have created a custom button ComponentButton. This extends javafx.scene.control.Button, the only real difference so far is that the button has an additional member variable. I would like all instances of my button to have an action listener attached to them, the listener would make use of the member variable. As a result I'd like to be able to define the action listener within the class definition of the button. As of yet I've been unable to make this work, I can make an action listener for each instance of the button but as there will be a lot of them I would rather avoid it.
Any ideas or alternative approaches are much appreciated, thanks in advance.
Edit: Answered it
I decided to make the view (where the button was used) implement EventHandler<ActionEvent>. This meant that I could then add the method
#Override
public void handle(final ActionEvent event)
{
//Do stuff
}
This then meant that I could call button.setOnAction(this); to execute the action
I decided to make the view (where the button was used) implement EventHandler<ActionEvent>. This meant that I could then add the method
#Override
public void handle(final ActionEvent event)
{
//Do stuff
}
This then meant that I could call button.setOnAction(this); to execute the action

SWT Keylistener is not triggered

I have a SWT GUI, containing different elements (Text, Buttons, Labels...) which are themselves in different Composites.
I would like to make the navigation easier using some keybindings such as "Alt+c" to call the Cancel Button, "Alt+f" to call the finish button etc... When using a KeyListener on a specific component, the listener is triggered, but it implies that the component has the focus (and this is not very convenient !).
So I tried to register the listener on the shell itself, but the result is the same and nothing is triggered.
How should I proceed in order to get my listener triggered no matters what element is currently focused ?
Any hint would be appreciated.
Thanks for reading.
Edit
Regarding the comments, I tried to add the keylistener recursively to all the composites of the GUI, and it's working. However, I guess there is probably a "clever" way to do it.
You can use the Display addFilter or addListener methods to add a listener which is always called.
Display.getDefault().addListener(SWT.KeyDown, new Listener() {
#Override
public void handleEvent(final Event event) {
// TODO handle event
}
});
These listeners use the lower level Listener interface rather than KeyListener.
addFilter is similar to addListener but is called earlier and can change the event.
The easiest way is to add a event filter to the display:
Here is an example I use to activate a search field when a user types command-F in our main application window.
Display.getCurrent().addFilter(SWT.KeyDown, event -> {
// Only respond to key events for our shell.
if (getShell().equals(Display.getCurrent().getActiveShell())) {
// Activate the focus for our search widget when user types 'f'
// (control-f, command-f, or just f)
if (event.keyCode == 'f') {
if (!searchField.isFocusControl()) {
searchField.setFocus();
}
}
}
});

How to check if and which mouse button is pressed in Swing

How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.

Java add ActionListener to special button

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

Categories

Resources