posted 23 years ago
try running this example, it should work with the focusGained(), focusLost()...
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;
public class testEvents extends JApplet implements MouseListener, FocusListener{
Container container;
JPanel aPanel, aPanel2, aPanel3;
JButton button;
JTextField text;
public void init(){
container = getContentPane();
container.setLayout(new BorderLayout());
container.setBackground(Color.white);
aPanel = new JPanel();
//aPanel.setBackground(Color.white);
aPanel2 = new JPanel();
//aPanel2.setBackground(Color.white);
text = new JTextField(20);
button = new JButton("test");
button.addMouseListener(this);
button.addFocusListener(this);
aPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black,1),"Button Response"));
aPanel.add(text);
aPanel2.add(button);
container.add(aPanel2, BorderLayout.CENTER);
container.add(aPanel, BorderLayout.SOUTH);
}
public void mousePressed(MouseEvent e){
text.setText("You pressed a button");
}
public void mouseClicked(MouseEvent e){
text.setText("You clicked a mouse");
}
public void mouseEntered(MouseEvent e){
text.setText("You entered a mouse");
}
public void mouseExited(MouseEvent e){
text.setText(" ");
}
public void mouseReleased(MouseEvent e){
text.setText("You released a button");
}
public void focusGained(FocusEvent e){
text.setText("focus gained...");
}
public void focusLost(FocusEvent e){
text.setText("waiting for action...");
}
}
All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson