|
-
Jun 28th, 2002, 01:28 AM
#1
Thread Starter
Dazed Member
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;
}
}
});
}
}
Last edited by Dilenger4; Jul 10th, 2002 at 11:52 PM.
-
Jun 28th, 2002, 02:53 PM
#2
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".
-
Jun 28th, 2002, 11:46 PM
#3
Thread Starter
Dazed Member
-
Jun 29th, 2002, 12:08 AM
#4
Thread Starter
Dazed Member
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.
-
Jun 29th, 2002, 12:43 PM
#5
implements actionlistener
-
Jun 29th, 2002, 05:36 PM
#6
Thread Starter
Dazed Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|