JDK-4181306 : Focus on wrong component after 2 dialogs
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1998-10-14
  • Updated: 2000-10-12
  • Resolved: 2000-10-12
Related Reports
Duplicate :  
Description

Name: mf23781			Date: 10/14/98


If there are 2 dialogs (JOptionPane.showMessageDialog(..)one 
after the other in an application (such as what happens with
the JFileChooser example in SwingSet),when the first is dismissed, 
the focus moves a component in the main app (such as JMenu or 
JTabbedPane), then to the button in the dialog and then BACK to 
the Component in the main app. It should remain on the button 
in the dialog since that is where keyboard input is directed. 
This is a SERIOUS problem for assistive technology trying to 
identify and speak the correct location of where input is directed.
Here a code sample to show this.. notice you need to have
the EventQueueClass loaded via the awt.properties and the line:

AWT.EventQueueClass=com.sun.java.accessibility.util.EventQueueMonitor

If you do alt-O to get to Options menu item and then activate
the 'Use this' menu, you see the message dialog, dismiss this,
and you see the second.. check the output of where focus is to
see the problem.

/*
 * FocusTest.java
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.accessibility.*;
import javax.swing.table.*;
import javax.swing.border.*;
import javax.swing.event.*;
import com.sun.java.accessibility.util.*;
import java.io.*;
/**
 * <P>FocusTest is an example app that uses MenuBar
 *
 * @version     1.0 07/28/97
 * @author      Fran Brown
 */
public class FocusTest extends JPanel  implements FocusListener, ActionListener{
   public static int INITIAL_WIDTH = 400;
   public static int INITIAL_HEIGHT = 200;
    JFrame jf = new JFrame("Focustest");
    BorderLayout bdl = new BorderLayout();
    JTabbedPane tabbedPane;
    JMenuBar menuBar = new JMenuBar();
    // Create the GUI
    //
    public FocusTest() {
       System.out.println("Starting...");

       SwingEventMonitor.addFocusListener((FocusListener)this);
       setLayout(bdl);
       // Create a tab pane
       tabbedPane = new JTabbedPane();
       add(tabbedPane, BorderLayout.CENTER);

        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
            public void windowIconified(WindowEvent e) {System.exit(0);}
        };
        jf.addWindowListener(l);

        JPanel logoPanel = new JPanel();
        JPanel lP = new JPanel();
        tabbedPane.addTab("First", null, logoPanel);
        tabbedPane.addTab("Second", null, lP);
        // set the Tab's AccessibleName 'cause we are using a graphic only
        tabbedPane.setSelectedIndex(0);



        JMenu choosers = (JMenu) menuBar.add(new JMenu("Options"));
        choosers.setMnemonic('O');
        JMenuItem mi = (JMenuItem) choosers.add(new JMenuItem("Option 1"));

        mi = (JMenuItem) choosers.add(new JMenuItem("Use this"));
        ActionListener startChooser = new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                   JOptionPane.showMessageDialog(FocusTest.this, "No file chosen");
                   JOptionPane.showMessageDialog(FocusTest.this, "Second - No file chosen");

                }
            };
            mi.addActionListener(startChooser);


        add("North",menuBar);
        jf.getContentPane().add(this);
        jf.setSize(INITIAL_WIDTH, INITIAL_HEIGHT);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        jf.setLocation(screenSize.width/2 - INITIAL_WIDTH/2,
                screenSize.height/2 - INITIAL_HEIGHT/2);
        jf.show();
    }

    static public void main(String args[]) {
        new FocusTest();
    }

    public void focusGained(FocusEvent theEvent) {
       Object o = theEvent.getSource();
       if (o instanceof Component) {
           System.out.println(new String("FocusTest:Focus gained.. " )+ o);
       }
    } // focusGained

    public void focusLost(FocusEvent theEvent) {
          // Do Nothing
    } // focusLost

    public void actionPerformed(ActionEvent e) {
       int xx = JOptionPane.showConfirmDialog(FocusTest.this, "No file chosen");
       JOptionPane.showMessageDialog(FocusTest.this, "Second - No file chosen");
    }
}


======================================================================

Name: krT82822			Date: 05/14/99


I am trapping a focusLost event and popping a Dialog in that method.
When I pop the Dialog, the focusLost method is getting called again,
which pops a second Dialog.

Here is the code:

class LossDateListener extends FocusAdapter{
         
  public void focusGained(FocusEvent evt)
      {System.out.printnl("Focus Gained");}
                
  public void focusLost(FocusEvent evt){
                   
     System.out.println("Focus lost");
     String dateText = lossText.getText();
     DateRules dateRules = new DateRules();
     String status = dateRules.validateLossDate(dateText);
     if(!status.equalsIgnoreCase("OK"))
         JOptionPane.showMessageDialog(mainFrame, status,
                 "Invalid",  JOptionPane.INFORMATION_MESSAGE );
       
   }
            
}

I get two Focus lost messages and two Dialogs on top of each other.  
When I dismiss the dialog showing on top, the focus lost method
is called again which pops another Dialog.  So, I am trapped 
with unstoppable dialogs.

I am running this on WinNT4.0 service pack 4 and Win98
jdk1.1.7 or jdk1.2
jfc1.1.1 or the one with jdk1.2
(Review ID: 55128)
======================================================================

Comments
EVALUATION So far I've tested it just with SwingSet (not with the test program supplied), and it doesn't occur either on Windows or on Sloaris with 1.2. Since we are not making 1.1.x releases anymore, I will close this bug as "will not fix" as soon as I verify that the test program supplied works on 1.2 just as SwingSet does. hania.gajewska@Eng 1999-06-24 I looked at this some more in light of it being such a high CQI bug, so here is some more information. I still don't know exactly what's going on. First of all, it should be made clear that where the description talks about "focus", what they really mean is how the visuals (and, in the case of Windows, the event sequence) reflect the focus -- the focus itself is correct on all the platforms that I have tested, in that the key events actually go to the window that you expect and want them to go to. The behavior of the test program is different on Windows and Solaris. Some of this may be only because the underlying cause is a race which gets resolved differently on Windows and Solaris. But there are probably other differences. I can reproduce the bug in the program provided (modified to work with jdk1.3) only on Windows; on Solaris the program works correctly, except that after the second dialog is dismissed, the focus is set to the JFrame rather than the JTabbedPane. On Windows, there is an extra Focus Gained event reported on the JTabbedPane after the JButton in the second Dialog gets the focus (the events are still actually going to the JButton). I am not sure where this event comes from; it doesn't happen on Solaris. In SwingSet on Solaris, the behavior is different still, in that the tabbed pane reflects having focus while the dialogs are up (i.e., it never loses it). It doesn't actually matter there that there are two dialogs (at least not on Solaris) -- the behavior is the same with just one. In SwingSet on Windows, the tabbed pane correctly does not reflect focus during the first dialog, but does during the second; I guess that's consistent with the behavior of the test program on Windows as well. It is clear from my investigation so far that this is an awt bug. Therefore, it should be fixed with the rewrite of awt for Merlin. I will commit it for merlin, and stop at that. hania.gajewska@Eng 2000-03-08 I am attaching an updated version of the test program that works with jdk1.3. It requires that /usr/local/java/jraccess1.3/jraccess.jar be on your path -- it uses this to show what events are happening. I am also attaching the output from the test program on on windows. To produce this output, I followed the directions of pulling down the menu and selecting the item which asks to be selected. This caused two dialogs to pop up; dismissing the first one should have resulted in the last event being a focus gained on the button of the second dialog, but in fact this event is followed immediately by a focus gained on the tabbed pane. The key events are actually going to the button at this point, and it's the button's window that is active. Dismissing the second dialog causes the jtabbed pane to get another focus gained event. (It just occured to me that the program I attached also prints focus lost events, whereas the output I attached was printed before I added that feature. but it's the only output I have at the moment; no focus lost is sent to the tabbed pane between the last two focus gained. hania.gajewska@Eng 2000-03-08 Name: keR10081 Date: 10/11/2000 this bug is not reproducible after focus management enhancement putback and should be closed. ====================================================================== Name: keR10081 Date: 10/12/2000 would be closed as a duplicate of 4290675 upon Dave's request ====================================================================== Name: dkR10074 Date: 11/13/2000 ###@###.### 2000-11-13 SunOS_sparc promoted b34 - PASSED promoted b35 - PASSED Windows_NT_x86 promoted b34 - FAILED promoted b35 - PASSED ======================================================================
11-06-2004

WORK AROUND Name: mf23781 Date: 10/14/98 None. ======================================================================
11-06-2004