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





Reply With Quote