KeyListener problem with JApplet
Hi Guys how are you doing?
I developed an JApplet which is drag and drop enabled to upload local system file to server. In this JApplet in addition to DRAG and DROP i am trying to add PrintScreen image copy on to the applet. I did everything like getting the clipboard image and creating a image file etc. even i am able to upload that file. But i am not able to paste the image on to the applet. For this i need to capture the Key Events.
For this I implemented the KeyListener to my class and overridden the abstract methods in that interface.
I also added the KeyListener to the JPanel like below
JPanel cp = new JPanel();
cp.addKeyListener(this);
but when I press any key the abstract methods are not called here....
any help guys.....
Please help me if anybody have idea about how to do this.....
thanks in advance
batta
[894 byte] By [
battassa] at [2007-10-2 3:24:39]

maybe this can help you
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KeyDemo extends JFrame implements KeyListener
{
private String line1 = "", line2 = "", line3 = "";
private JTextArea textArea;
public KeyDemo()
{
super("Demonstrating Deystroke Events");
textArea = new JTextArea(10, 15);
textArea.setText("Press any key on the keyboard...");
textArea.setEnabled(false);
addKeyListener(this);
getContentPane().add(textArea);
setSize(350, 100);
show();
}
public void keyPressed(KeyEvent e)
{
line1 = "Key released: " + e.getKeyText(e.getKeyCode());
setLines2and3(e);
}
public void keyReleased(KeyEvent e)
{
line1 = "Key released: " + e.getKeyText(e.getKeyCode());
setLines2and3(e);
}
public void keyTyped(KeyEvent e)
{
line1 = "Key typed: " + e.getKeyChar();
setLines2and3(e);
}
private void setLines2and3(KeyEvent e)
{
line2 = "This key is " + (e.isActionKey() ? "" : "not ") + "an action key";
String temp = e.getKeyModifiersText(e.getModifiers());
line3 = "Modifier keys pressed: " + (temp.equals("") ? "nome" : temp);
textArea.setText(line1 + "\n" + line2 + "\n" + line3 + "\n");
}
public static void main(String pirucaaa[])
{
KeyDemo app = new KeyDemo();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
please check my applet code here..... just i am giving my "init()" method and other abstract methods of the keylistener.
public class UploaderApplet extends JApplet implements KeyListener{
public static JFrame frame;
public JList list;
public DefaultListModel model;
public Vector uploadFiles=new Vector();
public File currentDir;
public DropTarget target;
JButton addButton, removeButton, uploadButton, pasteButton;
URL uploadImgURL,browseImgURL,removeImgURL,pasteImgURL;
//Uploader default constructor
public void init() {
uploadImgURL = UploaderApplet.class.getResource("upload.png");
browseImgURL = UploaderApplet.class.getResource("browse.png");
removeImgURL = UploaderApplet.class.getResource("remove.png");
pasteImgURL = UploaderApplet.class.getResource("paste.gif");
Action uploadAction = new UploadAction("Upload",new ImageIcon(uploadImgURL));
Action pasteAction = new PasteAction("Paste",new ImageIcon(pasteImgURL));
Action browseAction = new BrowseAction("Browse...",new ImageIcon(browseImgURL));
Action removeAction = new RemoveAction("Remove",new ImageIcon(removeImgURL));
Action selectAllAction = new SelectAllAction("Select All");
JPanel cp = new JPanel();
cp.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
cp.setLayout(new BorderLayout());
setContentPane(cp);
model = new DefaultListModel();
list = new JList(model);
list.getInputMap().put(KeyStroke.getKeyStroke(65,KeyEvent.META_MASK),"selectAllAction");
list.getActionMap().put("selectAllAction",selectAllAction);
list.getInputMap().put(KeyStroke.getKeyStroke(8,0),"removeAction");
list.getActionMap().put("removeAction",removeAction);
list.setVisibleRowCount(12);
list.setCellRenderer(new ImageCellRenderer());
target = new DropTarget(list,new FileDropTargetListener());
cp.add(new JScrollPane(list),BorderLayout.CENTER);
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,5,10));
JLabel uploadLabel = new JLabel();
uploadLabel.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
uploadButton = new JButton(uploadAction);
uploadLabel.add(uploadButton);
buttons.add(uploadButton);
addButton = new JButton(browseAction);
removeButton = new JButton(removeAction);
pasteButton = new JButton(pasteAction);
buttons.add(removeButton);
buttons.add(addButton);
buttons.add(pasteButton);
cp.add(buttons,BorderLayout.SOUTH);
addKeyListener(this);
currentDir = new File(this.getCodeBase().getPath());
}//end of Default Uploader() constructor
public void keyTyped(KeyEvent evt) {
int key = evt.getKeyCode(); // Keyboard code for the pressed key.
System.out.println("KEY CODE-KEY TYPED->"+key+"<");
} // end keyTyped()
public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode(); // Keyboard code for the pressed key.
System.out.println("KEY CODE-KEY PRESSED->"+key+"<");
} // end keyTyped()
public void keyReleased(KeyEvent evt) {
int key = evt.getKeyCode(); // Keyboard code for the pressed key.
System.out.println("KEY CODE-KEY RELEASED->"+key+"<");
} // end keyTyped()
}
--
thanks
batta
import java.applet.*;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends JApplet implements KeyListener{
JButton myButton = new JButton("test");
JPanel cp = new JPanel();
/**
* @param args
*/
public void init(){
cp.setLayout(new FlowLayout());
getContentPane().setLayout(new FlowLayout());
cp.add(myButton);
cp.addKeyListener(this);
getContentPane().add(cp);
cp.setFocusable(true);
cp.requestFocus();
cp.setBackground(new Color(10,20,30));
}
public void keyPressed(KeyEvent e){
showStatus("KEY PRESSED :" + e.getKeyChar());
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
//showStatus("ANY KEY TYPED");
}
}
To any component use eventlistener that component needs to gain the focus first.
So when you type a letter you can see the status bar changing the information, but when you click a button the jpanel loses the focus and cannot obtain again until you make some code to do.
I think this code helps you.