Results 1 to 6 of 6

Thread: this not working [resolved]

Threaded View

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Angry this not working [resolved]

    For some reason "this" seems to be not working. Im am trying to
    disable the focus manager to have all key events sent to the container for processing by overriding isManagingFocus(). For some reason the componets are not being laid out and the Listeners are not being registered and i can't figure out why. The code gets a clean compile but that's about it.
    Code:
     import javax.swing.*;
     import java.awt.*; 
     import java.awt.event.*; 
    
     public class Focus{
      public static void main(String[] args){
       Cycle c  = new Cycle();
       c.setVisible(true);
       c.setSize(300,250);
      }
     }
    
     class Cycle extends JFrame{
       
        private Container c;
        private int i; 
        private JLabel firstname;
        private JLabel middlename;
        private JLabel lastname;        
        private JTextField firstfield;
        private JTextField middlefield;     
        private JTextField lastfield;
    
        public void Cycle(){
          firstname = new JLabel("Joe", JLabel.RIGHT);
          middlename = new JLabel("Bob",JLabel.RIGHT);
          lastname = new JLabel("Brigs",JLabel.RIGHT);
    
          firstfield = new JTextField();
          middlefield = new JTextField();
          lastfield = new JTextField();   
    
          Object[] o = new Object[3];      
          o[0] = firstfield;
          o[1] = middlefield;
          o[2] = lastfield;
      
          addWindowListener(this);
          addKeyListener(this, o);
          layoutComps(this);
      }
    
       public boolean isManagingFocus(){
          return true;
       } 
    
      public void layoutComps(JFrame jf){
          Container c = jf.getContentPane();    
          c.setLayout(new GridLayout(3,2,5,5));
          c.add(firstname);
          c.add(firstfield);  
          c.add(middlename);
          c.add(middlefield);
          c.add(lastname);
          c.add(lastfield);
          
        }
      public void addWindowListener(JFrame jf){
          
       jf.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){
              System.exit(0);
           }
         });
        }
     
     public void addKeyListener(JFrame jf, final Object[] o){
      
       jf.addKeyListener(new KeyAdapter(){
          
          public void keyTyped(KeyEvent k){
         
          char keytyped = k.getKeyChar();
            if(keytyped == 97){ // lower case a
              if(i == 3){i = 0;}
               JTextField jtf = (JTextField) o[i];
                jtf.requestFocus();
                ++i;
            }
           }
          });
         }
        }

Posting Permissions

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



Click Here to Expand Forum to Full Width