Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame & FocusListener

 
Pratik Khetia
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I want to generate an event when there is a mouse click outside the JFrame window.
Like in my chat application, I open a JFrame for personal chat but when the user clicks outside that it is going into to the Windows taskbar which I want to prevent...so does anybody has any idea ???
thanks
Pratik
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't you use focusGained(FocusEvent e),focusLost(FocusEvent e) ?
 
Pratik Khetia
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Kravets:
can't you use focusGained(FocusEvent e),focusLost(FocusEvent e) ?

I tried using that but its not working...
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...");
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic