Hi,
I have a strange basic problem with windowClosing() in a JFrame.
Has anybody seen this before?
Many thanks.
Bernard
/*
When I click on the "Test" button, then the "Right" button gets the
focus border although it is disabled.
The bad side effect is that the [Alt+F4] key combination no longer
closes the frame.
If jButtonCenter.requestFocus(); is coded, then on [Alt+F4], the focus
border jumps back to jButtonRight and closing does not work either.
This is tested with JDK 1.4 under Windows 98.
What would be a clean and simple method to avoid this strange
behavior?
*/
import javax.swing.*;
import java.awt.*;
public class FocusBorder extends javax.swing.JFrame {
public FocusBorder() {
initComponents();
}
public static void main(String[] args){
new FocusBorder().show();
}
private void initComponents() {
jPanelTop = new javax.swing.JPanel();
jButtonLeft = new javax.swing.JButton();
jButtonTest = new javax.swing.JButton();
jButtonRight = new javax.swing.JButton();
jPanelCenter = new javax.swing.JPanel();
jButtonCenter = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt)
{
exitForm(evt);
}
});
jButtonLeft.setText("Left");
jPanelTop.add(jButtonLeft);
jButtonTest.setText("Test");
jButtonTest.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButtonTestActionPerformed(evt);
}
});
jPanelTop.add(jButtonTest);
jButtonRight.setText("Right");
jPanelTop.add(jButtonRight);
getContentPane().add(jPanelTop, java.awt.BorderLayout.NORTH);
jButtonCenter.setText("Center");
jPanelCenter.add(jButtonCenter);
getContentPane().add(jPanelCenter,
java.awt.BorderLayout.CENTER);
pack();
}
private void jButtonTestActionPerformed(java.awt.event.ActionEvent
evt) {
jButtonTest.setEnabled(false);
jButtonRight.setEnabled(false);
}
private void exitForm(java.awt.event.WindowEvent evt) {
exit();
}
private void exit(){
System.exit(0);
}
private javax.swing.JButton jButtonCenter;
private javax.swing.JButton jButtonLeft;
private javax.swing.JButton jButtonRight;
private javax.swing.JButton jButtonTest;
private javax.swing.JPanel jPanelCenter;
private javax.swing.JPanel jPanelTop;
}
Ryan Stewart - 07 Jan 2004 23:41 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> Many thanks.
> Bernard
*snip helpful code sample*
It performs fine on my system, though the right button does get the focus
even though it's disabled. Adding jButtonCenter.requestFocus(); fixes it
though. I had no problems at any time closing it with Alt+F4. Running
Windows XP Home.
Bernard - 09 Jan 2004 07:28 GMT
Hi Ryan,
Thanks for your test.
It looks like this is Win98 specific, so I won't care too much.
When I add your code, then under Win98 the focus is temporarily
transferred away, but somehow goes back again. Strange.
Bernard
>> Hi,
>>
[quoted text clipped - 11 lines]
>though. I had no problems at any time closing it with Alt+F4. Running
>Windows XP Home.