textfield stole focus from applet

E

Eddie Hsu

I have a paint program and say when i paint a square in the paint()
function, I was able to click the square then use up/down/left/right
to move it. However, after I added a textfield to my applet, the
keyListener does not work anymore; the square no longer moves. I
understand that the textfield is probably the first component in my
program which stole the focus and somehow causing conflict with my
keyListener. How can i lose defocus the textfield and get it back to
the applet (the default focus before the introduction of textfield).
Or how can i requestFocus on the applet without the focus
automactically slipping back to the textfield?
 
E

Eddie Hsu

Let the code (mostly) do the talking.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class testing extends Applet implements KeyListener
{

/**/private TextField tf = new TextField("", 10);
private int x = 100, y = 20;
private int mpx, mpy;

public void init()
{
addKeyListener(this);
/**/ add(tf);
/**/ tf.setBounds(20, 100, 125, 20);
}

public void update(Graphics g) {paint(g);}

public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(x, y, 50, 50);
}
public void keyPressed(KeyEvent ke)
{
if ((ke.getKeyCode() == 37) || (ke.getKeyChar() == 'a'))
x--;
repaint();
}

public void keyTyped(KeyEvent ke) {}
public void keyReleased(KeyEvent ke) {}
}

Ok here is the code like how I described, the left arrow should move
the rectangle after the user clicked on the applet. But it won't
because the textfield stole focus (with or without cursor showing),
where the left arrow is sucked in by the textfield like a blackhole.
Well, after you uncomment the three lines of code relating to the
textfield which I marked, the focus on the applet works again.
 
A

Andrew Thompson

...
Ok here is the code like how I described, the left arrow should move
the rectangle after the user clicked on the applet.

OK, a few things..

1) I forgot to mention that a better group for
GUI related matters is c.l.j.gui, further described here..
<http://www.physci.org/codes/javafaq.jsp#cljg>

2) Good (well prepared) example.

3) My first point was my excuse for the hackishness
(and lack of meaningful documentation) in this example.. ;-)

<sscce>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class testing extends Applet
{

private TextField tf = new TextField("", 10);

public void init()
{
PaintContainer pc = new PaintContainer();
add( pc );
add(tf);
tf.setBounds(20, 100, 125, 20);
pc.requestFocus();
}
}

class PaintContainer extends Container implements KeyListener {

private int x = 100, y = 20;
private int mpx, mpy;

PaintContainer() {
addKeyListener(this);
}

public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(x, y, 50, 50);
}

public boolean isFocusable() {
return true;
}

public void keyPressed(KeyEvent ke)
{
if ((ke.getKeyCode() == 37) || (ke.getKeyChar() == 'a'))
x--;
repaint();
}

public Dimension getPreferredSize() {
return new Dimension(200,200);
}

public void keyTyped(KeyEvent ke) {}
public void keyReleased(KeyEvent ke) {}
}
</sscce>

HTH
 
E

Eddie Hsu

Thanks a lot for your help Andrew Thompson. Turns out I did not need
to create a seperate container class. The applet itself is already a
container. All I needed to do was simply add the isFocusable()
function to the applet class and everything turned out great. I was
searching for this exact command and had never found it through
countless threads and tutorials.

Here is the change that made everything work for future reference:

public void init()
{
addKeyListener(this);
add(tf);
tf.setBounds(20, 100, 125, 20);
requestFocus();
}

public boolean isFocusable() {
return true;
}
 
A

Andrew Thompson

Thanks a lot for your help Andrew Thompson.

You're welcome.
Turns out I did not need
to create a seperate container class.

Cool. I just generally find it a bad idea to
draw anything directly to a Frame or Applet..

The thing is, a Container can be dropped into
either, so when you later decide you want an
application from your applet, or vice versa,
it becomes trivially easy if your entire UI is
contained within a single Container.

(+ I like to 'encapsulate' both functionality
and leading from that, problems, in separate
classes. )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top