Results 1 to 4 of 4
  1. #1
    pandar2 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default how do i get componet event in Panel ?

    hi all.
    I am newer in java and English is not my native language.

    I made a JPanel and add several JTextFields in the Panel.
    If KeyEvent occur in JTextFiled ,I want to get Event in Panel using processJKeyEvent.
    When Focus is on JTextField and if key was pressed, I think code,super.processKeyEvent in overrided JTextField call processKeyEvent overrided JPanel.
    But It does not work.
    How do i get JTextFields event processKeyEvent overrided JPanel ?
    Even though I call super.precessKeyEvent,why it does not work ?
    The test code like this

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    class JPanelTest extends JPanel
    {
    public JPanelTest()
    {
    Button b=new Button("b1");
    myJTextField t=new myJTextField(10);

    setLayout(new FlowLayout());

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

    protected void processKeyEvent(KeyEvent e)
    {
    char KeyChar;

    KeyChar=e.getKeyChar();
    System.out.println("test:Panel");
    }

    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");
    super.processKeyEvent(e);
    }
    }

    public static void main(String args[])
    {
    JFrame f=new JFrame();
    JPanelTest jt=new JPanelTest();

    f.add(jt);
    f.setSize(300,300);
    f.setVisible(true);

    }
    }
    Last edited by pandar2; 11-02-2009 at 03:54 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    KeyEvents are dispatched to the component with focus.

    I don't really understand what you are attempting to do, but it seems like you should be using a Global Event Listener or maybe Key Bindings.

    I don't think there is ever an reason to override the processKeyEvent.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class ForwardTest extends JPanel
    {
        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);
        }
    }

  4. #4
    pandar2 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    Thnaks for reply.
    It done as I want.
    I try to make a three TextField input telephone number and want to like a excel cells with arrow key
    ,so I have to filter Numberic key input ,and control three Fileds caret.
    Such reason make me to get KeyEvent in JPanel.
    thanks again hardwi,camickr


    But I have a an other question ...
    I make a severial JTextFields for name,adderss,etc and add them JFrame using GridLayout.
    and add above three JTextField unsing FlowLayout in JPanel for telephone number and then add this JPanel on JFrame together.
    Because telephone number need to use 3 columns and they are not different size,I have been used FlowLayout.
    How to remove margine(?),space(?) in FlowLayout Manager ?

    code like this

    import javax.swing.*;
    import java.awt.*;

    class Customer extends JFrame
    {
    public Customer()
    {
    JTextField t1,t2,t3,t4,t5;
    JPanel p1;

    p1=new JPanel();

    t1=new JTextField(10);
    t2=new JTextField(10);

    p1.setLayout(new FlowLayout());

    t3=new JTextField(3);
    t4=new JTextField(4);
    t5=new JTextField(5);


    p1.add(t3);
    p1.add(t4);
    p1.add(t5);


    setLayout(new GridLayout(3,1));

    add(t1);
    add(t2);
    add(p1);

    setSize(300,120);
    setVisible(true);
    }

    public static void main(String[] args)
    {
    new Customer();

    }
    }
    Last edited by pandar2; 11-05-2009 at 04:53 PM.

Similar Threads

  1. Redraw panel in timer event
    By knuth in forum Advanced Java
    Replies: 6
    Last Post: 10-03-2009, 07:39 PM
  2. panel resizing
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 08-15-2009, 02:09 PM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  4. How to output to panel as a .gif
    By Teh Brian in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 05:26 PM
  5. panel resizing
    By kumar_gemi in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:51 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
  •