How can I send a custom key event to an applet? - java

Basically I have 2 JFrame windows, one of which contains an applet. I am trying to send the key typed to the applet.
#Override
public void keyReleased(KeyEvent e) {
dispatchKeyTyped(e.getID(),e.getModifiers(),e.getKeyCode(),e.getKeyChar(),e.getKeyLocation());
}
#Override
public void keyTyped(KeyEvent e) {
dispatchKeyTyped(e.getID(),e.getModifiers(),e.getKeyCode(),e.getKeyChar(),e.getKeyLocation());
}
public void dispatchKeyTyped(int id, int modifiers, int keycode, char keychar, int keylocation) {
applet.getComponent(0).dispatchEvent(new KeyEvent(applet,id,System.currentTimeMillis(),modifiers,keycode,keychar,keylocation));
}
When I try to do this nothing happens, the key is not send. If I replace the code by sending the KeyEvent like this:
#Override
public void keyReleased(KeyEvent e) {
dispatchKeyTyped(e);
}
#Override
public void keyTyped(KeyEvent e) {
dispatchKeyTyped(e);
}
public void dispatchKeyTyped(KeyEvent event) {
applet.getComponent(0).dispatchEvent(event);
}
This seems to work fine but I want to create the KeyEvent myself and not sure why the first example does not work.

Related

Dynamically find the correct method in order to not repeat the same code [duplicate]

This question already has answers here:
Java 8 Lambda Expressions - what about multiple methods in nested class
(6 answers)
Java idiom for lambdas with non-SAM interfaces
(4 answers)
Closed 3 years ago.
The following code adds Listeners to several SWT Text elements. The only difference is the code inside the Listeners method. Is there a way to make this code less repetitive by finding the correct method to use dynamically?
In this example a FocusListener is used, but it's not relevant.
private void addFocusLostListeners() {
nameText.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
myDataObject.setName(nameText.getText());
}
});
ageText.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
myDataObject.setAge(ageText.getText());
}
});
emailText.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
myDataObject.setEmail(emailText.getText());
}
});
...
}
You could make a helper method (you need to replace TextField with the actual class of nameText, ageText, etc.):
private static void addFocusListener(TextField field, Consumer<? super String> setter) {
field.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
setter.accept(field.getText());
}
});
}
Which you then could call:
private void addFocusLostListeners() {
addFocusListener(nameText, myDataObject::setName);
addFocusListener(ageText, myDataObject::setAge);
addFocusListener(emailText, myDataObject::setEmail);
}

Java call "addActionListener" from "FocusListener"

I have JTextField. I need to save the changes, if user writes something in it and then lost the focus(like click some where else)
mMaxLabelLength = new JTextField();
mMaxLabelLength.addActionListener(this);
public void focusGained(FocusEvent fe)
{
System.out.println("4");
mMaxLabelLength.addActionListener(this);
}
#Override
public void focusLost(FocusEvent fe)
{
System.out.println("5");
mMaxLabelLength.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//Do something
}
The problem is I am not able to call "actionPerformed" from "focusLost/focusGain". I need to keep the "actionPerformed" as separate method as I am calling it from another places also.
So, you want to do exactly the same thing when the focus is lost as what you're already doing in actionPerformed(), right, right. So, do just that:
#Override
public void focusLost(FocusEvent fe) {
doSomething();
}
public void actionPerformed(ActionEvent e){
doSomething();
}
private void doSomething() {
// ...
}

How do I simplify MouseListener so that I don't have all these unused methods?

Below I have the following code, so that when someone clicks on the "Close", the window will close. Below that is another exit button on the same menu bar, simply for redundancy (it'll be changed later to be something else, but the point stands as follows). My question is, is there any way to make this more simplistic? I mean there are four unused methods for every menu, and I'm going to need to do a few more. Any ideas on how to fix this?
closeFile.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
exit.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
Also, ActionListener wouldn't work for me, so I can't use that (don't believe I'm supposed to either).
Use a MouseAdapter and override the methods that you want.
closeFile.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
closeFile.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
//your code
}
});
Note: You dont have to write 'implements MouseListener' during class definition.
For more information, search for adapter classes, more specifically for MouseAdapter class.

Java Event listeners (mouse and keyboard)

So everything in my program works, except for the mouse and the keyboard listeners.
I've got a couple actionListeners working on Jbuttons that do exactly what I'm trying to do here, but the assignment says it has to work with all three.
So I would like to know why it compiles, but doesn't work. Am I doing something wrong?
panel.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
shape.addSides();
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
shape.subSides();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
);
panel.addMouseListener(new MouseListener(){
#Override
public void mouseEntered(MouseEvent e){
if(e.getButton() == MouseEvent.BUTTON1){
shape.addSides();
}
if(e.getButton() == MouseEvent.BUTTON3){
shape.subSides();
}
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
);
Regarding KeyListener: it won't work unless the listened-to component is focusable and has focus. So you will want to call setFocusable(true) and requestFocusInWindow() on your JPanel. As for the MouseListener -- something else may be taking the mouse event and preventing it from reaching your JPanel. To debug this you need to post a minimal, compilable, runnable example program.
Also regarding your MouseListener, you're checking getButton() in a mouseEntered event which makes no sense. The buttons are not involved in this type of event. Are you instead meaning to check mouseDragged(...) of a MouseMotionListener?

KeyListener doesn't produce any response

Given the following (partial) code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test extends Applet implements MouseListener , KeyListener
{
private static final long serialVersionUID = 1L;
private static final int TOTAL_POINTS = 500;
private static final int THRESHOLD = 5;
// the arrays that contain the indexes of the points that the user created
private int[] m_Xindex, m_yIndex;
// The number of points that the user created
private int m_pointsCreated;
#Override
public void keyTyped(KeyEvent keyEvent)
{
char key = keyEvent.getKeyChar();
if (key == 'F')
System.out.println("123");
}
#Override
public void mouseReleased(MouseEvent arg0) {/* Empty */ }
#Override
public void mouseClicked(MouseEvent e) {/* Empty */ }
#Override
public void mouseEntered(MouseEvent e) {/* Empty */ }
#Override
public void mouseExited(MouseEvent e) {/* Empty */ }
#Override
public void mousePressed(MouseEvent myEvent) {/* Empty */ }
#Override
public void keyPressed(KeyEvent keyEvent) {}
#Override
public void keyReleased(KeyEvent keyEvent) {}
}
I removed my working code and left only the problematic code.
When I press F , I want to print to the screen 123 , but nothing is
printed to the screen.
What's wrong with the code of keyTyped?
Change if (key == 'F') to if (key.equals('F')). Test for object equivalence rather than equality.
Be sure that the component is focusable & to requestFocusInWindow(). The latter should best be done by an #Override on the start() method.
Consider using Swing (JApplet) and key bindings instead of AWT Applet and KeyListener.

Categories