Reputation: 3442
This is my code :
public class JJD extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public JJD() {
super("test");
JPanel center = new JPanel();
center.setBackground(Color.red);
JScrollPane pane = new JScrollPane(center);
this.getContentPane().add(pane, BorderLayout.CENTER);
final JFrame fr = this;
this.addWindowFocusListener(new WindowFocusListener() {
@Override
public void windowLostFocus(WindowEvent arg0) {
// TODO Auto-generated method stub
if (fr.isValid()) {
System.out.println("FOCUS VALID!");
} else {
System.out.println("Focus INVALID");
}
}
@Override
public void windowGainedFocus(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
this.setVisible(true);
this.setSize(500, 500);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame fr = new JJD();
fr.validate();
}
}
Could you tell my why the listener prints out that my frame is not valid? I tried also other methods like pack()
revalidate()
and still, the listener prints out that my frame is not valid.
HINT : The problem is because of the JScrollPane
but I can't figure out how to make my frame valid.
Upvotes: 1
Views: 178
Reputation: 1194
I think it might be because you are not override windowsGainedFocus method .
public void windowGainedFocus(WindowEvent arg0) {
if (fr.isValid()) {
System.out.println("FOCUS VALID!");
} else {
System.out.println("Focus INVALID");
}
}
You can override it just like the other one and look if it is valid. If it is not work you can add fr.validate()
method call inside the else clause. I hope it would help.
Upvotes: 2