JDK-5098565 : REGRESSION: Focus problems with JTextAreas
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2004-09-08
  • Updated: 2004-09-15
  • Resolved: 2004-09-15
Related Reports
Duplicate :  
Description
Name: gm110360			Date: 09/08/2004


FULL PRODUCT VERSION :
java version "1.5.0-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-rc-b63)
Java HotSpot(TM) Client VM (build 1.5.0-rc-b63, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux dhcppc0 2.6.3-16mdk #1 Fri Aug 13 16:33:14 MDT 2004 i686 unknown unknown GNU/Linux

A DESCRIPTION OF THE PROBLEM :
I am the author of a small OpenSource flashcard application (http://pauker.sourceforge.net/). It works great with Java 1.4.x. Users reported that it does not work with Java 1.5 Beta. So I took a closer look at your latest release candidate of Java 5 and I found a problem with the focus handling of JTextAreas (in combination with JComboBoxes).

Short description:
I have one JComboBox with font names and two JTextAreas with potentially different fonts. If the user selects a font from the JComboBox the font of the focussed JTextArea changes. If the user clicks into one JTextArea the JComboBox displays the active font of the selected JTextArea.

If the program is run with Java 1.5.0 it is at first impossible to type in any character into the focussed JTextArea. If you type something on the keyboard nothing shows up. All the characters suddenly appear at the other JTextArea if you click on it.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the test case with Java 1.4.2 and see how it should work. Execute the test case with Java 1.5.0 and see how it should NOT work.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect the characters to appear in the focussed JTextArea.
ACTUAL -
The characters appear at another JTextArea if gets the focus.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;

public class ComboTest extends javax.swing.JFrame {
    
    private static final int LEFT  = 0;
    private static final int RIGHT = 1;
    private int focus;
    
    public ComboTest() {
        initComponents();
        DefaultComboBoxModel fontComboModel = new DefaultComboBoxModel();
        String[] fontFamilyNames = GraphicsEnvironment.
        getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for (int i = 0; i < fontFamilyNames.length; i++) {
            fontComboModel.addElement(fontFamilyNames[i]);
        }
        fontComboBox.setModel(fontComboModel);
    }
    
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        fontComboBox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        leftTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        rightTextArea = new javax.swing.JTextArea();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setTitle("ComboTest");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        fontComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fontComboBoxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(fontComboBox, gridBagConstraints);

        leftTextArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                leftTextAreaFocusGained(evt);
            }
        });

        jScrollPane1.setViewportView(leftTextArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        rightTextArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                rightTextAreaFocusGained(evt);
            }
        });

        jScrollPane2.setViewportView(rightTextArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane2, gridBagConstraints);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
    }
    
    private void rightTextAreaFocusGained(java.awt.event.FocusEvent evt) {
        focus = RIGHT;
        fontComboBox.setSelectedItem(rightTextArea.getFont().getFamily());
    }
    
    private void fontComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
        if (focus == LEFT) {
            changeFont(leftTextArea);
        } else {
            changeFont(rightTextArea);
        }
    }

    private void changeFont(JTextArea textArea) {
        Font oldFont = textArea.getFont();
        int fontSize = oldFont.getSize();
        int fontStyle = oldFont.getStyle();
        String fontFamilyName = (String)fontComboBox.getSelectedItem();
        Font font = new Font(fontFamilyName, fontStyle, fontSize);
        textArea.setFont(font);
        textArea.requestFocus();
    }
    
    private void leftTextAreaFocusGained(java.awt.event.FocusEvent evt) {
        focus = LEFT;
        fontComboBox.setSelectedItem(leftTextArea.getFont().getFamily());
    }
    
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    public static void main(String args[]) {
        new ComboTest().show();
    }
    
    private javax.swing.JComboBox fontComboBox;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea leftTextArea;
    private javax.swing.JTextArea rightTextArea;
    
}

---------- END SOURCE ----------

Release Regression From : 1.4.2
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Incident Review ID: 301761) 
======================================================================

Comments
EVALUATION ComboBox.setSelectedItem does something strange to the focus. Try commenting it out and application works fine. Focus lost does not get called for the text component but key events are not delivered either. Reassigning to ###@###.### I see in the comments that this is only reproducible on Linux. Sounds like an AWT focus issue. ###@###.### 2004-09-14 Name: at153223 Date: 09/15/2004 The same problem as in the bug 5082319, that is requesting focus from focus listener routine. So, I closed it as a duplicate. ======================================================================
17-09-2004