I'm trying to test by SWTBot hyperlinks inside my Eclipse's text editor. The problem is that the hyperlinks are shown on demand (an Eclipse feature), meaning - the token changes is revealed as hyperLink only when mouse moves over it + a keyboard key (Ctrl or Alt) is pressed.
How can I simulate in SWTBot the mouse-move together with a key pressed?
When mouse moves over a link, then a MouseEvent is generated. Some MouseMotionListener (or maybe MouseListener) consumes this event and then shows hiperlink for you.
You can simulate this event:
Component source = null; // TODO set up a valid component
MouseEvent event = new MouseEvent(source, MouseEvent.MOUSE_ENTERED, System.currentTimeMillis(), InputEvent.ALT_DOWN_MASK, source.getX(), source.getY(), 0, false);
MouseMotionListener[] mouseMotionListeners = source.getMouseMotionListeners();
if (mouseMotionListeners!= null && mouseMotionListeners.length > 0) {
MouseMotionListener mouseMotionListener = mouseMotionListeners[0];
mouseMotionListener.mouseMoved(event);
}
The InputEvent.ALT_DOWN_MASK in the constructor means that Alt is pressed.
Note that you should define what Component responsible for consuming events in your case.
You can find more information in the tutorial How to Write a Mouse Listener and MouseEvent API
Related
This is a weird question so I'll make my best to explain myself properly.
What I'd like is to trigger an event when a Tab in a TabPane get clicked, and by "clicked" I mean just clicked, not necessarily selected.
I already tried using the selectedProperty of the Tab, but that does call the event only if the Tab is clicked when it's not selected, not even if it's clicked when it's already selected.
The reason why I'm doing this is that I'm trying to make a collapsible tab pane that hides the content of the TabPane if you click again on the opened tab, I've already wrote the code for collapsing the TabPane and that works but... I have no idea on how to get a click event from the tab header.
I've even looked into TabPane source code too hoping that I could find the tab header container but I didn't find it there.
No need for a completely new skin - we can access the header nodes by lookup. Beware: implies relying on implementation details, which might change across versions.
The (undocumented!) style id to look for is ".tab-container" - that's the only child of the TabHeaderSkin (== region for a single tab header). It contains the label, the close button (if any) and the focus marker. This "skin" keeps a reference to its tab in its properties (undocumented, of course ;)
So the basic approach is to
lookup all tab-containers after the skin is installed
for each, register an appropriate mouse handler on its parent
on the respective mouseEvent, do whatever is needed
Note that the listeners have to be removed/added when the list of tabs is modified (not included in the snippet below).
In example code:
/**
* looks up the styled part of tab header and installs a mouseHandler
* which calls the work load method.
*
* #param tabPane
*/
private void installTabHandlers(TabPane tabPane) {
Set<Node> headers = tabPane.lookupAll(".tab-container");
headers.forEach(node -> {
// implementation detail: header of tabContainer is the TabHeaderSkin
Parent parent = node.getParent();
parent.setOnMouseClicked(ev -> handleHeader(parent));
});
}
/**
* Workload for tab.
* #param tabHeaderSkin
*/
private void handleHeader(Node tabHeaderSkin) {
// implementation detail: skin keeps reference to associated Tab
Tab tab = (Tab) tabHeaderSkin.getProperties().get(Tab.class);
System.out.println("do stuff for tab: " + tab.getText());
}
Context: JDK 8 & JavaFX
I have a TextField control that is used in a dialog. It is the first edit control, so it gets the focus when the dialog opens. The dialog has a button configured as the cancel button (Button.setCancelButton(true))
With a plain TextField, if I hit ESC immediately after the dialog opens, the dialog is closed (as expected).
However, once I add a TextFormatter with input filter to the TextField, the ESC keypress appears to be being consumed by the input control and ESC no longer closes the dialog.
The TextFormatter only has an input filter (to restrict the input control to just digits), but the input filter does not get invoked on the ESC keypress - because the content of the field has not changed.
It's a fairly minor issue, but it's annoying not having consistent behaviour, and not being able to just hit ESC to dismiss the dialog. Any ideas on how to ensure that the ESC keypress is propagated/not consumed, so that it is handled by the dialog?
Edit:
My question appears to be a duplicate of this one: Escape from a Number TextField in a JavaFX dialog. Which of course I failed to find despite trawling through Google before posting... TLDR; the TextFormatter class fails to forward the ESC keypress event on.
I think the easiest approach is to avoid trying to “fix” the TextField and TextFormatter, and just add a key listener:
textField.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ESCAPE) {
dialog.setResult(ButtonType.CANCEL);
}
});
If the Dialog is not an Alert (or more precisely, is not a Dialog<ButtonType>), you can locate the button and activate it yourself:
textField.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ESCAPE) {
Button cancelButton = (Button)
dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancelButton.fire();
}
});
I have a requirement that when a certain automation script is run, the browser should show the cursor moving from one field to another as the script moves ahead. I am not sure about what exactly I need to do to get it done. I used the Action class to implement it but it's not working.
Please find the code I have implemented below:
public void MouseHover(WebElement Mouse,WebDriver driver) throws InterruptedException
{
Actions act = new Actions(driver);
act.moveToElement(Mouse).build().perform();
System.out.println("Curser movement Performed Successfully");
}
The java.awt.Robot class can be used to programmatically move the user's mouse (among other things). See: Link.
For example:
Robot r = new Robot();//construct a Robot object for default screen
r.mouseMove(1360, 7);//move mouse to java coords 1360, 7
r.mousePress(InputEvent.BUTTON1_MASK);//press the left mouse button
r.mouseRelease(InputEvent.BUTTON1_MASK);//release the mouse button
I'm working on a simple Java swing app, which adds an icon to the system tray when created. What I'm trying to do is to detect when this icon is single clicked by the user (whether through left click or right click), There's no popup menu, I just want the app to be restored when the icon is clicked.
This is the code I'm using:
SystemTray tray = SystemTray.getSystemTray();
Image icon = toolkit.getImage("icon.png");
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("click detected");
}
};
TrayIcon trayIcon = new TrayIcon(icon, "Test Program", null);
trayIcon.addActionListener(listener);
tray.add(trayIcon);
What happens when I run this program though, is that single clicks (either left or right) have no effect, but when I double click, then it shows the message 'click detected' in the console.
What can I do to have single clicks also be detected? Do I need to use a MouseListener for this? ( I've heard that MouseListeners can cause problems, and ActionListeners are better)
You could use MouseListener, ie:
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
}
}
});
See How to Write a Mouse Listener for more details.
EDIT: ActionListener vs MouseListener
There is a concept of low level and semantic events. Whenever possible, you should listen for semantic events rather than low-level events, such as listening for action events, rather than mouse events. Read for more details in Low-Level Events and Semantic Events.
In this case you just need more details from the event so using MouseListener is required.
I am using my predefined inherited Focus Traversal Class For My JFrame
I have defined the key press event for one of my button with some action on pressing Tab key to select other tab of my jTabbed Pane . This button is not responding only for the tab key .
int index=1;
if(evt.getKeyCode() == KeyEvent.VK_TAB)
{
// wrap around
if(evt.isShiftDown())
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent();
}
else
{
System.out.print("Shift Up");
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
jtabPaneProducts.setSelectedIndex(index);
}
}
Please guide me how can i made jbutton to respond to the TAB key press in addition to focus traversal functionality.
You should be interested to read How to Write a Key Listener:
Alternatively, you can use the KeyEventDispatcher class to pre-listen to all key events. The focus page has detailed information on the focus subsystem.
And consequently: Interface KeyEventDispatcher