Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-11-2009
Member
 
Join Date: Oct 2009
Posts: 67
Component event in JPanel

I made a JPanel and several JTextFields in the Panel. The KeyEvent occurs in the JTextField, I want the event to occure in the panel using JKeyEvent. How to override JPanel with KeyEvent to get JTextFields. I am calling super.precessKeyEvent but it's not working
Code:
protected void processKeyEvent(KeyEvent e)
{
char KeyChar;
KeyChar=e.getKeyChar();
System.out.println("test:Panel");
}
Code:
protected void processKeyEvent(KeyEvent e)
{
System.out.println("test:TextField");
super.processKeyEvent(e);
}
Reply With Quote
  #2  
Old 04-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Component event in JPanel

It would be better if you posted your full source code. Observer the following code, this is an easy and common code. It will definitely help you.
class ForwardTest extends JPanel
Code:
{
    public ForwardTest()
    {
        JButton b=new JButton("b1");
        myJTextField t=new myJTextField(10);
        System.out.println("Default layout manager for JPanel: " +
                            getLayout().getClass().getName());
//        setLayout(new FlowLayout());

        add(b);
        add(t);
        enableEvents(AWTEvent.KEY_EVENT_MASK);
        setFocusable(true);
    }

    protected void processKeyEvent(KeyEvent e)
    {
        System.out.println("test:Panel " +
                            e.getKeyText(e.getKeyCode()));
    }

    class myJTextField extends JTextField
    {
        public myJTextField(int leng)
        {
            super(leng);
            enableEvents(AWTEvent.KEY_EVENT_MASK);
        }

        protected void processKeyEvent(KeyEvent e)
        {
            System.out.println("test:TextField " +
                                e.getKeyText(e.getKeyCode()));
            // Forward this event to the parent JPanel.
            getParent().dispatchEvent(e);
            // Pass it on.
            super.processKeyEvent(e);
        }
    }

    public static void main(String args[])
    {
        JFrame f=new JFrame();
        f.add(new ForwardTest());
        f.setSize(300,300);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Component event in JPanel"
Thread Thread Starter Forum Replies Last Post
Display JtextField in Jpanel New ID Software Development 5 04-03-2010 12:01 PM
JPanel class of java Ram Bharose Software Development 4 16-02-2010 11:01 AM
Help: Java swing JPanel to repaint PaintComponent( Graphics g ) grudge Software Development 4 22-07-2009 07:05 PM
Display an image in JPanel S_Asnodkar Software Development 3 09-05-2009 10:23 PM
Event Log Error: Event Source:WinMgmt Event ID:10 BlackSunReyes Small Business Server 2 01-03-2007 03:27 AM


All times are GMT +5.5. The time now is 03:16 AM.