JDK-4233427 : Dismissing an internal JOptionPane doesn't restore the focus
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 1999-04-27
  • Updated: 2001-07-11
  • Resolved: 2001-07-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 beta2Fixed
Related Reports
Relates :  
Description

Name: skT88420			Date: 04/27/99


/*
  Dismissing an internal JOptionPane doesn't restore the focus.
  
  I consider this a bug, since a JOptionPane shouldn't affect anything
  in the parent component. 
  
  Run this application. start typing into the field. When you exceed
  4 characters, an internal JOptionPane will appear. When you dismiss
  the message, the text field should still have the focus, but it doesn't.

  I tested this on Windows 98, but I'm assuming it's platform independent
*/

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.*;


public class OptionPaneFocusBug
  implements DocumentListener
{
  public static void main(String[] args)
  {
    JFrame mf = new JFrame("Option Pane Bug");
    mf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    mf.addWindowListener(new WindowAdapter()
      {
        public void windowClosing(WindowEvent evt)
        {
          System.exit(0);
        }
      });
    Container cp = mf.getContentPane();
    cp.setLayout(new FlowLayout());
    
    JTextField fld = new JTextField(40);
    Document doc = fld.getDocument();
    doc.addDocumentListener(new OptionPaneFocusBug(mf));
    cp.add(fld);
    
    mf.setBounds(10, 10, 400, 300);
    mf.show();
    fld.requestFocus();
  }
  
  JFrame myFrame;
  
  OptionPaneFocusBug(JFrame ff)
  {
    myFrame = ff;
  }
  
  public void insertUpdate(DocumentEvent evt)
  {
    Document doc = evt.getDocument();
    if (doc.getLength() > 4)
      JOptionPane.showInternalMessageDialog
        (myFrame.getContentPane(), "No more than 4 chars allowed");
  }
  
  public void removeUpdate(DocumentEvent e) {}
  public void changedUpdate(DocumentEvent e) {}
}
(Review ID: 57560) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin merlin-beta2 FIXED IN: merlin-beta2 INTEGRATED IN: merlin-beta2
14-06-2004

WORK AROUND Name: skT88420 Date: 04/27/99 Workaround: 1) Use showMessageDialog() instead of showInternalMessageDialog(). Unfortunately, this hangs on Windows 98. (I'm submitting that as a separate bug.) or 2) Put in code to save and restore the focus before and after the JOptionPane. This is a pain, because JTextComponent.getFocusedComponent() has package access, so I can't call it. And FocusManager doesn't tell me who has the focus. So I need to save and restore the focus myself in various FocusListeners. This is a pain in the buns. It's also error prone. That's why I feel this is a bug, and not an RFE. ======================================================================
11-06-2004

EVALUATION This is still reproducible on 1.3. This should be fixed because AWT's new merlin focus architecture will support remembering the "last focus" for a given window (restoring it properly). Although this is really an AWT problem, I'll leave this open so we can verify it works when the AWT focus architecture is implemented for merlin. amy.fowler@Eng 2000-03-03 The last focused component is now remembered, and focus is restored to it (assuming the component is visible). scott.violet@eng 2001-06-27
03-03-2000