Results 1 to 6 of 6

Thread: this not working [resolved]

  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;
            }
           }
          });
         }
        }

  2. #2
    VirtuallyVB
    Guest
    Hey there Dil,

    1) I would have expected a compile error for the Cycle Constructor. Constructors are supposed to have no return type, not even void.

    2) I see the components when I place the "c.setVisible" after the "c.setSize". I can't explain that now; I'm getting rusty.

    3) Which object do you want to "implement KeyListener"? Class Cycle?
    Code:
     class Cycle extends JFrame implements KeyListener{
    4) Which objects should add themselves to a KeyListener? Surely your Object o array.
    ((JTextField)o[n]).addKeyListener(this);
    Where you might want to loop through n or add them explicitly 0,1,2.

    Or maybe you are trying to have your own KeyListener and "this" will be replaced by "that".

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    1) I knew that.

    2) I changed the properties around and nothing happens for me.

    3) I wanted the JFrame to implement the key listener. Since the Cycle class is extending the JFrame i figured i could add it using this

    4) What i basically wanted to do is overide the focus manager so instead of having to use the tab key to tab through the contained components, another key could be used instead. But i also wanted to see if i could just cycle through a specific set of components in a different sequence. I guess im just trying to stop the rust from forming.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I took out the method calls that were in the constructor but now im stumped on how i would go about adding a listener to a component that is being subclassed. Now for somthing like the following.
    Code:
    this.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){
              System.exit(0);
           }
         });
    I keep getting a illegal start of type at the this part.

  5. #5
    DaoK
    Guest
    implements actionlistener

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I figured out how to register my listeners to my subclass and eveything works well when the window events fire off. All i need now is to figure out how to overide the ismanagingFocus() method to overide the focus manager.
    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.setSize(300,250);
       c.setVisible(true);
      }
     }
    
     class Cycle extends JFrame{
       
        private final Object[] o = new Object[3]; 
        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 Cycle(){
    
          enableEvents(AWTEvent.WINDOW_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); 
          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();   
            
          Container c = this.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);
          
          o[0] = firstfield;
          o[1] = middlefield;
          o[2] = lastfield;
       } 
    
      public void processWindowEvent(WindowEvent e){
            if(e.getID() == WindowEvent.WINDOW_CLOSING){
                System.exit(0);
                 super.processWindowEvent(e); 
          }
      }
        
      public void processKeyEvent(KeyEvent k){    
        if(k.getID() == KeyEvent.KEY_TYPED){
          char keytyped = k.getKeyChar();
            if(keytyped == 97){ // lower case a
              if(i == 3){i = 0;}
               JTextField jtf = (JTextField) o[i];
                jtf.requestFocus();
                ++i;
           }
         }
       } 
      
      // private void ismanagingFocus(boolean){
      //    super(true);
      // } 
    
     }

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