I need to select appropriate option from the popup window. Unfortunately, it is not the popup displayed by the web page, so I cannot use Selenium framework for that purpose.
As I checked, it is possible to navigate to different option by pressing arrow keys. Thus keyPress and keyRelease from Java AWT Robot should work for me perfectly to select option and confirm the selection by pressing Enter key.
Unfortunately, I do not see a method to read currently selected item text. Without that I cannot find appropriate option. Is it possible to read item label using Java AWT Robot?
If the target application is another java application, then you should be able to get a reference to the component hierarchy and traverse it until you find the text field & label in question.
Assuming it's not a java application, then I don't believe it's possible to do this - at least directly. Robot just provides mouse / keyboard input, but you could use it combined with other classes in the toolkit to at least partially solve your problem.
Use the Robot methods keyPress & keyRelease to input CTRL-A. Then CTRL-C.
Once the text is in the clipboard, you could read it using Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)
You might be able to use the same approach for a text label, assuming it is selectable.
This question is talking about using java.awt.Robot to alter text in another program (MS excel), but it might provide you some additional ideas for tackling the problem:
How can I select text in another application with java.awt.robot (example: shift+home)
Related
I am writing a JavaFX application and I have used accelerators to add shortcut keys to the application menus.
Now I am creating the application help and I want to describe the usage of the shortcut keys.
The recommendation for JavaFX accelerators is to use SHORTCUT instead of CONTROL (Windows) and COMMAND (Apple). This works fine and in the menus when running the application on different platforms show the right key combination.
For example, MenuItem Exit I have added the accelerator SHORTCUT_DOWN + X which is displayed as
Ctrl+X under Windows
and
⌘+X under Mac OS
Now I would like to get the explanations (Ctrl+X, ⌘+X) from the system in order to add it to the user help.
Is it possible to ask JavaFX for the presentation of the accelerator in the menu? Or get the presentation of SHORTCUT_DOWN used in the menus?
Thx in advance
Thorsten
According to the documentation, the getDisplayText() method
Returns a string representation of this KeyCombination that is suitable for display in a user interface (for example, beside a menu item).
So all you need is
String acceleratorAsString = menuItem.getAccelerator().getDisplayText();
I am working on a program that will allow the user to record steps of simple tasks and then generate a file to send to people that show these steps. So if you left click on a window it will say "User Left Clicked on Google Chrome" with an appropriate screenshot and highlighted cursor for visbility.
I am using Java Native Hook Found here for the global mouse/key listeners and Java Native Access Found here to get the title of the application that is clicked.
I would like to include something that highlights an area where text is entered. At the moment I am thinking of taking a screenshot when the user clicks a textbox and then storing all the keys that are pressed (for the guide) and taking a second screenshot after the text has been input, and also adding a highlight outline around the text.
I feel like it would be easier to generate the highlighting if I could get the location of the caret but i'm not exactly sure how to do this for global applications.
I need to make selection of text using mouse instead of ctrl+A
I tried:
sendAcceleratorKey(MouseEvent.BUTTON1, "");
but I don't know which argument could I set to say make a click with mouse and let the mouse enforced to select the text.
If the text is in a JTextComponent, selectAll() may be a suitable choice in your MouseListener.
Addendum: You may also be able to leverage the select-all Action, which is bound to control-A or meta-A by default on various platforms.
Your sscce may be helpful in deciding. There's a related example here.
How can i disable such keys and their combinations as, for example, Alt ; Alt + F4 and others in my Java AWT application?
E.g. my KeyboardListener should handle that keys as 'usual' keys and combinations without closing window or entering window menu.
One way is to create a program in "kiosk mode", something that requires more than Java to achieve (such as JNA or JNI). If you google this or search this site for this, you'll find out more about it. If I were using your code, though, I'd be very frustrated and perhaps angry, unless this were being run on a dedicated kiosk terminal.
Edit: Another option is as per this thread: java-full-screen-program-swing-tab-alt-f4:
window.setExtendedState(Frame.MAXIMIZED_BOTH); //maximise window
window.setUndecorated(true); //remove decorations e.g. x in top right
window.setAlwaysOnTop(true);
Edit 2: and this brute-force method: Remove the possibility of using Alt-F4 and Alt-TAB in Java GUI
Found this solution:
for Tab - use Frame.setFocusTraversalKeysEnabled(false);
for Alt - add keyEvent.consume(); at the end of each key
event handling code block
Then, to find out if Alt or Ctrl key is pressed - use keyEvent.isAltDown() and keyEvent.isControlDown() methods of keyPressed or keyReleased events.
Thanks, #Hovercraft , for your quick response!
I am creating a SQL editor. I am using JTextPane for the editor. I want to implement AutoCompletion for table name etc. like Eclipse.
I think the appropriate class for displaying info on top of another component is JPopupMenu, which already handles layering correctly to display itself. JPopupMenu has a show() method that takes its 'parent' component as an argument, and it will show itself in that component's coordinate space. Since you want to display a selection of terms for the user to choose from, a menu seems appropriate.
To check for text changes, you'd add a DocumentListener to the document that's wrapped by the JTextPane; you can access it using getDocument().
To find out where the cursor (actually, the caret) is, you can use getCaretPosition(). That returns the caret's position within the text stream as an int. You can use modelToView() to translate that position to actual (x,y) coordinates. That in turn will tell you where to show your menu.
You can use addKeyListener() to catch keyboard events on your JTextPane, like hitting Ctrl-Space.
The combination of all that should allow you to do what you're looking to do.
You can also use http://fifesoft.com/autocomplete/. You can install it on any JTextComponent.
For things like this you probably should consider layered panes so your auto-complete suggestions appear in the correct place and z-order.
Furthermore you will have to look for changes in the JTextPane to know when the user is typing and you will need a parser that understands what is typed so you can offer the feature only at appropriate points.
It's not quite clear what exactly your problem is and what you got so far.
I achieved this by adding a key listener to the JTextPane and checking for CTRL + Space keystrokes. When the appropriate key combo was detected the listener went off and looked up the list of possible matches based on the characters directly to the left of the cursor at the time of the key press and found the best matches and displayed them to the user in a JPopup. If there was an exact match then it simply replaced the partial text with the match. If no matches were found an option was given to the user to add the text that they had already typed, edit it and record it into the list of acceptable data.
We use jide. They have a lot of components that help you do this kind of thing really easily