Sending slash and backslash with Java - java

When using a Java robot, when sending a slash or backslash throws an exception.
For example:
public void slash() throws AWTException {
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_SLASH);
rob.keyRelease(KeyEvent.VK_SLASH);
}
public void backSlash() throws AWTException {
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_BACK_SLASH);
rob.keyRelease(KeyEvent.VK_BACK_SLASH);
}
Then, when I want to type those, I use:
public void type() {
try {
slash();
} catch (AWTException e) { System.out.println("Exception when typing slash."); }
try {
backSlash();
} catch (AWTException e) { System.out.println("Exception when typing back slash."); }
}
I get two error messages in my console. By the way, all other keystrokes I tried to send worked fine.
I get the follwowing stacktrace for slash:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.paschoalinoto.bruno.pastescript.Paste.slash(Paste.java:23)
at com.paschoalinoto.bruno.pastescript.Paste.type(Paste.java:36)
at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Backslashes work, but also throw an IllegalArgumentException:
java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.paschoalinoto.bruno.pastescript.Paste.press(Paste.java:198)
at com.paschoalinoto.bruno.pastescript.Paste.paste(Paste.java:173)
at com.paschoalinoto.bruno.pastescript.Paste.finalPaste(Paste.java:227)
at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Is there any way I can send slash and backslash keystrokes?

Okay, I have found a solution that may be handy for users working with different keyboard layouts. It uses Alt codes.
public static void alt(int event1, int event2, int event3, int event4) throws Exception {
Robot bot = new Robot();
bot.delay(50); //Optional
bot.keyPress(KeyEvent.VK_ALT);
bot.keyPress(event1);
bot.keyRelease(event1);
bot.keyPress(event2);
bot.keyRelease(event2);
bot.keyPress(event3);
bot.keyRelease(event3);
bot.keyPress(event4);
bot.keyRelease(event4);
bot.keyRelease(KeyEvent.VK_ALT);
}
Then you call it like this:
For back slashes:
alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD9, KeyEvent.VK_NUMPAD2);
For normal ones:
alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD7);
No exceptions.
Also works for all other characters. But make sure you have Num Lock on when using this.

