Reputation: 465
In many cases it happened to me that I have written an event and later I discovered that in another event I am repeating same code again and again. For example I have this code in mouuseclick:
Text_1.addMouseListener(new org.eclipse.swt.events.MouseListener() {
public void mouseDown(MouseEvent e) {
if(Highlighted)
//UnHighlightWords();
SetActualWord();
selectedWord=ActualWord;
fetchWord(ActualWord);
DisplayData.label_translation.setText(String.valueOf(ActualWord.getSurahNumber()));
ShowWord(ActualWord);
}
and again I will have keypress event like this:
Text_1.addKeyListener(new org.eclipse.swt.events.KeyListener(){
public void keyPressed(KeyEvent e) {
// ******recall Mousedown event*******
}
}
Is it possible to recall Mousedown method inside Keypressed to do not repeat same code again here or not? If yes how?
Upvotes: 0
Views: 7174
Reputation: 1743
You can't call a KeyListener's method inside a MouseListener's method, as they are different events and used for different purposes, so you can't cast KeyListener
event to MouseListener
, and vice versa.
But you can call call keyPressed()
inside keyReleased()
for example, as they are the same event.
UPDATE
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EventDemo extends JFrame implements ActionListener,KeyListener{
private JTextField text;
private JButton saveButton,cancelButton;
public EventDemo(){
JPanel p = new JPanel();
text = new JTextField("Event Demonstration");
saveButton = new JButton("SAVE");
cancelButton = new JButton("CANCEL");
saveButton.addActionListener(this);
cancelButton.addActionListener(this);
text.addKeyListener(this);
p.add(saveButton);
p.add(cancelButton);
p.add(text);
setDefaultCloseOperation(3);
getContentPane().add(p,BorderLayout.CENTER);
setLocationRelativeTo(null);
setVisible(true);
pack();
}
public void actionPerformed(ActionEvent ev){
Object source = ev.getSource();
if(source== saveButton){
System.out.println("Save button action");
}
else if(source == cancelButton){
//do something
System.out.println("cancel button action");
}
}
public void keyPressed(KeyEvent e){
//see here we called keyEvent method
keyEvents(e);
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
/*
this method created for key events
*/
public void keyEvents(KeyEvent e){
System.out.println(e.getKeyChar());
}
public static void main(String...args){
new EventDemo();
}
}
Upvotes: 1
Reputation: 63
Simply put all this code (which you already call it in mouse event) in a separate method and call the method whenever you need
if(Highlighted)
//UnHighlightWords();
SetActualWord();
selectedWord=ActualWord;
fetchWord(ActualWord);
DisplayData.label_translation.setText(String.valueOf(ActualWord.getSurahNumber()));
ShowWord(ActualWord);
Upvotes: 3
Reputation: 41281
Simple. First get a reference to the object(which is an instance of an anonymous class by:
//I'll import the class for readability
import org.eclipse.swt.events.KeyListener;
//snip
//Do this line ***OUTSIDE*** the method, and in the class body!
MouseListener ml=new MouseListener(){
void mouseClicked(MouseEvent e){
//Do something
}
//All the other methods as well
}
KeyListener kl=new KeyListener(){
public void keyPressed(KeyEvent e) {
ml.mouseClicked(new MouseEvent(Component source, ...)); //edit this part as needed
}
}
Text_1.addKeyListener(kl);
You can now repeat the use of kl
as needed. You can also manually invoke said listener by doing kl.keyPressed(KeyEvent e);
Upvotes: 1