Results 1 to 3 of 3
  1. #1
    Suren is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default How to delete a JLabel by using the keyboard 'delete' key?

    Hi all,

    I want to select a particular label and delete it by keyboard's 'delete' key.
    I'm new to swings please help me ...
    Thanks in advance.:)

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Register a keyEventListener or a key binding. See Writing Event Listenersl
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Suren is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Smile

    Thank you,
    The following code worked out,

    component.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    component.setFocusable(true);
    component.requestFocus();
    component.addKeyListener(new KeyListener() {

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
    System.out.println(e.getKeyCode());

    if (e.getKeyCode() == 127) {
    int i = JOptionPane.showConfirmDialog(null, "Do you want to delete?");
    if (i == 0) {
    container.remove(component);
    container.validate();
    container.repaint();
    }
    }
    }

    public void keyReleased(KeyEvent e) {
    }
    });
    }

    });

Similar Threads

  1. Test - please delete
    By Fubarable in forum Forum Lobby
    Replies: 7
    Last Post: 04-25-2009, 08:08 PM
  2. help to delete
    By andykots in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2009, 08:29 AM
  3. How to delete the records
    By kiran kumar in forum Java Servlet
    Replies: 6
    Last Post: 11-09-2008, 01:16 PM
  4. Delete
    By Sarinam in forum New To Java
    Replies: 6
    Last Post: 07-23-2008, 11:09 AM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •