Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

No windowClosing() with keyboard?

Thread view: 
Bernard - 07 Jan 2004 21:56 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.