So, following my last post (UBW: Unlimited Bullshit Works) I figured I'd post something a little useful and practical. Being the shaznazzy Java programmer that I am and lurking dev sites, I tend to notice that a lot of Java developer forums are loaded with a shit ton of questions regarding Java based OSK's (On-Screen Keyboards), aka Virtual Keyboards. Also having been one of the douche-n00bs to Java some time ago wanting to create one, I figured I'd lend a helping hand to all those who are struggling to make their Java 101 class project deadline by posting some code about Java OSK's.
First and foremost, an OSK's gotta be on top. You can accomplish this by calling the setAlwaysOnTop() method and giving it a value of true (so it should look like this: setAlwaysOnTop(true) ). This method will, obviously, set the JFrame to always be on top of other windows regardless of which window actually has the focus. And speaking of focus, the next thing that's required for the Java Virtual Keyboard is something called focusable window state.
As it should have already become aware to you Java programmers, a virtual keyboard won't really function if it's always the window that has the focus (the one that's usually on top and has a darker highlight on the frame decoration than the others) and I've seen numerous people on those Java dev forums ask things like "how can I have a JFrame input keystrokes into another window?", or "is it possible to put a string of text from one window into the text area of another?". The list goes on, with some weird answers like "use MouseInfo.getPointerInfo().getLocation() to get the position of the mouse when they click on the text area and store the JButtons that are hit representing letters/numbers until they're done typing then yada yada yada..." Those are probably the questions/answers you've found too if you've been trying to find premade code to steal for your class project you so boldly declared you'd do doing any research into Java OSK's. Thinking about it, you probably could do that using threads and then you'd negate needing to unfocus the virtual keyboard window and (using your infinite knowledge in Java code) have the text magically appear in the text area.
Bull. Shit.
I'm gonna tell you somethin' right here and now guys, and you'll forever worship the ground I walk on for departing a portion of my enlightened wisdom to you:
setFocusableWindowState(false)
That's right, it was that easy. Java has a built-in method that you can call (setFocusableWindowState()) and set to false so that the JFrame can never receive the focus, no matter how much you click on it. Combining that with setAlwaysOnTop(true) will give you the desired effect of having an unfocusable window always be the center of attention.
By now, I would think, in your attempt to steal create an OSK you've probably learned about the Robot Class. But, if not, I'll let you in on it.
The Robot class does a lot of neat things, but I'm going to focus (ha, pun intended >_>) on it's ability to simulate keystrokes, also called KeyEvents. To call the Robot class, just set it up like you would if you were making an int or a JButton:
import java.awt.Robot; (eclipse should import automatically if you try to create a Robot)
import java.awt.AWTException
try{
Robot robotVariableName = new Robot();
} catch(AWTException e) {
//some exception handler here
}
you will need a try/catch block for Robot creation. and then, once you set up your ActionListener for your JButton (if you plan on following what I'm doing, then I'd do an ActionListener for each JButton), keystroke simulation goes like this:
robotVariableName.keyPress()
robotVariableName.keyRelease()
In the (parenthesis) is a special command called KeyEvent where after you type a period(.) a boatload of choices pops up (if you use the eclipse IDE) that follow this example:
VK_A
And you've got to have both keyPress and keyRelease. Problems incur during the program's execution if you don't. So, in summation, a Robot named 'r' simulating the 'home' key looks like this:
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_HOME);
r.keyRelease(KeyEvent.VK_HOME);
} catch (AWTException ae) {
//ae.printStackTrace();
}
I commented out the print stack trace because you really don't want that popping up, even if there is an error.
As an added treat, I'll leave you with some code for a virtual number pad(feel free toplay around with it steal it if you'd like):
package VirtualKeyboard;
/**
* this class demonstrates the use of:
*
* setFocusableWindowState(BOOLEAN_VALUE)
*
* which accepts a boolean (true or false) to
* determine if the JFrame will ever receive focus.
*/
import java.awt.AWTException;
import java.awt.Robot;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
public class windowClass extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
windowClass frame = new windowClass();
frame.setVisible(true);
} catch (Exception e) {
//e.printStackTrace();
//POP-UP AN ERROR MESSAGE IF APP CAN'T RUN!
JFrame err=new JFrame("Error");
err.add(new JLabel("The application could not be launched."));
err.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
err.setSize(250, 100);
err.setVisible(true);
}
}
public windowClass() {
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 385, 360);
setFocusableWindowState(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton home = new JButton("hm");
home.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_HOME);
r.keyRelease(KeyEvent.VK_HOME);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
home.setBounds(10, 11, 65, 40);
contentPane.add(home);
JButton seven = new JButton("7");
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD7);
r.keyRelease(KeyEvent.VK_NUMPAD7);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
seven.setBounds(10, 113, 65, 40);
contentPane.add(seven);
JButton four = new JButton("4");
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD4);
r.keyRelease(KeyEvent.VK_NUMPAD4);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
four.setBounds(10, 164, 65, 40);
contentPane.add(four);
JButton one = new JButton("1");
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD1);
r.keyRelease(KeyEvent.VK_NUMPAD1);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
one.setBounds(10, 215, 65, 40);
contentPane.add(one);
JButton zero = new JButton("0");
zero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD0);
r.keyRelease(KeyEvent.VK_NUMPAD0);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
zero.setBounds(10, 266, 156, 40);
contentPane.add(zero);
JButton pgup = new JButton("pgup");
pgup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PAGE_UP);
r.keyRelease(KeyEvent.VK_PAGE_UP);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
pgup.setBounds(101, 11, 65, 40);
contentPane.add(pgup);
JButton eight = new JButton("8");
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD8);
r.keyRelease(KeyEvent.VK_NUMPAD8);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
eight.setBounds(101, 113, 65, 40);
contentPane.add(eight);
JButton five = new JButton("5");
five.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD5);
r.keyRelease(KeyEvent.VK_NUMPAD5);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
five.setBounds(101, 164, 65, 40);
contentPane.add(five);
JButton two = new JButton("2");
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD2);
r.keyRelease(KeyEvent.VK_NUMPAD2);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
two.setBounds(101, 215, 65, 40);
contentPane.add(two);
JButton pgdn = new JButton("pgdn");
pgdn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PAGE_DOWN);
r.keyRelease(KeyEvent.VK_PAGE_DOWN);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
pgdn.setBounds(190, 11, 65, 40);
contentPane.add(pgdn);
JButton nine = new JButton("9");
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD9);
r.keyRelease(KeyEvent.VK_NUMPAD9);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
nine.setBounds(190, 113, 65, 40);
contentPane.add(nine);
JButton six = new JButton("6");
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD6);
r.keyRelease(KeyEvent.VK_NUMPAD6);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
six.setBounds(190, 164, 65, 40);
contentPane.add(six);
JButton three = new JButton("3");
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD3);
r.keyRelease(KeyEvent.VK_NUMPAD3);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
three.setBounds(190, 215, 65, 40);
contentPane.add(three);
JButton period = new JButton(".");
period.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PERIOD);
r.keyRelease(KeyEvent.VK_PERIOD);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
period.setBounds(190, 266, 65, 40);
contentPane.add(period);
JButton end = new JButton("end");
end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_END);
r.keyRelease(KeyEvent.VK_END);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
end.setBounds(280, 11, 65, 40);
contentPane.add(end);
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_ADD);
r.keyRelease(KeyEvent.VK_ADD);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
plus.setBounds(280, 113, 65, 91);
contentPane.add(plus);
JButton enter = new JButton("enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
enter.setBounds(280, 215, 65, 91);
contentPane.add(enter);
JButton backspace = new JButton("<[X]");
backspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_BACK_SPACE);
r.keyRelease(KeyEvent.VK_BACK_SPACE);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
backspace.setBounds(10, 62, 65, 40);
contentPane.add(backspace);
JButton fwdslash = new JButton("/");
fwdslash.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_SLASH);
r.keyRelease(KeyEvent.VK_SLASH);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
fwdslash.setBounds(101, 62, 65, 40);
contentPane.add(fwdslash);
JButton asterisk = new JButton("*");
asterisk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_MULTIPLY);
r.keyRelease(KeyEvent.VK_MULTIPLY);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
asterisk.setBounds(190, 62, 65, 40);
contentPane.add(asterisk);
JButton minus = new JButton("-");
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_MINUS);
r.keyRelease(KeyEvent.VK_MINUS);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
minus.setBounds(280, 62, 65, 40);
contentPane.add(minus);
}
}
And, as always,
"Thank you. Fuck You."
import java.awt.AWTException
try{
Robot robotVariableName = new Robot();
} catch(AWTException e) {
//some exception handler here
}
you will need a try/catch block for Robot creation. and then, once you set up your ActionListener for your JButton (if you plan on following what I'm doing, then I'd do an ActionListener for each JButton), keystroke simulation goes like this:
robotVariableName.keyPress()
robotVariableName.keyRelease()
In the (parenthesis) is a special command called KeyEvent where after you type a period(.) a boatload of choices pops up (if you use the eclipse IDE) that follow this example:
VK_A
And you've got to have both keyPress and keyRelease. Problems incur during the program's execution if you don't. So, in summation, a Robot named 'r' simulating the 'home' key looks like this:
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_HOME);
r.keyRelease(KeyEvent.VK_HOME);
} catch (AWTException ae) {
//ae.printStackTrace();
}
I commented out the print stack trace because you really don't want that popping up, even if there is an error.
As an added treat, I'll leave you with some code for a virtual number pad(feel free to
package VirtualKeyboard;
/**
* this class demonstrates the use of:
*
* setFocusableWindowState(BOOLEAN_VALUE)
*
* which accepts a boolean (true or false) to
* determine if the JFrame will ever receive focus.
*/
import java.awt.AWTException;
import java.awt.Robot;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
public class windowClass extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
windowClass frame = new windowClass();
frame.setVisible(true);
} catch (Exception e) {
//e.printStackTrace();
//POP-UP AN ERROR MESSAGE IF APP CAN'T RUN!
JFrame err=new JFrame("Error");
err.add(new JLabel("The application could not be launched."));
err.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
err.setSize(250, 100);
err.setVisible(true);
}
}
public windowClass() {
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 385, 360);
setFocusableWindowState(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton home = new JButton("hm");
home.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_HOME);
r.keyRelease(KeyEvent.VK_HOME);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
home.setBounds(10, 11, 65, 40);
contentPane.add(home);
JButton seven = new JButton("7");
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD7);
r.keyRelease(KeyEvent.VK_NUMPAD7);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
seven.setBounds(10, 113, 65, 40);
contentPane.add(seven);
JButton four = new JButton("4");
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD4);
r.keyRelease(KeyEvent.VK_NUMPAD4);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
four.setBounds(10, 164, 65, 40);
contentPane.add(four);
JButton one = new JButton("1");
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD1);
r.keyRelease(KeyEvent.VK_NUMPAD1);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
one.setBounds(10, 215, 65, 40);
contentPane.add(one);
JButton zero = new JButton("0");
zero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD0);
r.keyRelease(KeyEvent.VK_NUMPAD0);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
zero.setBounds(10, 266, 156, 40);
contentPane.add(zero);
JButton pgup = new JButton("pgup");
pgup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PAGE_UP);
r.keyRelease(KeyEvent.VK_PAGE_UP);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
pgup.setBounds(101, 11, 65, 40);
contentPane.add(pgup);
JButton eight = new JButton("8");
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD8);
r.keyRelease(KeyEvent.VK_NUMPAD8);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
eight.setBounds(101, 113, 65, 40);
contentPane.add(eight);
JButton five = new JButton("5");
five.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD5);
r.keyRelease(KeyEvent.VK_NUMPAD5);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
five.setBounds(101, 164, 65, 40);
contentPane.add(five);
JButton two = new JButton("2");
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD2);
r.keyRelease(KeyEvent.VK_NUMPAD2);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
two.setBounds(101, 215, 65, 40);
contentPane.add(two);
JButton pgdn = new JButton("pgdn");
pgdn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PAGE_DOWN);
r.keyRelease(KeyEvent.VK_PAGE_DOWN);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
pgdn.setBounds(190, 11, 65, 40);
contentPane.add(pgdn);
JButton nine = new JButton("9");
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD9);
r.keyRelease(KeyEvent.VK_NUMPAD9);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
nine.setBounds(190, 113, 65, 40);
contentPane.add(nine);
JButton six = new JButton("6");
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD6);
r.keyRelease(KeyEvent.VK_NUMPAD6);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
six.setBounds(190, 164, 65, 40);
contentPane.add(six);
JButton three = new JButton("3");
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_NUMPAD3);
r.keyRelease(KeyEvent.VK_NUMPAD3);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
three.setBounds(190, 215, 65, 40);
contentPane.add(three);
JButton period = new JButton(".");
period.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PERIOD);
r.keyRelease(KeyEvent.VK_PERIOD);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
period.setBounds(190, 266, 65, 40);
contentPane.add(period);
JButton end = new JButton("end");
end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_END);
r.keyRelease(KeyEvent.VK_END);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
end.setBounds(280, 11, 65, 40);
contentPane.add(end);
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_ADD);
r.keyRelease(KeyEvent.VK_ADD);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
plus.setBounds(280, 113, 65, 91);
contentPane.add(plus);
JButton enter = new JButton("enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
enter.setBounds(280, 215, 65, 91);
contentPane.add(enter);
JButton backspace = new JButton("<[X]");
backspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_BACK_SPACE);
r.keyRelease(KeyEvent.VK_BACK_SPACE);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
backspace.setBounds(10, 62, 65, 40);
contentPane.add(backspace);
JButton fwdslash = new JButton("/");
fwdslash.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_SLASH);
r.keyRelease(KeyEvent.VK_SLASH);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
fwdslash.setBounds(101, 62, 65, 40);
contentPane.add(fwdslash);
JButton asterisk = new JButton("*");
asterisk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_MULTIPLY);
r.keyRelease(KeyEvent.VK_MULTIPLY);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
asterisk.setBounds(190, 62, 65, 40);
contentPane.add(asterisk);
JButton minus = new JButton("-");
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot r=new Robot();
r.keyPress(KeyEvent.VK_MINUS);
r.keyRelease(KeyEvent.VK_MINUS);
} catch (AWTException ae) {
//ae.printStackTrace();
}
}
});
minus.setBounds(280, 62, 65, 40);
contentPane.add(minus);
}
}
And, as always,
Howdy. Thanks a million for this. Exactly what I needed - setFocusableWindowState(false);
ReplyDeleteCheers,
Brian, Ireland
Ground worshipped. That setFocusableWindowState call is pure gold.
ReplyDeleteThanks.