Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:28:42+00:00 2026-05-29T22:28:42+00:00

public class MyPanel extends JPanel implements KeyListener { private char c = ‘e’; public

  • 0
public class MyPanel extends JPanel implements KeyListener {
    private char c = 'e';
    public MyPanel() {
        this.setPreferredSize(new Dimension(500,500));
        addKeyListener(this);
    }
    public void paintComponent(Graphics g) {
        super.repaint();
        g.drawString("the key that pressed is" + c, 250,250);
    }

    public void keyPressed(KeyEvent e) {
        c=e.getKeyChar();
        repaint();

    }

    public void keyReleased(KeyEvent e) {
    }


    public void keyTyped(KeyEvent e) {
        c=e.getKeyChar();
        repaint();
    }

    public static void main(String[] s) {
        JFrame f=new JFrame();
        f.getContentPane().add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }
}

I tried reading this yet didnt mange to understand how to simply implement a KeyListener. so what do i need to change for this to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:28:43+00:00Added an answer on May 29, 2026 at 10:28 pm

    Here are the reasons why it doesn’t work:

    1. The JPanel does not have the keyboard focus. (The frame has it.) You probably want to requestFocus when the panel is added to the screen.

    2. You need to call repaint when the graphic should change.

    3. You mustn’t call repaint in the paintComponent method.

    4. You need to clear the drawing area before drawing the string again (otherwise all characters will end up on top of each other).

    Here’s a complete working example:

    class MyPanel extends JPanel implements KeyListener {
        private char c = 'e';
    
        public MyPanel() {
            this.setPreferredSize(new Dimension(500, 500));
            addKeyListener(this);
        }
    
        public void addNotify() {
            super.addNotify();
            requestFocus();
        }
    
        public void paintComponent(Graphics g) {
            g.clearRect(0, 0, getWidth(), getHeight());
            g.drawString("the key that pressed is " + c, 250, 250);
        }
    
        public void keyPressed(KeyEvent e) { }
        public void keyReleased(KeyEvent e) { }
        public void keyTyped(KeyEvent e) {
            c = e.getKeyChar();
            repaint();
        }
    
        public static void main(String[] s) {
            JFrame f = new JFrame();
            f.getContentPane().add(new MyPanel());
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);
        }
    }
    

    Oh, and you may want to add f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to make the application terminate when you close the window. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider the following code: public class MyPanel extends JPanel { private long secretInfo
I can not access public method of my class MyPanel which extends JPanel .
public class SuperUser extends User implements Serializable{ private static final long serialVersionUID = 1L;
public class XPBN extends Activity{ private Map _map; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
public class ViewThread extends Thread { private Panel mPanel; private SurfaceHolder mHolder; private boolean
public class Greeter extends MovieClip { public function Greeter() { addEventListener(Event.ADDED_TO_STAGE, go); } private
public class CovariantTest { public A getObj() { return new A(); } public static
I have a class that extends JPanel with several buttons on it. I would
public class MyActivity extends Activity { /** Called when the activity is first created.
public class ReportView extends JFrame { Connection con=null; void showReport() throws SQLException, ClassNotFoundException, JRException

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.