I'm using Swing to create a hot key for all the menus. For Save As menu item The 'A' should be under lined instead of 'a' immediately following the S in Save as. I've used the following code to create hot keys for menuitems
"for Save As Menu: MenuItem.setMnemonic('a');"
You'll want to use the setDisplayedMnemonicIndex(...) to provide a hint to the JVM for which letter should get the underline. You invoke this after calling setMnemonic(KeyEvent.VK_A)
Please check out the API for more on this: AbstractButton API
Related
This is my first Java project and I'm not sure where to look/what to search for for information on this question.
I've built Java code in Eclipse using Selenium WebDriver that opens a product page, adds a product to cart, and completes the checkout process automatically. Is it possible to create a GUI with textbox submissions that would insert variables into my already-created code? For example, I'd like to create a GUI that would update my code with the submitted size rather than having to hard-code '6.5' every time as seen in the code below.
wd.findElement(By.xpath("//div[#class='size-options-buttons']/ul//a[#title='6.5']")).click();
Any information or guidance on what to research would be greatly appreciated!
I wouldn't think you would want/need to create a GUI to do this. You should just create a variable that contains the value you want used in your searches and then you can update that value any time you want in only one place.
String title = "6.5";
wd.findElement(By.xpath("//div[#class='size-options-buttons']/ul//a[#title='" + title + "']")).click();
If you wanted, you could read this and other values from a text file, XML, etc.
This is achievable with Java, but its not a part of Selenium as such. You can explore Java GUI frameworks such as AWT, Swing etc, using which you can create your GUI. You can add a text box and button in your UI. Upon click of the button, you can run your Selenium code, where it can fetch the value you entered in the text box as a variable as pass it on to your script.
It all depends on how far you want to take this. You can create very basic UIs with say JavaScript, VBScript as well. These would give you the basic functionality of a text box and button.
You can use an excel sheet also, where you can set these values and pass it on to your script.
I have my own custom editor and now I want to add refactor option to this editor. I know I can add a new menu item for the same, but I want to use the existing refactor option. Like when user selects text and if it is valid variable (in Java) the all other variables are highlighted and later it is replaced. Also, I want to reuse the same refactor option and not provide new menu item.
Any pointers on how to go about this?
I followed the link: Eclipse rename/refactoring override
Now I get the notifications during each refactor but for my custom editor refactor -> rename menu is disabled. How do I enable it?
I am writing a Java application for digitizing a group of documents in the office that I am working in and I am wanting to check if 5 textfields are populated and 4 combobox fields as well before the save button is enabled (I have it checking if i press a button (that happily says "Check"), but i would much rather have it auto-check to see if they are populated or if they are null).
If it makes a difference i am using NetBeans for this project.
Basically I need the fields to have something in them before the document can be saved.
Any and all help will be greatly appreciated as this is the final step in creating this application... :D
Thanks,
Erik
There are two ways (I can think of):
1- Put a listener on each field, this listener will be triggered when the field is populated. Inside the listener increment a counter for example, or set a flag. If all flags are set or if the counter reaches (9 in your example) then enable the button.
2- Enable the Save button, but call a validate() method before doing the Save action. Any unpopulated field will have a red mark beside it (shown by validating) like in web applications.
I want to make an auto completion textfield, which replaces the input with the first hit and selects the part which wasn't entered by the keyboard yet. To do so, I'll have to count the chars typed (or deleted). How can I filter if the KeyEvent is a printable Char (and not SHIFT f.e.)?
Look at https://swingx.dev.java.net/
This lib is already have great autocomplete text fields
i prefer to do it with the KeyListener
Well, a KeyListener is the wrong tool for the job.
If you really want to write your own code then you should start with a DocumentListener. Every time text is added or removed from the text field you will be notified. Then you can implement your auto completion code.
How can I get JTextFields to allow Ctrl-Delete and Ctrl-Backspace when editing text?
In various other programs, these key combinations can delete an entire word in one go.
From what I can tell, the default behaviour of a JTextField allows the user to use CTRL to jump over an entire word when using left and right keys, and to select an entire word when SHIFT is used too. However, deleting just doesn't work.
Swing uses Key Bindings to map Actions to components. To find out the default mappings for a given component you can use the Key Bindings program. The article also contains a link to the Swing tutorial which contains a section on "How to Use Key Bindings".
To create your custom Action you would extend TextAction so you have access to the text component. You would then need to get the current caret position. Then you can use the Utilities class to get the start or end of the current word and then you can remove the characters from the Document.
You need to define an Action and put it into the action map of the composite. See this article for an introduction.