(Sorry I should mention this isn't quite an answer, just saying that it does work for me so I'm guessing it's a configuration issue or something--but I thought that it was something others might find handy. It should be a comment but for technical reasons I have to make it an answer)
I hadn't messed with the Robot class and just spent a FUN half hour messing around and bulding some stuff on top of this cool java class.
For me sending slash and backslash work fine. Since the VK_??? stuff maps pretty well to the ASCII characters, you can send '\' or '/' and it should work too.
I used Groovy because that's what I play in these days, but here's a nice example and a bunch of reusable code I just came up with. It's written as a script but could easily be converted to a class in either Groovy or Java (and I will do so soon).
This must be run from a "Priviliged" shell (for instance right-click on the command prompt and select "Run as administrator").
also it MUST have time for you to let go of the keyboard! (Learned that the hard way), so if you use groovyShell and use alt-r to run it, be sure to put a 1 second delay before sending the first keys or your ALT will become part of the keys pressed.
import java.awt.*
import java.awt.event.*
import static java.awt.event.KeyEvent.*
r=new Robot()
r.autoWaitForIdle = true
r.autoDelay=200 // Usually works with 0 but sometimes that's too fast.
// This will alt-tab you to your "Previous" app. While testing I edited this in notepad++
// then tabbed out to a shell to execute it, this tabbed back into my editor and typed
// the "test" text.
alt VK_TAB
send "backslash=\\ \nforward slash =/"
// This will send any string
def send(String s)
{
def difference = ("a" as Character) - ("A" as Character)
s.each {
Character c=it as Character
if(c.isUpperCase()) {
shift c
} else if(c.isLowerCase()) {
send(c - difference)
}
else send(c)
}
}
// These will work for integers and chars, NOT strings
def send(key)
{
press(key as Integer)
release(key as Integer)
}
def alt(key)
{
press VK_ALT
send key
release VK_ALT
}
def shift(key)
{
press VK_SHIFT
send key
release VK_SHIFT
}
def press(key)
{
r.keyPress(key as Integer)
}
def release(int key)
{
r.keyRelease(key as Integer)
}

Related

FileUtils.copyURLToFile not working for project file

I am building Java SWT application. As a part of whole process i am working on an excel file which have specific format to process things. What i want is a link on my JFrame which triggers download of a sample excel file stored in my project without local or internet dependency. I don't know how exactly i can do this.
I have tried this FileUtils.copyURLToFile but it is throwing null pointer exception. I am showing my code. Any help would be greatly appreciated.
public void actionPerformed(ActionEvent arg0) {
log("clicked to download");
String temp_path = System.getProperty("user.dir")+"\\resources\\EmailSheet.xlsx";
log("path = " + temp_path);
URL inputUrl = getClass().getResource(temp_path);
log("URL = " +inputUrl.toString());
File dest = new File("D:/new_file.xlsx");
try {
FileUtils.copyURLToFile(inputUrl, dest);
} catch (IOException e) {
log("Error saving sample file : " + e.getMessage().toString());
e.printStackTrace();
}
}
My Console is printing this error
Mon Oct 03 09:14:29 IST 2016 : clicked to download
Exception in thread "AWT-EventQueue-0" Mon Oct 03 09:14:29 IST 2016 : path = \resources\EmailSheet.xlsx
java.lang.NullPointerException
at tech.excelemail.com.TechExcelEmailApp$7.actionPerformed(TechExcelEmailApp.java:221)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I want to know if there is mistake in my code. or if i am going the wrong way. Ask me anything that if u think something missing unspecified.
Thanks in advance.
It's rather hard to believe that System.getProperty("user.dir") really returned an empty string, as your trace appears to suggest. Is this the real code?
In any case resources are named relative to the CLASSPATH, not the file system, so user.dir has nothing to do with it. They aren't files. And they don't use backslashes. Use forward slashes. And make sure that the resource named is present under that name in the JAR file.

java.io.IOException trying to load Java KeyStore

I've started getting a serious problem by the beggining of this week when our customer got new Smartcards version to use on one of our products, during the keyStore loading it throws an exception like this:
java.lang.Exception: Login failure: java.io.IOException: ObjectIdentifier() -- data isn't an object ID (tag = -95)
at certificate.helper.HelperClass.validateCard(HelperClass.java:194)
at certificate.helper.HelperClass.retrieveToken(HelperClass.java:107)
at certificate.view.LoginDialog.validate(LoginDialog.java:144)
at certificate.view.LoginDialog$1.actionPerformed(LoginDialog.java:84)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
To get things worse, I just can't simulate it on development env!
I started thinking it is something related to user's environment, here our team use Windows 10 x64 + JDK 1.8_x 64, we can simulate this same error in some statios with Windows 7 x64 + JRE 1.8_x x86, our customer got also Linux in some station and they said it works fine.
I tried to change keyStore model and the way it is loaded but it seems nothing solves the problem! Here is how things were done:
public TokenVO retrieveToken(String pin) throws Exception {
Provider pkcs11Provider = new SunPKCS11(leitorGemPC_Windows())
Security.addProvider(pkcs11Provider);
Security.addProvider(new BouncyCastleProvider());
KeyStore smartCardKeyStore = createKeyStore("PKCS11");
validateCard(pin, smartCardKeyStore);
Enumeration aliasesEnum = smartCardKeyStore.aliases();
while (aliasesEnum.hasMoreElements()) {
.... things don't even get this far
}
public void validateCard(String pin, KeyStore smartCardKeyStore) throws Exception {
try {
smartCardKeyStore.load(null, pin.toCharArray());
}
catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
public KeyStore createKeyStore(String keyStoreName) throws KeyStoreException {
if (keyStoreName== null || keyStoreName.isEmpty()) {
keyStoreName= DEFAULT_KEYSTORE;
}
KeyStore smartCardKeyStore = KeyStore.getInstance(keyStoreName);
return smartCardKeyStore;
}
The only keyStore that really works is the PKCS11, every other I tried don't validate user's Smartcard PIN/Password allowing to use anything even a blank password and return an empty aliasesEnum so that data cannot be retrived from the card.
Is there something I can do?

Java Load PDF File File Doesnt Exist

I am attempting to have my Java Application open a PDF file when the user clicks button. However, I get the stack trace below stating that the file doesn't exist. Bascially I would like to be able to load this file when the user makes the selection.
Below I will have the stack trace then the code and a screenshot of the path.
StackTrace:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \RFBase-TD_Communications\src\pdf\RFTDAnalyzerHelpFile.pdf doesn't exist.
at java.awt.Desktop.checkFileValidation(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at GUI.rfbgui.openPDF(rfbgui.java:787)
at GUI.rfbgui.access$7(rfbgui.java:773)
at GUI.rfbgui$6.actionPerformed(rfbgui.java:921)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Code:
private static void openPDF()
{
File pdfHelpFile = new File("/RFBase-TD_Communications/src/pdf/RFTDAnalyzerHelpFile.pdf");
try
{
Desktop.getDesktop().open(pdfHelpFile);
}catch(IOException ex)
{
ex.printStackTrace();
}
}
I have some general advice for how to handle these situations. Files were one of the things I got very frustrated with when starting to learn to program.
Use System.getProperty("user.dir"); This can be very helpful especially when you do not know where the program is going to be run from, or you have a specific file structure.
In Java, I generally recommend using "\" instead of "/".
Run a sanity check on the file you are attempting to load. Specifically check if it is null, .isFile(), etc. You never know what you might get back, so its good to take a peak before accidently crashing your program.
Here is some links for similar questions that might help you out;
How should I load files into my Java application?
Getting the Current Working Directory in Java
Getting the inputstream from a classpath resource (XML file)
File myFile = new File(getClass().getResource("/files/test.pdf").toURI());
or
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}

Java Robot Class Using Variables

I have a very simple question, or at least I think it is simple. Currently I'm trying to use the robot class with variables. What I mean by this is the following (the variables "pass" are chars.)
pass1 = 0;
pass2 = 0;
pass3 = 0;
pass4 = 0;
try{
Robot robot = new Robot();
robot.delay(2000);
robot.mouseMove(1318, 322);
robot.keyPress(pass1);
robot.keyPress(pass2);
robot.keyPress(pass3);
robot.keyPress(pass4);
} catch (AWTException e) {e.printStackTrace();}
When I run the program, I get this error (keep in mind I only get this error when the portion of the code with "pass1, pass2...etc." is in it.):
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code
at java.awt.Robot.checkKeycodeArgument(Unknown Source)
at java.awt.Robot.keyPress(Unknown Source)
at Cracker$2.mouseReleased(Cracker.java:117)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Robot#keyPress expects an int, which represents the virtual key code as described in java.awt.KeyEvent
Specifically, key 0 represents KeyEvent.VK_UNDEFINED which is an "invalid key code"
Don't forget that until you call keyRelease, the keys will continue to be pressed, generating repeated key pressed events...

I thought that java interfaces could hold a subclass, but maybe not in ActionListener... correct?

The issue could be different then what I suspect, but I figure the problem is either I read wrongly when learning about interfaces or it applies differently inside of the action listener.
First off, here is my error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to main.GUI
at main.GUI$3.actionPerformed(GUI.java:224)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The code I am using is as follows:
this.select.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
((GUI) e.getSource()).tool = new Selection();
}
});
I have an interface called tool, which I make a global variable Tool tool; for the GUI class. Then I attempt in the method above to assign the Selection class to the var tool which implements Tool. Am I doing something wrong and how do I fix it? If you need more code just ask.
Also When I assign a new Selection() to the global var tool outside of the ActionListener it works... so that is what I don't understand.
I have the button inside of a toolbar.
Use Action, shown here, to encapsulate functionality.
Regarding
I thought that java interfaces could hold a subclass, but maybe not in ActionListener… correct?
No, this has nothing to do with your problem. Rather your problem is an incorrect cast pure and simple. The object returned by e.getsource() is not a GUI object but rather an AbstractButton, perhaps a JButton (it's the variable named "select"). The component that has the ActionListener added to it is the one that is returned by ActionEvent#getSource().
Also, I don't see where you have an interface holding a subclass anywhere.
could you do?:
this.select.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
tool = new Selection(); //??
}
});
It's hard to say what you should do as we really don't know what the structure of the rest of your code looks like.

Categories

Resources